它说:此 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方法有问题