2016-11-04 24 views
-1

我正在嘗試創建簡單的登錄頁面。這裏是我的代碼:使用Servlet和Mysql進行簡單身份驗證

的Servlet:

import java.io.IOException; 
    import java.io.PrintWriter; 
    import java.sql.Connection; 
    import java.sql.DriverManager; 
    import java.sql.PreparedStatement; 
    import java.sql.ResultSet; 

    import javax.servlet.ServletException; 
    import javax.servlet.annotation.WebServlet; 
    import javax.servlet.http.HttpServlet; 
    import javax.servlet.http.HttpServletRequest; 
    import javax.servlet.http.HttpServletResponse; 

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

     //do post method calling 
     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
      response.setContentType("text/html"); 
      PrintWriter out = response.getWriter(); 
      String user = request.getParameter("user"); 
      String pass = request.getParameter("pass"); 
      try { 
       Class.forName("com.mysql.jdbc.Driver"); 
       Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/javademo", "root", "admin"); 
       //prepared statement for calling query 
       PreparedStatement pst = conn.prepareStatement("Select user,pass from login where user=? and pass=?"); 
       pst.setString(1, user); 
       pst.setString(2, pass); 
       ResultSet rs = pst.executeQuery(); 
       if (rs.next()) { 
        out.println("Correct login credentials"); 
       } 
       else { 
        out.println("Incorrect login credentials"); 
       } 
      } catch (Exception e) { 

       e.printStackTrace(); 
      } 
      finally{} 

     } 
     } 

的index.html:

<html> 
    <head> 
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> 
    <title>Insert title here</title> 
    </head> 
    <body> 
     <form method="post" action="MySQLConnect">   
      UserName :<input type="text" name="user" /><br/><br/> 
      Password :<input type="password" name="pass" /><br/><br/> 
      <input type="submit" value="Login" /> 
     </form> 
    </body> 
    </html> 

的web.xml

<display-name>AuthenticationUsingMySQL</display-name> 

     <servlet> 
     <servlet-name>MySQLConnect</servlet-name> 
     <servlet-class>MySQLConnect</servlet-class> 
     </servlet> 
    <servlet-mapping> 
     <servlet-name>MySQLConnect</servlet-name> 
     <url-pattern>/Login</url-pattern> 
    </servlet-mapping> 
    <session-config> 
     <session-timeout>30</session-timeout> 
    </session-config> 

我想用

http://localhost:8080/AuthenticationUsingMySQL/Login

打開的頁面卻是露出HTTP Status 405 - HTTP method GET is not supported這個URL

+0

您同時擁有基於註釋的servlet配置@WebServlet和web.xml,爲什麼? – rkosegi

+0

請爲登錄頁面調用http:// localhost:8080/AuthenticationUsingMySQL/index.html。這應該顯示登錄表單。通過帖子提交表單應該調用你的servlet。也許你需要修復/檢查一些網址。 –

回答

0

不能直接使用TE網址在瀏覽器用作webservlet支持POST請求,所以嘗試僅從JSP發佈請求或使用諸如POSTMAN,SOAPUI之類的工具。

0

如果您試圖在瀏覽器中打開該鏈接,那麼該錯誤是有道理的。你需要使用像Fiddler這樣的工具,或者設置一個javascript測試方法來測試你的代碼。它看起來像你在你的代碼中構建一個POST方法。 Web瀏覽器默認使用GET來訪問Web資源,因此需要一個能夠執行POST的不同工具。否則,您將需要修改您的代碼,以便它接受GET請求。