java - JSP在JavaEE和tomcat 9中无法指向Servlet类中的Post方法

标签 java jsp tomcat servlets

我在 jsp 中有以下表单,其中包含一个文本字段和一个按钮

<form action="/login.do" method="Post">
Enter 
<br/><br/>
Your Name <input type="text" name="name"/>
<br/><br/>
Password <input type="text" name="password"/>
<br/><br/>
<select name="Gender">
<option value="Male">Male</option>
<option value="Female">Female</option>
</select>
<br/><br/>
<input type="submit" value="Login"/>

我的小服务程序:

@WebServlet(urlPatterns = "/login.do")
public class LoginServlet extends HttpServlet{
protected void doGet(HttpServletRequest request, javax.servlet.http.HttpServletResponse response) 
        throws ServletException, IOException{
    request.getRequestDispatcher("/WEB-INF/view/login.jsp").forward(request, response);


}

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    String name =request.getParameter("name");
    String password = request.getParameter("password");
    String gender = ((String)request.getParameter("Gender")=="Female")? "Ms.":"Mr.";

    if(new ValidateUser().validate(name, password)){
    request.setAttribute("name", name);
    request.setAttribute("password", password);
    request.setAttribute("gender", gender);
    request.getRequestDispatcher("/WEB-INF/view/welcome.jsp").forward(request, response);;
    } else {
    request.setAttribute("errorMes", "Login Failed");
    request.getRequestDispatcher("/WEB-INF/view/login.jsp").forward(request, response);;    
    }
}

根据教程,当我在jsp页面中点击按钮时,会触发Servlet类中的doPost()方法。

但是我一直收到 HTTP 状态 404 -/login.do 错误

请帮忙! ...

我的 web.xml

<?xml version="1.0" encoding="UTF-8"?>
<!-- webapp/WEB-INF/web.xml -->
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">

  <display-name>To do List</display-name>
  <welcome-file-list>
    <welcome-file>login.do</welcome-file>
  </welcome-file-list>

当我运行项目时,我连接到这个 URL:http://localhost:8081/in28Minutes-first-webapp/

当我点击按钮时,我连接到这个 url:http://localhost:8081/login.do并给出“HTTP Status 404 -/login.do”错误

我试过“${pageContext.request.contextPath}/login.do”,还是报错

我有这个 pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.in28minutes</groupId>
    <artifactId>in28Minutes-first-webapp</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>

<dependencies>
    <dependency>
        <groupId>javax</groupId>
        <artifactId>javaee-web-api</artifactId>
        <version>6.0</version>
        <scope>provided</scope>
    </dependency>
</dependencies>

<build>
    <pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.2</version>
                <configuration>
                    <verbose>true</verbose>
                    <source>1.7</source>
                    <target>1.7</target>
                    <showWarnings>true</showWarnings>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.tomcat.maven</groupId>
                <artifactId>tomcat7-maven-plugin</artifactId>
                <version>2.2</version>
                <configuration>
                    <path>/</path>
                    <contextReloadable>true</contextReloadable>
                </configuration>
            </plugin>
        </plugins>
    </pluginManagement>
</build>

最佳答案

我的建议是:

您应该在表单标签的操作参数中设置您在页面 localhost:8081/in28Minutes-first-webapp/login.do 上的位置,而不是 localhost:8081/login.do。尝试设置以下设置之一:action="login.do"action="localhost:8081/in28Minutes-first-webapp/login.do" action="/in28Minutes-first-webapp/login.do".

I have tried "${pageContext.request.contextPath}/login.do", it still gives the error

你给定的错误如何? ${pageContext.request.contextPath} 的值如何?

关于java - JSP在JavaEE和tomcat 9中无法指向Servlet类中的Post方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40317530/

相关文章:

java - 使用 cssSelector 或 xPath Selenium Web 驱动程序在过滤器上应用按钮?

java - 在Java Spring 4中从应用程序A到应用程序B进行rest或soap api调用时,将用户名存储在数据库中

java - HTTP 状态 404 : The requested resource is not available

javascript - 在 jsp 中选择 <option> 从数据库中获取值

java - 子资源在 Jersey REST API 框架中不起作用

java - 在 pom.xml 中配置进程类型时 Heroku Webapp 崩溃

java - 应用引擎 : REST with Jersey not working

java - 了解元素在 Java Swing 中的使用

java - 如何使用 JSP 使用 Spring MVC 显示图像

java - Tomcat DataSource getConnection 工作但在日志中抛出异常