PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, July 13, 2022

[FIXED] Why doesn`t url change when running servlet that presents in welcome-file-list

 July 13, 2022     java, servlets, tomcat, web-deployment, welcome-file     No comments   

Issue

I have simple servlet that prints something to response

@WebServlet(name = "helloServlet", value = "/hello-servlet" , s )
public class HelloServlet extends HttpServlet {
    private String message;

    public void init() {
        message = "Hello World!";
    }

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setContentType("text/html");

        // Hello
        PrintWriter out = response.getWriter();
        out.println("<html><body>");
        out.println("<h1>" + message + "</h1>");
        out.println("</body></html>");
    }

    public void destroy() {
    }
}

that what web.xml looks like:

<?xml version="1.0" encoding="UTF-8"?>
<web-app 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_4_0.xsd"
         version="4.0">
    <welcome-file-list>
        <welcome-file>hello-servlet</welcome-file>
    </welcome-file-list>
</web-app>

and the problem is that url isn`t changed when it redirect to servlet in welcome-file list
my url : http://localhost:8080/testsss_war_exploded/
but should be : http://localhost:8080/testsss_war_exploded/hello-servlet


Solution

That is the way welcome resources are supposed to work:

The container may send the request to the welcome resource with a forward, a redirect, or a container specific mechanism that is indistinguishable from a direct request.

(Servlet 5.0 Specification)

Tomcat redirects the request internally. If you want to send a HTTP redirect, you need to do it yourself. You can check the original URI to see if the request was forwarded by the welcome files mechanism:

@WebServlet(name = "helloServlet", urlPatterns = {"/hello-servlet"})
public class HelloServlet extends HttpServlet {

   @Override
   protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
      if (req.getRequestURI().endsWith("/")) {
         resp.sendRedirect("hello-servlet");
         return;
      }
}

Another solution would be to renounce the the welcome files mechanism and explicitly bind your servlet to the applications root:

@WebServlet(name = "helloServlet", urlPatterns = {"", "/hello-servlet"})
public class HelloServlet extends HttpServlet {

   @Override
   protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
      if (req.getServletPath().isEmpty()) {
         resp.sendRedirect("hello-servlet");
         return;
      }
}


Answered By - Piotr P. Karwasz
Answer Checked By - Terry (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing