0

它说:此 URL 不支持 HTTP 方法 POST。

请求的资源不允许指定的 HTTP 方法。

这是我的 servlet 代码:

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.RequestDispatcher;
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 java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class UserController extends HttpServlet {
    private static final long serialVersionUID = 1L;
    private static final int Id = 0;
    private static final String Password = null;
    private static final String Username = null;
    private static final int Userid = 0;
    private static String INSERT_OR_EDIT = "/user.jsp";
    private static String LIST_USER = "/listUser.jsp";
    public static String PARAM_USERNAME = "uname";
    public static String PARAM_PASSWORD = "pass";
    private UserDao dao;
    private Connection connection;


    public UserController() {
        super();
        dao = new UserDao();
    }


    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
         String forward="";
         String act = request.getParameter("act");
         if (act != null && !act.equalsIgnoreCase("null") && act.equalsIgnoreCase("login")) {
            forward= "/Login.jsp";
            }

         else if (act != null && !act.equalsIgnoreCase("null") && act.equalsIgnoreCase("delete"))
        {
            int userId = Integer.parseInt(request.getParameter("userId"));
            dao.deleteUser(userId);
            forward = LIST_USER;
            request.setAttribute("users", dao.getAllUsers());    
        } else if (act!=null && !act.equalsIgnoreCase("null") && act.equalsIgnoreCase("edit")){
            forward = INSERT_OR_EDIT;
            int userId = Integer.parseInt(request.getParameter("userId"));
            User user1 = dao.getUserById(userId);
            request.setAttribute("user", user1);
        } else if (act!=null && !act.equalsIgnoreCase("null") && act.equalsIgnoreCase("listUser")){
            forward = LIST_USER;
            request.setAttribute("users", dao.getAllUsers());
        } else if (act!=null && !act.equalsIgnoreCase("null") && act.equalsIgnoreCase("register")){
            forward = "/reg.jsp";
           // request.setAttribute("users", dao.getAllUsers());
        }else if (act!=null && !act.equalsIgnoreCase("null") && act.equalsIgnoreCase("update")){
            forward = "/welcome.jsp";
        } else 
            forward = "/Login.jsp";

        RequestDispatcher view = request.getRequestDispatcher(forward);
        view.forward(request, response);
        } 


    protected void doPost(HttpServletRequest request, HttpServletResponse response, Details details) throws ServletException, IOException {

        response.setContentType("text/html");
        PrintWriter out=response.getWriter();

        String act = request.getParameter("act");
        if (act != null && !act.equalsIgnoreCase("null") && act.equalsIgnoreCase("login")) {

            String str=request.getParameter("username");
            String str1=request.getParameter("password");


            if(str.equalsIgnoreCase("shreya")&&str1.equalsIgnoreCase("singh"))
            {        
                System.out.println("Login!");
                   request.setAttribute("users",dao.getAllUsers());
                    request.getRequestDispatcher("/listUser.jsp").forward(request, response); 

            }else
             {
                  System.out.println("Login failed!");

              }
        }







        if (act != null && !act.equalsIgnoreCase("null") && act.equalsIgnoreCase("register"))
        {

            String userid1=request.getParameter("userid");
            String username=request.getParameter("username");
            String password=request.getParameter("password");
            String id=request.getParameter("id");


            if(userid1.equals("") || username.equals("") || password.equals("") || id.equals(""))
            { 
                out.println("Please insert valid data");
            }
            RequestDispatcher rd = request.getRequestDispatcher("/listUser.jsp");
            rd.include(request, response); 
            }

        else
        {
            RequestDispatcher rd = request.getRequestDispatcher("/listUser.jsp");
            rd.include(request, response); 

        try {
            PreparedStatement preparedStatement = connection.
                    prepareStatement("select * from details where id=?");
            preparedStatement.setInt(1, Id);
            ResultSet rs = preparedStatement.executeQuery();

            if (rs.next()) {
                details.setUserid(rs.getInt("userid"));
                details.setUsername(rs.getString("username"));
                details.setPassword(rs.getString("password"));
                details.setId(rs.getInt("id"));
            }


        PreparedStatement preparedStatement1 = connection.
                prepareStatement( "insert into details values(?,?,?,?)");
        preparedStatement1.setInt(1, Userid);
        preparedStatement1.setString(2, Username);
        preparedStatement1.setString(3, Password);
        preparedStatement1.setInt(4, Id);

        int i=preparedStatement1.executeUpdate(); 
        if(i>0)
        {
            System.out.println("Data updated sucessfully");
            System.out.print("Student record successfully inserted");
            RequestDispatcher rd1 = request.getRequestDispatcher("/registration.jsp");
            rd1.include(request, response);
        }
        }
        catch (Exception e)
        {
            System.out.println(e);
        }
        if (act != null && !act.equalsIgnoreCase("null") && act.equalsIgnoreCase("update"))
        {
            String userid=request.getParameter("userid");
            String username=request.getParameter("username");
            String password=request.getParameter("password");
            String id=request.getParameter("id");
            try
            {
             Statement stmt = connection.createStatement();
             String sql = "UPDATE details SET  username='hi',password='hello' where id=1";
             stmt.executeUpdate(sql);
             dao.saveData(details, sql);
             System.out.println("Data update sucessfully");
                     } 
                            catch (SQLException e) {
                                e.printStackTrace();
                            }
                        catch (Exception e) {
                                e.printStackTrace();
                            }
                            finally{
                                try {
                                    connection.close();
                                } catch (SQLException e) {
                                    e.printStackTrace();
                                }
                             }

                          }
                         } 
                    }
                } 

早些时候它工作正常。我猜post方法有问题

4

2 回答 2

0

HttpServlet doPost(HttpServletRequest req, HttpServletResponse resp)方法是

protected void doPost(HttpServletRequest req, HttpServletResponse resp)
    throws ServletException, IOException
{
    String protocol = req.getProtocol();
    String msg = lStrings.getString("http.method_post_not_supported");
    if (protocol.endsWith("1.1")) {
        resp.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED, msg);
    } else {
        resp.sendError(HttpServletResponse.SC_BAD_REQUEST, msg);
    }
}

立即响应METHOD_NOT_ALLOWED。您尚未覆盖此方法。您已重载它以添加Details参数。因此它没有被调用

protected void doPost(HttpServletRequest request, HttpServletResponse response, Details details) throws ServletException, IOException {

你期望这些Details来自哪里?如果有的话,请覆盖该doPost方法并使其调用您的方法。

于 2013-09-11T15:00:43.747 回答
0

改变

protected void doPost(HttpServletRequest request, HttpServletResponse response, 
      Details details) throws ServletException, IOException

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

邮政

于 2013-09-11T15:02:51.293 回答