First, the registration requirements
1, a registration page
- username (textbox)
- password: password (password box)
- passwordYes: Enter the password (password box) again
- hobby (checkbox)
- Sex (radio button)
- info (text field)
2. servlet handling the registration information to determine whether the same password twice
- Skip to page the same success
- A difference in the registration fails, redirected to the registration page
3. Successful page
- Show successful landing of user names, passwords, preferences, gender, and information;
- Display a logout button
4. If the cancellation, or no login, not directly into the success page, jump to the failed interface
Second, the specific steps
1: Write a hyperlink index.jsp (Home), lets jump to the registration interface
2: After registration interface written submission to ServeltRegister processing
3: Go to ServletRegister registration interface, the user session into the message and determine whether the password is correct
4: If the password is successful, the successful jump to the interface, and displays the user interface fill in the information in success
5: After the success of the Page Setup button logout, the logout button is pressed, jump to ServletOut logout interface
6: When users log off the message, let it return to the success of the page, then prompts the user is not registered or logged off, please register
7: Setting the registration button in the prompt page, allowed to re-register, return to the registration page
8: If the password is output error, let it return to the registration page, and alert the bomb box
Third, the code
1: Home index.jsp
<html>
<head>
<title>首页</title>
<style>
body{
margin: 0 auto;
text-align: center;
}
</style>
</head>
<body>
<h1>首页</h1>
<hr>
<a href="${pageContext.request.contextPath}/register.jsp">注册</a>
</body>
</html>
2: Registration page register.jsp
<html>
<head>
<title>注册</title>
<script>
var status = '${sessionScope.pwdFail}';
if (status=='yes'){
alert("两次密码输入不一样,请重新输入")
}
</script>
</head>
<body>
<h1>注册</h1>
<form action="${pageContext.request.contextPath}/Register" method="post">
<p>用户名: <input type="text" name="username" required ></p>
<p>密码:<input type="password" name="password1" required></p>
<p>确认密码:<input type="password" name="password2" required></p>
<p>爱好:
<input type="checkbox" name="hobby" value="movie">电影
<input type="checkbox" name="hobby" value="TV">电视
<input type="checkbox" name="hobby" value="music">音乐
</p>
<p>性别:
<input type="radio" name="sex" value="boy">男
<input type="radio" name="sex" value="girl">女
</p>
<p>个人简介:
<textarea name="info"></textarea>
</p>
<p>
<input type="submit">
<input type="reset">
</p>
</form>
</body>
</html>
3: Successful page success.jsp
<html>
<head>
<title>注册成功</title>
<%
HttpSession session1 = request.getSession();
if(session1.getAttribute("username")==null){
request.getRequestDispatcher("loss.jsp").forward(request,response);
}
%>
</head>
<body>
<h1>注册成功!</h1>
<%--通过sessionScope得到信息--%>
<p> 用户名:${sessionScope.username}</p>
<p> 密码:${sessionScope.password1}</p>
<p> 性别:${sessionScope.sex}</p>
<p> 爱好:${sessionScope.hobbies}</p>
<p> 简介:${sessionScope.info}</p>
<a href="${pageContext.request.contextPath}/Out">注销</a>
</body>
</html>
4: Tips page loss.jsp
<html>
<head>
<title>失败</title>
</head>
<body>
<h1>
没有session信息,未注册或者已注销,请先注册
</h1>
<a href="${pageContext.request.contextPath}/register.jsp">注册</a>
</body>
</html>
5:web.xml
<servlet>
<servlet-name>ServletRegister</servlet-name>
<servlet-class>com.sunzhen.ServletRegister</servlet-class>
</servlet>
<servlet>
<servlet-name>ServletOut</servlet-name>
<servlet-class>com.sunzhen.ServletOut</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ServletOut</servlet-name>
<url-pattern>/Out</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>ServletRegister</servlet-name>
<url-pattern>/Register</url-pattern>
</servlet-mapping>
6: Registration ServletRegister
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
//获得用户提交的数据
String username = request.getParameter("username");
String password1 = request.getParameter("password1");
String password2 = request.getParameter("password2");
String sex = request.getParameter("sex");
String info = request.getParameter("info");
String[] hobbies = request.getParameterValues("hobby");
if(password1.equals(password2)){
//把用户消息放进session中
HttpSession session = request.getSession();
session.setAttribute("username",username);
session.setAttribute("password1",password1);
session.setAttribute("sex",sex);
session.setAttribute("info",info);
session.setAttribute(" hobbies", Arrays.toString(hobbies));
response.sendRedirect("success.jsp");
//request.getRequestDispatcher("success.jsp").forward(request,response);
}else {
request.getSession().setAttribute("pwdFail","yes");
response.sendRedirect("register.jsp");
}
7: cancellation ServletOut
HttpSession session = request.getSession();
//注销session或者移除session中的数据
session.removeAttribute("username");
session.removeAttribute("password1");
session.removeAttribute("sex");
session.removeAttribute("hobbies");
session.removeAttribute("info");
response.sendRedirect("success.jsp");