(1)创建一个名为UserInfo的javaBean,用来保存用户信息:
package com.Bean;
public class UserInfo {
private String name;
private String job;
public UserInfo(){
name="";
job="";
}
public String getName() {
return name;
}
public void setName(String name) throws Exception {
this.name = new String(name.getBytes("ISO-8859-1"),"gbk");
}
public String getJob() {
return job;
}
public void setJob(String job) throws Exception {
this.job = new String(job.getBytes("ISO-8859-1"),"gbk");
}
}
(2)创建提供用户输入注册信息的页面index.jsp:
<%@ page language="java" contentType="text/html; charset=gb2312"%>
<html>
<head>
<title>用户注册</title>
<link rel="stylesheet" type="text/css" href="css/style.css">
</head>
<body>
<center>
<form action="doreg.jsp" method="get">
<table border="1" style="margin-top:150" width="250">
<tr height="25" bgcolor="lightgrey">
<td align="center" colspan="2">用户注册</td>
</tr>
<tr>
<td align="right">用户名:</td>
<td align="center"><input type="text" name="name" size="29"></td>
</tr>
<tr>
<td align="right">职 务:</td>
<td align="center"><input type="text" name="job" size="29"></td>
</tr>
<tr>
<td align="center" colspan="2">
<input type="submit" value="注册">
<input type="reset" value="重置">
</td>
</tr>
</table>
</form>
<br>
</center>
</body>
</html>
(3)创建接受form表单的jsp页面doreg.jsp,在其中判断用户输入的信息并将请求转发至指定的页面中
<%@ page language="java" contentType="text/html; charset=gb2312"%>
<jsp:useBean id="us" class="com.Bean.UserInfo" scope="request"/>
<jsp:setProperty name="us" property="*"/>
<%
String name=us.getName();
String job=us.getJob();
if(name.equals("")||job.equals("")){
%>
<jsp:forward page="/false.jsp"/>
<% }else{ %>
<jsp:forward page="/success.jsp"/>
<% } %>
(4)创建false.jsp页面,显示注册失败提示信息
<%@ page language="java" contentType="text/html; charset=gb2312"%>
<html>
<head>
<title>注册失败!</title>
<link rel="stylesheet" type="text/css" href="css/style.css">
</head>
<body>
<center>
<table border="1" height="100" width="250" style="margin-top:150">
<tr bgcolor="lightgrey" height="25">
<td align="center">注册失败!</td>
</tr>
<tr>
<td align="center">请输入 <b>用户名</b> 或 <b>职务</b>!</td>
</tr>
</table>
<a href="index.jsp">返回</a>
</center>
</body>
</html>
(5)创建success.jsp页面,显示注册成功提示信息
<%@ page language="java" contentType="text/html; charset=gb2312"%>
<%@ page import="com.Bean.UserInfo" %>
<%
String username=((UserInfo)request.getAttribute("us")).getName();
String userjob=((UserInfo)request.getAttribute("us")).getJob();
%>
<html>
<head>
<title>注册成功</title>
<link rel="stylesheet" type="text/css" href="css/style.css">
</head>
<body>
<center>
<table border="1" width="250" height="100" style="margin-top:150" >
<tr height="25" bgcolor="lightgrey">
<td align="center">注册成功</td>
</tr>
<tr>
<td align="center">
<b>用户名:</b><%=username%>
<b>职 务:</b><%=userjob%>
</td>
</tr>
</table>
<a href="index.jsp">返回</a>
</center>
</body>
</html>
最终实现结果:
注册成功: