2017-05-28 51 views
1

我已經創建了一個jsp登錄頁面表單,並且將我的邏輯放在了doPost()中。但似乎它不工作。 即使在控制檯中未顯示doPost()方法的System.out.println語句結果。 JSP頁面::(login.jsp的)servlet類的doPost方法沒有給出所需的結果

<body> 
    <form method="post" action="LogIn"> 
     <table> 
      <tr> 
       <td colspan=2 align="center" 
        style="font-weight: bold; font-size: 20pt;" align="center"><b>User 
         Login Form</b></td> 
      </tr> 
      <tr> 
       <td colspan=2></td> 
      </tr> 
      <tr> 
       <td>User Name:</td> 
       <td><input type="text" id="txtUserName" /></td> 
      </tr> 

      <tr> 
       <td>Password:</td> 
       <td><input type="password" id="txtpassword" /></td> 
      </tr> 
      <tr> 
       <td></td> 
       <td><input type="button" id="btnSubmit" value="Submit" /></td> 
      </tr> 
     </table> 
    </form> 

</body> 

Servlet類(LogIn.java)

@WebServlet("/LogIn") 
public class LogIn extends HttpServlet { 
    private static final long serialVersionUID = 1L; 

    public LogIn() { 
     super(); 
     // TODO Auto-generated constructor stub 
    } 
    protected void doGet(HttpServletRequest request, HttpServletResponse response) 
      throws ServletException, IOException { 
     // TODO Auto-generated method stub 
     response.getWriter().append("Served at: ").append(request.getContextPath()); 

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

     String UserName = request.getParameter("txtUserName"); 
     String Password = request.getParameter("txtpassword"); 

     System.out.println(UserName); 
     System.out.println(Password); 

     if (UserName == "Servlet" && Password == "admin") {   
      response.sendRedirect("Home.jsp"); 

     Enumeration paramNames = request.getParameterNames(); 

     while (paramNames.hasMoreElements()) { 
      String paramName = (String) paramNames.nextElement();   
      String[] paramValues = request.getParameterValues(paramName); 

      // Read single valued data 
      if (paramValues.length == 1) { 
       String paramValue = paramValues[0]; 
       if (paramValue.length() == 0) 
       { 
        System.out.println("no values"); 
       } 
       else 
        System.out.println(paramValue); 
      } else { 
       // Read multiple valued data 
       System.out.println("....."); 

       for (int i = 0; i < paramValues.length; i++) { 
        System.out.println(paramValues[i]); 
       } 
      } 
     } 
    } 
} 

web.xml文件

<servlet> 
<servlet-name>LogIn</servlet-name> 
<servlet-class>LogIn</servlet-class>  
</servlet> 
<servlet-mapping> 
<servlet-name>LogIn</servlet-name> 
<url-pattern>/LogIn</url-pattern> 
</servlet-mapping> 
+1

你爲什麼從doPost調用doGet? –

+0

@MichaelMarkidis對不起,我不理解你的評論。請你解釋一下。 – user4221591

+0

您的servlet中的'doPost'方法的第一行是'doGet(request,response);'爲什麼要從post方法調用get方法? –

回答

0

對Servlet API的註解的支持是唯一在Servlet 3.0(Tomcat 7)中添加。 嘗試在web.xml中第一行來定義它:

欲瞭解更多信息請參閱

<web-app 
    xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" 
    version="3.0"> 

@WebServlet annotation with Tomcat 7

1
request.getParameter("txtUserName"); 

輸入標籤搜索的NAME = 「txtUserName」屬性和密碼字段一樣,不存在,請修改JSP代碼,如下所示:

<body> 
    <form method="post" action="LogIn"> 
     <table> 
      <tr> 
       <td colspan=2 align="center" 
        style="font-weight: bold; font-size: 20pt;" align="center"><b>User 
         Login Form</b></td> 
      </tr> 
      <tr> 
       <td colspan=2></td> 
      </tr> 
      <tr> 
       <td>User Name:</td> 
       <td><input type="text" id="txtUserName" name="txtUserName"/></td> 
      </tr> 

      <tr> 
       <td>Password:</td> 
       <td><input type="password" id="txtpassword" name="txtpassword" /></td> 
      </tr> 
      <tr> 
       <td></td> 
       <td><input type="button" id="btnSubmit" value="Submit" /></td> 
      </tr> 
     </table> 
    </form> 

</body>