package srever; public class Users { private String username; private String password; public Users() { } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } } 建立一个logind的jsp<%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8"%> <!DOCTYPE html> <html> <head> <meta charset="ISO-8859-1"> <title>Insert title here</title> </head> <body> <h1>用户登录</h1> <hr> <form action="dologind.jsp" name="loginForm" method="post"> <table> <tr> <td>用户名:</td> <td><input type="text" name="username"/></td> </tr> <tr> <td>密码:</td> <td><input type="password" name="password"/></td> </tr> <tr> <td colspan="2"><input type="submit" value="登录"></td> </tr> </table> </body> </html>
<%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8"%> <!DOCTYPE html> <html> <head> <meta charset="ISO-8859-1"> <title>Insert title here</title> </head> <body> <jsp:useBean id="myUsers" class="srever.Users" scope="page"/> <h1>setproperty</h1><br> <!-- 更具表单匹配部分的属性 --> <jsp:setProperty name="myUsers" property="username"/> 用户名:<%=myUsers.getUsername() %><br> 密码:<%=myUsers.getPassword() %><br> </body> </html>
<%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8"%> <!DOCTYPE html> <html> <head> <meta charset="ISO-8859-1"> <title>Insert title here</title> </head> <body> <jsp:useBean id="myUsers" class="srever.Users" scope="page"/> <h1>setproperty</h1><br> <!-- 通过手工赋值给属性--> <jsp:setProperty name="myUsers" property="username" value="list"/> <jsp:setProperty name="myUsers" property="password" value="888888"/> 用户名:<%=myUsers.getUsername() %><br> 密码:<%=myUsers.getPassword() %><br> </body> </html>
%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8"%> <!DOCTYPE html> <html> <head> <meta charset="ISO-8859-1"> <title>Insert title here</title> </head> <body> <jsp:useBean id="myUsers" class="srever.Users" scope="page"/> <h1>setproperty</h1><br> <!-- 通过url传参数给属性赋值--> <jsp:setProperty name="myUsers" property="username"/> <jsp:setProperty name="myUsers" property="password" param="mypass"/> 用户名:<%=myUsers.getUsername() %><br> 密码:<%=myUsers.getPassword() %><br> </body> </html>