Servlet(4):一个简单的注册页面
一. 注册要求
1. 一个注册页面
- username (文本框)
- password:密码 (密码框)
- passwordYes :再次输入密码(密码框)
- hobby (多选框)
- sex (单选框)
- info (文本域)
2. servlet处理注册信息,判断两次密码是否相同
- 相同则跳到成功页面
- 不同则注册失败,重定向到注册页面
3. 成功页面
- 显示登陆成功的用户名,密码,爱好,性别和信息;
- 显示一个注销按钮
4. 如果注销后,或者没登录,不能直接进入成功页面,跳转到失败界面
二. 具体操作步骤
1:在index.jsp(首页)写一个超链接,让跳转到注册界面
2:写好注册界面后,提交给ServeltRegister处理
3:跳转到ServletRegister注册界面,把用户消息放进session中,并判断输入的密码是否正确
4:如果密码成功,跳转到成功界面,并在成功界面显示用户填写的信息
5:在成功页面设置注销按钮,按下注销按钮后,跳转到ServletOut注销界面
6:当用户消息注销后,让其回到成功页面,然后提示用户未注册或者已注销,请先注册
7:在提示页面设置注册按钮,让其重新注册,回到注册页面
8:如果密码输出错误,让其回到注册页面,并用弹框提醒
三. 代码
1:首页 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:注册页 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:成功页 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:提示页 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>
<span class="hljs-tag"><<span class="hljs-name">servlet-mapping</span>></span>
<span class="hljs-tag"><<span class="hljs-name">servlet-name</span>></span>ServletOut<span class="hljs-tag"></<span class="hljs-name">servlet-name</span>></span>
<span class="hljs-tag"><<span class="hljs-name">url-pattern</span>></span>/Out<span class="hljs-tag"></<span class="hljs-name">url-pattern</span>></span>
<span class="hljs-tag"></<span class="hljs-name">servlet-mapping</span>></span>
<span class="hljs-tag"><<span class="hljs-name">servlet-mapping</span>></span>
<span class="hljs-tag"><<span class="hljs-name">servlet-name</span>></span>ServletRegister<span class="hljs-tag"></<span class="hljs-name">servlet-name</span>></span>
<span class="hljs-tag"><<span class="hljs-name">url-pattern</span>></span>/Register<span class="hljs-tag"></<span class="hljs-name">url-pattern</span>></span>
<span class="hljs-tag"></<span class="hljs-name">servlet-mapping</span>></span>
6:注册 ServletRegister
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
<span class="hljs-comment">//获得用户提交的数据</span>
<span class="hljs-built_in">String</span> username = request.getParameter(<span class="hljs-string">"username"</span>);
<span class="hljs-built_in">String</span> password1 = request.getParameter(<span class="hljs-string">"password1"</span>);
<span class="hljs-built_in">String</span> password2 = request.getParameter(<span class="hljs-string">"password2"</span>);
<span class="hljs-built_in">String</span> sex = request.getParameter(<span class="hljs-string">"sex"</span>);
<span class="hljs-built_in">String</span> info = request.getParameter(<span class="hljs-string">"info"</span>);
<span class="hljs-built_in">String</span>[] hobbies = request.getParameterValues(<span class="hljs-string">"hobby"</span>);
<span class="hljs-keyword">if</span>(password1.equals(password2)){
<span class="hljs-comment">//把用户消息放进session中</span>
HttpSession session = request.getSession();
session.setAttribute(<span class="hljs-string">"username"</span>,username);
session.setAttribute(<span class="hljs-string">"password1"</span>,password1);
session.setAttribute(<span class="hljs-string">"sex"</span>,sex);
session.setAttribute(<span class="hljs-string">"info"</span>,info);
session.setAttribute(<span class="hljs-string">" hobbies"</span>, Arrays.toString(hobbies));
response.sendRedirect(<span class="hljs-string">"success.jsp"</span>);
<span class="hljs-comment">//request.getRequestDispatcher("success.jsp").forward(request,response);</span>
}<span class="hljs-keyword">else</span> {
request.getSession().setAttribute(<span class="hljs-string">"pwdFail"</span>,<span class="hljs-string">"yes"</span>);
response.sendRedirect(<span class="hljs-string">"register.jsp"</span>);
}
7:注销 ServletOut
HttpSession session = request.getSession();
//注销session或者移除session中的数据
session.removeAttribute("username");
session.removeAttribute("password1");
session.removeAttribute("sex");
session.removeAttribute("hobbies");
session.removeAttribute("info");
response.sendRedirect(<span class="hljs-string">"success.jsp"</span>);