创建名为User的JavaBean
该JavaBean类包含username、password、useraddress和userId四个属性和一个默认构造方法。
代码:
package com.po;
public class User {
private String username;
private String password;
private String userId;//用户内部编号,有系统分配;
public void setUserId(String userId) {
this.userId = userId;
}
public String getUserId() {
return userId;
}
public void setUsername(String username) {
this.username = username;
}
public String getUsername() {
return username;
}
public void setPassword(String password) {
this.password = password;
}
public String getPassword() {
return password;
}
public User(){}
}
注册界面
Login.jsp页面
<%@ page contentType="text/html;charset=gb2312"%>
<script language="javascript">
function on_submit() {//验证数据的合法性
if (form1.username.value == "") {
alert("用户名不能为空,请输入用户名!");
form1.username.focus();
return false;
}
if (form1.userpassword.value == "") {
alert("用户密码不能为空,请输入密码!");
form1.userpassword.focus();
return false;
}
}
</script>
<%! public String codeToString(String str)
{//处理中文字符串的函数
String s=str;
try
{
byte tempB[]=s.getBytes("ISO-8859-1");
s=new String(tempB);
return s;
}
catch(Exception e)
{
return s;
}
} %>
<% String username=request.getParameter("username");
String userpassword=request.getParameter("userpassword");
if(username!=null&userpassword!=null)
{//如果用户名和密码都合法,记下用户名
session.setAttribute("username",codeToString(username));
response.sendRedirect("sessionUserLogin1.jsp");
}
%>
<html>
<head>
<title>用户注册</title>
</head>
<body>
<form name="form1" method="post" action=" LoginInfo.jsp?userId=0000 ">
账号:<input name="username" type="text"><br>
密码:<input name="password" type="password"><br>
<input type="submit" value="注册">
<input type="reset" value="重新输入">
</form>
</body>
</html>
页面效果:
显示页面
LigonInfo.jsp页面
该页面主要使用了<jsp: usebean>标签和<jsp: setProperty>,用于获取的用户信息。
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>用户信息</title>
</head>
<body>
<jsp:useBean id="myUser" class="com.po.User" scope="page"/>
<jsp:setProperty name="myUser" property="username"/>
<jsp:setProperty name="myUser" property="password"/>
<jsp:setProperty property="userId" name="myUser" value="0001"/><!-- 用户内部编号是由系统指定而非用户注册时指定 -->
账号:<%=myUser.getUsername() %><br>
密码:<%=myUser.getPassword() %><br>
编号:<%=myUser.getUserId() %><br>
</body>
</html>
页面效果: