JavaEE_Test2_Servlet

 1 package servlet;
 2 import javax.servlet.http.HttpSession;
 3 import java.io.IOException;
 4 import java.io.PrintWriter;
 5 public class Register extends javax.servlet.http.HttpServlet {
 6     private void doProcess(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {
 7         String studentName = request.getParameter("studentName");
 8         String pairname = request.getParameter("pairname");
 9         String parentname = request.getParameter("parentname");
10         String p1 = request.getParameter("password");
11         String p2 = request.getParameter("password2");
12         String type = request.getParameter("catagloryID");
13         System.out.println("studentName" + studentName);
14         System.out.println("pairname" + request.getParameter("pairname"));
15         System.out.println("parentname" + request.getParameter("parentname"));
16         System.out.println("password" + p1);
17         System.out.println("password2" + p2);
18         HttpSession session = request.getSession();
19         session.setAttribute("studentName",studentName);
20         session.setAttribute("pairname",pairname);
21         session.setAttribute("parentname",parentname);
22         if(type == null)
23         {
24             session.setAttribute("wrongMsgType","必须选择其中之一");
25         }
26         if(type == "1")
27         {
28             System.out.println("catagloryID" + "初中英语");
29         }
30         else if(type == "2")
31         {
32             System.out.println("catagloryID" + "TOEFL");
33         }
34         else if(type == "5")
35         {
36             System.out.println("catagloryID" + "高中英语");
37         }
38         else if(type == "6")
39         {
40             System.out.println("catagloryID" + "GRE");
41         }
42         else if(type == "7")
43         {
44             System.out.println("catagloryID" + "SAT");
45         }
46         request.setAttribute("studentName",studentName);
47         request.setAttribute("pairname",pairname);
48         request.setAttribute("parentname",parentname);
49         if(!p1.equals(p2))
50         {
51             session.setAttribute("wrongMsgPw","两次密码不同");
52         }
53         request.getRequestDispatcher("register.jsp").forward(request,response);
54     }
55     protected void doPost(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {
56         doProcess(request,response);
57     }
58     protected void doGet(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {
59         doProcess(request,response);
60     }
61 }
  1 <%@ page language="java" contentType="text/html; charset=utf-8"
  2          pageEncoding="utf-8"%>
  3 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  4 <html>
  5 <head>
  6     <title>Register User</title>
  7     <meta http-equiv="pragma" content="no-cache">
  8     <meta http-equiv="cache-control" content="no-cache">
  9     <meta http-equiv="expires" content="0">
 10     <meta http-equiv="keywords" content="English word">
 11 </head>
 12 <style>
 13     h7 {
 14         color:red;
 15     }
 16 </style>
 17 <body bgcolor="#DFFFBF">
 18 <h1>注册信息:</h1>
 19 <hr/>
 20 
 21 <form id="registerForm" name="registerForm" method="post"  action="Register"  >
 22     <table>
 23         <tr>  </tr>
 24         <tr align="left">
 25             <td align="right" width="100px"  class="inputLabel">学生姓名: </td>
 26             <td align="left" width="100px">
 27 <%--                方法一 使用if-else实现输出和不输出--%>
 28 <%--            <%--%>
 29 <%--                if(request.getAttribute("studentName") != null){--%>
 30 <%--            %>--%>
 31 <%--                <input class="inputFrame" type="text" id="studentName" name="studentName" value="<%=request.getAttribute("studentName")%>"></td>--%>
 32 <%--                <td align="right" width="100px" class="inputLabel">帮背者姓名:</td>--%>
 33 <%--                <td align="left" width="100px"><input class="inputFrame" type="text" id="pairname" name="pairname" value="<%=request.getAttribute("pairname")%>"></td>--%>
 34 <%--                <td align="right" width="100px" class="inputLabel">父母姓名 :</td>--%>
 35 <%--                <td align="left" width="100px"><input class="inputFrame" type="text" id="parentname" name="parentname" value="<%=request.getAttribute("parentname")%>"></td>--%>
 36 
 37 <%--            <%--%>
 38 <%--                }else{--%>
 39 <%--            %>--%>
 40 <%--            <input class="inputFrame" type="text" id="studentName" name="studentName" value=""></td>--%>
 41 <%--            <td align="right" width="100px" class="inputLabel">帮背者姓名:</td>--%>
 42 <%--            <td align="left" width="100px"><input class="inputFrame" type="text" id="pairname" name="pairname" value=""></td>--%>
 43 <%--            <td align="right" width="100px" class="inputLabel">父母姓名 :</td>--%>
 44 <%--            <td align="left" width="100px"><input class="inputFrame" type="text" id="parentname" name="parentname" value=""></td>--%>
 45 
 46 <%--            <%--%>
 47 <%--                }--%>
 48 <%--            %>--%>
 49 <%--            方法二,使用el,自动回填--%>
 50             <input class="inputFrame" type="text" id="studentName" name="studentName" value="${studentName}"></td>
 51             <td align="right" width="100px" class="inputLabel">帮背者姓名:</td>
 52             <td align="left" width="100px"><input class="inputFrame" type="text" id="pairname" name="pairname" value="${pairname}"></td>
 53             <td align="right" width="100px" class="inputLabel">父母姓名 :</td>
 54             <td align="left" width="100px"><input class="inputFrame" type="text" id="parentname" name="parentname" value="${parentname}"></td>
 55 
 56 
 57         </tr>
 58         <tr align="left">
 59             <td align="right" width="100px" class="inputLabel">密码:</td>
 60             <td align="left" width="100px"><input class="inputFrame" type="password" id="password" name="password" > </td>
 61             <td align="right" width="100px" class="input.label">确认密码:</td>
 62             <td><input class="inputFrame" type="password" id="password2" name="password2">
 63             <td>
 64             <%
 65                 String pwMsg = (String)session.getAttribute("wrongMsgPw");
 66                 if(pwMsg != null)
 67                     out.println("<h7>" + pwMsg + "</h7>");
 68                 session.setAttribute("wrongMsgPw",null);
 69             %>
 70         </td>
 71             <td></td>
 72         </tr>
 73         <tr>
 74             <td colspan="4" class="alert">
 75                 <div id="checkUser">     </div> </td>
 76         </tr>
 77         <tr height="5px">
 78             <td colspan="6">请选择需要背的单词(后期可更改):
 79                 <%
 80                 String typeMsg = (String)session.getAttribute("wrongMsgType");
 81                 if(typeMsg != null)
 82                     out.println("<h7>" + typeMsg + "</h7>");
 83                 session.setAttribute("wrongMsgType",null);
 84                 %>
 85             </td>
 86         </tr>
 87 
 88         <tr><td align="center"><input type="radio" id="catagloryID1" name="catagloryID" value="6"></td>
 89             <td align="center" colspan="3">GRE</td>
 90         </tr>
 91 
 92         <tr><td align="center"><input type="radio" id="catagloryID1" name="catagloryID" value="2"></td>
 93             <td align="center" colspan="3">TOEFL</td>
 94         </tr>
 95 
 96         <tr><td align="center"><input type="radio" id="catagloryID1" name="catagloryID" value="7"></td>
 97             <td align="center" colspan="3">SAT</td>
 98         </tr>
 99 
100         <tr><td align="center"><input type="radio" id="catagloryID1" name="catagloryID" value="5"></td>
101             <td align="center" colspan="3">高中英语 </td>
102         </tr>
103 
104         <tr><td align="center"><input type="radio" id="catagloryID1" name="catagloryID" value="1"></td>
105             <td align="center" colspan="3">初中英语 </td>
106         </tr>
107 
108 
109         <tr  height="5px"></tr>
110         <tr><td colspan="4" align="center"><input class="button gray" type="submit" value="Submit" ></td>    </tr>
111     </table>
112 
113 </form>
114 <p class="tips">&nbsp;</p>
115 </body>
116 </html>
原文地址:https://www.cnblogs.com/Comet-Fei/p/11766627.html