【问题标题】:Simple authentication using Servlet and Mysql使用 Servlet 和 Mysql 的简单身份验证
【发布时间】:2016-11-04 16:43:55
【问题描述】:

我正在尝试创建简单的登录页面。这是我的代码:

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

【问题讨论】:

  • 你同时拥有基于注解的 servlet 配置 @WebServlet 和 web.xml,为什么?
  • 请致电localhost:8080/AuthenticationUsingMySQL/index.html获取登录页面。这应该显示登录表单。通过 post 提交表单应该调用您的 servlet。也许你需要修复/检查一些网址。

标签: java xml servlets


【解决方案1】:

当您在浏览器上转到http://localhost:8080/AuthenticationUsingMySQL/Login 时,您会向不提供 GET 方法的 servlet 发送 GET 请求。 只需转到http://localhost:8080/AuthenticationUsingMySQL/ 即可。

【讨论】:

    【解决方案2】:

    如果您尝试在浏览器中打开该链接,则该错误是有道理的。你需要使用像 Fiddler 这样的工具,或者设置一个 javascript 测试方法来测试你的代码。看起来您正在代码中构造一个 POST 方法。默认情况下,Web 浏览器使用 GET 来访问 Web 资源,因此需要能够执行 POST 的不同工具。否则,您将需要修改您的代码,以便它接受 GET 请求。

    【讨论】:

      【解决方案3】:

      由于webservlet支持POST请求,所以不能直接在浏览器中使用te URL,所以尽量只从JSP POST请求,或者使用POSTMAN、SOAPUI等工具。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-10-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-07-14
        • 1970-01-01
        • 2016-02-29
        相关资源
        最近更新 更多