在我的 servlet url 模式上,该应用程序适用于"/path",但不适用于"/path/to"



我有以下Java servlet:

package com.controller;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Teste extends HttpServlet {
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        request.setAttribute("teste", "Test");
        request.getSession().setAttribute("teste", "Test Session");
        RequestDispatcher rd = request.getRequestDispatcher("teste.jsp");
        rd.forward(request, response);
    }
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }
    @Override
    public String getServletInfo() {
        return "Short description";
    }
}

和以下web.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
    <servlet>
        <servlet-name>Teste</servlet-name>
        <servlet-class>com.controller.Teste</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>Teste</servlet-name>
        <url-pattern>/teste</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
</web-app>

我可以通过url http://localhost:8084/myapp/teste

访问servlet

我想要的是将url模式更改为/teste/edit,但当我这样做时,并尝试通过url http://localhost:8084/myapp/teste/edit访问servlet,我得到以下404错误:

HTTP状态404 -/TrabalhoPSW/teste/test .jsp

类型状态报告

消息/TrabalhoPSW/证人/teste.jsp

description请求的资源不可用。ApacheTomcat/8.0.27

为什么会发生这种情况?我该如何解决这个问题?

问题是servlet试图查找的JSP文件。当我从

request.getRequestDispatcher("teste.jsp"); 

request.getRequestDispatcher("/teste.jsp");

运行正常

如果您需要servlet与/path/path/to一起工作,请将web.xml中的servlet映射更改为:

<url-pattern>/teste/*</url-pattern>

相关内容

最新更新