第十个JSP作业-servlet注册

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>首页</title>
</head>
<body>
<h1>首页</h1>
 <hr>
 <a href="${pageContext.request.contextPath}/register.jsp">注册</a>
</body>
</html>

  

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<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="radio" name="sex" value="boy">男
        <input type="radio" name="sex" value="girl">女
    </p>
    <p>
        <input type="submit" value="注册">
        <input type="reset" value="返回">
    </p>
</form>
</body>
</html>

  

public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html");
        response.setCharacterEncoding("UTF-8");
        PrintWriter out = response.getWriter();
        String username = request.getParameter("username");
        String password1 = request.getParameter("password1");
        String password2 = request.getParameter("password2");
        String sex = request.getParameter("sex");
        if(password1.equals(password2)){
            HttpSession session = request.getSession();
            session.setAttribute("username",username);
            session.setAttribute("password1",password1);
            session.setAttribute("sex",sex);
            response.sendRedirect("success.jsp");
            request.getRequestDispatcher("success.jsp").forward(request,response);
        }else {
            System.out.println("注册失败");
            response.sendRedirect("register.jsp");
        }
        out.flush();
        out.close();
    }

  

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>注册成功</title>
<%
     HttpSession session1 = request.getSession();
     if(session1.getAttribute("username")==null){
    	 System.out.println("注册失败");
         response.sendRedirect("index.jsp");
        }
    %>
</head>
<body>
<h1>注册成功!</h1>
<p> 用户名:${sessionScope.username}</p>
<p> 密码:${sessionScope.password1}</p>
<p> 性别:${sessionScope.sex}</p>
</body>
</html>

  

原文地址:https://www.cnblogs.com/minmeishaonv/p/12971019.html