实验任务
Weibo系统的注册和登陆已经有了V1版本,现在在此基础之上,迭代开发,
使用forward()实现:先LoginServlet进行登陆判断,如果用户名和密码正确,转到另一个MemberServlet实现登陆成功页面显示(因为登陆成功,需要把参数给携带过去,以便进行页面显示用户名);如果登陆失败,Redirect()回登陆页面index.html。(登陆失败,不需要携带参数)。
使用forward()实现:先RegisterServlet进行注册,如果注册成功,转到另一个SuccessServlet实现注册成功页面显示;注册失败,转到另一个ErrorServlet实现注册失败页面。
RequestDispatcher的使用:
利用RequestDispatcher对象可以将请求转发给其他的Servlet或JSP页面
RequestDispatcher rd = request.getRequestDispatcher(“path”);
rd.forward(request,response);
e.g.
RequestDispatcher rd = request.getRequestDispatcher("/Member");
rd.forward(request, response);
sendRedirect()的使用:
HttpServletResponse的sendRedirect()要求浏览器重新请求另一个URL,又称重定向(Redirect)
response.sendRedirect(url);
e.g.
response.sendRedirect(“http://localhost:8080/WeiboSystem/index.html”);
实验步骤:
创建view包,新建三个Servlet,它们的功能是专门绘制页面:注册失败(Error)、登陆成功(Member)、注册成功(Success)这三个页面。src目录结构如下图所示:
修改Login:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
String username = request.getParameter("username");
String password = request.getParameter("password");
//把users从servletcontext中取出来
ServletContext context = this.getServletContext();
HashMap<String,String> users = (HashMap)context.getAttribute("data");
//进行判断users中是否存在匹配的username和password
//如果存在,绘制成功界面
if(users.get(username).equals(password)){
//如果存在匹配的讲servlet转向MemberServlet
RequestDispatcher rd = request.getRequestDispatcher("/Member");
rd.forward(request, response);
}else{
//如果不存在那么重定向为index.html界面
**response.sendRedirect("http://localhost:8080/WeiboSystem/index.html");**
}
}
修改Register:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
String email = request.getParameter("email");
String username = request.getParameter("username");
String password = request.getParameter("password");
String confirmedPasswd = request.getParameter("confirmedPasswd");
//数据存放在users中
HashMap<String,String> users = new HashMap<>();
users.put(username, password);
//把users存放在ServletContext中,为以后其他的Servlet共享
ServletContext context = this.getServletContext();
context.setAttribute("data", users);
//判断两次密码是否相等
if(password.equals(confirmedPasswd)) {
//servletRegister将请求发给servletSuccess
RequestDispatcher rd = context.getRequestDispatcher("/Success");
rd.forward(request, response);
}else {
RequestDispatcher rd = context.getRequestDispatcher("/Error");
rd.forward(request, response);
}
}
Success:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//绘制注册成功界面
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE html PUBLIC '-//W3C//DTD HTML 4.01 Translation//EN'>");
out.println("<html>");
out.println("<head>");
out.println("<meta content='text/html;charset=UTF-8' http-equiv='content-type'>");
out.println("<title>会员注册成功</ title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>会员"+request.getParameter("username")+"注册成功</h1>");
out.println("<a href='index.html'>回首页登陆</ a>");
out.println("</body>");
out.println("</html>");
out.close();
}
Error:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE html PUBLIC '-//W3C//DTD HTML 4.01 Transitional//EN'");
out.println("<html>");
out.println("<head>");
out.println("<meta content='text/html; charset=UTF-8' http-equiv='content-type'>");
out.println("<title>会员注册失败</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>对不起您注册失败</h1>");
out.println("</body>");
out.println("</html>");
out.close();
}
Member:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE html PUBLIC '-//W3C//DTD HTML 4.01 Transitional//EN'");
out.println("<html>");
out.println("<head>");
out.println("<meta content='text/html; charset=UTF-8' http-equiv='content-type'>");
out.println("<title>会员登入成功</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>会员"+ request.getParameter("username") + "你好</h1>");
out.println("<a href='index.html'>回首页登入</a>");
out.println("</body>");
out.println("</html>");
out.close();
}
web.xml:
<servlet>
<servlet-name>Register</ servlet-name>
<servlet-class>controller.Register</ servlet-class>
</servlet>
<servlet>
<servlet-name>Login</ servlet-name>
<servlet-class>controller.Login</ servlet-class>
</servlet>
<servlet>
<servlet-name>Success</servlet-name>
<servlet-class>view.Success</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Register</servlet-name>
<url-pattern>/register.do</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Login</servlet-name>
<url-pattern>/login.do</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Success</servlet-name>
<url-pattern>/success.view</url-pattern>
</servlet-mapping>