mercredi 10 août 2016

On my servlet url-pattern, the application works with "/path" but not with "/path/to"

I have the following 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", "MothaFucka");
        request.getSession().setAttribute("teste", "MothaFucking 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";
    }

}

And the following web.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://ift.tt/19L2NlC" xmlns:xsi="http://ift.tt/ra1lAU" xsi:schemaLocation="http://ift.tt/19L2NlC http://ift.tt/1drxgYl">
    <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>

I can access the servlet through the url http://localhost:8084/myapp/teste

What I want is change the url-pattern to /teste/edit, but when I do that, and try to access the servlet through the url http://localhost:8084/myapp/teste/edit I get the following 404 error:

HTTP Status 404 - /TrabalhoPSW/teste/teste.jsp

type Status report

message /TrabalhoPSW/teste/teste.jsp

description The requested resource is not available. Apache Tomcat/8.0.27

Why is this happening? How can I fix this?




Aucun commentaire:

Enregistrer un commentaire