我正在使用eclipse中的servlet开发一个Login应用程序。 我创建了一个登录表单,其中包含两个文本字段(用于输入用户名和密码)和一个登录按钮。当用户单击此按钮时,它会重定向到LoginCheck页面,并在其中检查登录详细信息。但问题是,当我运行此项目时,我收到错误:HTTP状态405 - 此URL不支持HTTP方法POST。我做的问题是什么?
以下是LoginPage
的代码:
public class LoginPage extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<form method='post' actin='LoginCheck'>");
out.println("Username: <input type='text' name='username'>");
out.println("Password: <input type='password' name='password'>");
out.println("<input type='submit' value='login'>");
out.println("</form>");
}
}
现在,当用户点击“登录”按钮时,必须转到此页面的LoginCheck
页面代码,如下所示:
public class LoginCheck extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<h3>Checking login details...</h3>");
}
}
在这里它给出了问题。
这是XML文件:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>LoginApp</display-name>
<servlet>
<servlet-name>LoginPage</servlet-name>
<servlet-class>LoginPage</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LoginPage</servlet-name>
<url-pattern>/LoginPage</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>LoginCheck</servlet-name>
<servlet-class>LoginCheck</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LoginCheck</servlet-name>
<url-pattern>/LoginCheck</url-pattern>
</servlet-mapping>
</web-app>
答案 0 :(得分:0)
看起来你实际上并没有重定向到LoginCheck
servlet。尝试使用request.getContextPath
代替您拥有的内容。这是完整的代码:
public class LoginPage extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String contextPath = request.getContextPath();
out.println("<form method='post' action='/" + contextPath + "/LoginCheck'>");
out.println("Username: <input type='text' name='username'>");
out.println("Password: <input type='password' name='password'>");
out.println("<input type='submit' value='login'>");
out.println("</form>");
}
}
答案 1 :(得分:0)
如果方法“get”重定向到“post”,则您的GET请求将转换为POST,从而丢失所有获取数据。所以我的建议是,从doGet方法调用doPost方法,如...
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
PrintWriter out = resp.getWriter();
out.println("<html><form method='post' action='LoginCheck'>");
out.println("Username: <input type='text' name='username'>");
out.println("Password: <input type='password' name='password'>");
out.println("<input type='submit' value='login'>");
out.println("</form></html>");
}
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
doPost(req, resp);
}