【问题标题】:Web application gives http method is not supported by this url error and also SQL error此 url 错误和 SQL 错误不支持 Web 应用程序提供的 http 方法
【发布时间】:2017-05-19 23:11:02
【问题描述】:

我正在创建一个 Web 应用程序,当我们访问它时,它会给出一个“此 url 不支持 http 方法;错误,也是一个 SQL 错误。

请告诉我问题出在哪里。有时它会给出一个 SQL 错误,现在它给出“此 url 不支持 http 方法发布”错误。

能否请您提供成功运行的方法?

index.jsp

 <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
  pageEncoding="ISO-8859-1"%>
  <!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=ISO-8859-1">
    <title>My Web Project</title>
    <link rel="stylesheet" type="text/css" href="css/style.css">
     <script type="text/javascript" src="js/script.js"></script>
     </head>
    <body>
    <table width="100%" height="15%">
    <tr>
    <td><h2>**My Web Project**</h2>
     <td width="200px"></td>
    <td>
    <form method="post" action="http://localhost:8080/webapp1/login">
     UserName: <input type="text" name="usern" class="textbox">
    Password: <input type="password" name="pass" class="textbox">
    <input type="submit" value="login" class="circle" >
    </form>.....

login.java

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.mysql.jdbc.PreparedStatement;

public class Login extends HttpServlet {

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            String username1 = request.getParameter("usern");
            String password1 = request.getParameter("pass");
            System.out.println(username1);

            Class.forName("com.mysql.jdbc.Driver");
            PrintWriter out = response.getWriter();
            Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/userlogin?useSSL=false", "root",
                    "root");
            System.out.println("hi");
            java.sql.PreparedStatement ps = con
                    .prepareStatement("select *from test where username=?");
            ps.setString(1, username1);


            ResultSet rs = ps.executeQuery();

            if (rs.next()) {
                String firstname = rs.getString("firstname");
                String bday = rs.getString("birth_day");
                String bmonth = rs.getString("birth_month");
                String byear = rs.getString("birth_year");
                String gender = rs.getString("gender");

                response.sendRedirect("http://localhost:8080/webapp1/Userhome.jsp");

            } else {
                response.sendRedirect("http://localhost:8080/webapp1/index.jsp?result=Failure");
            }

        } catch (Exception e) {
            System.out.println("we got some error");
            e.printStackTrace();
            response.sendRedirect("SessionExpire.jsp");
        }
    }
}

【问题讨论】:

  • 警告:不要将密码存储为纯文本。 始终使用密码级哈希系统,例如 BCryptScrypt

标签: java mysql jsp servlets


【解决方案1】:

您已将doPost 声明为

protected void doPost(HttpServletRequest request, 
                      HttpServletResponse  response, String geek) 
        throws ServletException, IOException 

问题在于您添加的“geek”参数。这会导致您的方法签名与标准 doPost 方法签名不匹配。这意味着您重载 doPost 而不是覆盖它。

结果是 servlet 根本不会调用您的 doPost。相反,它调用您应该重写的方法......其行为是说“不支持 POST”。

解决方法:去掉伪造的geek参数

解决方案2:在方法中添加@Override注解。如果方法不是覆盖,这将导致编译器告诉您

【讨论】:

  • 那么你检查过你的doPost方法被调用了吗?
  • 建议:阅读e.printStackTrace() 将写入的日志文件并查找堆栈跟踪。
猜你喜欢
  • 1970-01-01
  • 2015-03-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-02-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多