在我的servlet url-pattern上,应用程序使用“/ path”但不使用“/ path / to”

问题描述 投票:0回答:2

我有以下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-pattern更改为/teste/edit,但是当我这样做,并尝试通过url http://localhost:8084/myapp/teste/edit访问servlet时,我收到以下404错误:

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

类型状态报告

消息/TrabalhoPSW/teste/teste.jsp

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

为什么会这样?我怎样才能解决这个问题?

java jsp servlets web
2个回答
0
投票

问题是servlet试图找到的JSP文件。当我改变了

request.getRequestDispatcher("teste.jsp"); 

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

它运作正常。


0
投票

如果你需要你的servlet使用/ path和/ path / to,请将web.xml中servlet的映射更改为:

<url-pattern>/teste/*</url-pattern>
© www.soinside.com 2019 - 2024. All rights reserved.