Search code examples
javahtmlservletsweb.xmlservlet-filters

Java : Redirect from Servlet leads to a blank page


I am trying to redirect to an HTML page from a servlet. For debugging, I have minimal amount of code in my servlet, HTML page and in web.xml

I can load HTML page fine in browser without the servlet. But when I try to redirect to the same page from a servlet, only a blank page is rendered. No error is displayed. Following is the relevant code.

web.xml

    <?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    id="WebApp_ID" version="2.5">
    <display-name>name</display-name>

    <listener>
        <listener-class>net.semandex.salsa.webapp.SalsaWebApp</listener-class>
    </listener>

    <servlet>
        <description>
        </description>
        <display-name>SalsaValidationServlet</display-name>
        <servlet-name>SalsaValidationServlet</servlet-name>
        <servlet-class>net.semandex.salsa.validationServlets.SalsaValidationServlet</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>SalsaValidationServlet</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>

    <session-config>
        <session-timeout>20</session-timeout>
    </session-config>

</web-app>

Servlet

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class SalsaValidationServlet extends HttpServlet {

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String path = request.getPathInfo();
        if(path == null) return;
        String []p = path.split("/");
        if( !path.endsWith("licenseValidation.html") )
            //request.getRequestDispatcher("/auth/licenseValidation.html").forward(request, response);
            response.sendRedirect( request.getContextPath() + "/auth/licenseValidation.html" );
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        doGet(req, resp);
    }
}

HTML Page - licenseValidation.html

<html>
<head>
<title>License upload page</title>
</head>
<body>

<form>
        <input type="text" name="name"/><br>        
        <input type="text" name="group"/>
        <input type="text" name="pass"/>
        <input type="submit" value="submit">            
</form>

</body>
</html> 

Why is this code loading a blank page and not an HTML page? Redirection does happen but to a blank page. Status code for the new URL in browser is 200 but the response is empty in debugger.

Edit:

The problem was "/*" URL pattern as stated by BalusC. Found more useful information in his another answer. Difference between / and /* in servlet mapping url pattern


Solution

  • The request URL that you are using doesn't have any extra path information associated. So the request.getPathInfo() is returning null and the redirection is not getting triggered.

    If you want to redirect to any page, even if there are no extra path info, you need to remove the null check. Just redirect it :

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //String path = request.getPathInfo();
        //if(path == null) return;
        //String []p = path.split("/");
        //if( !path.endsWith("licenseValidation.html") )
        //request.getRequestDispatcher("/auth/licenseValidation.html").forward(request, response);
        response.sendRedirect( request.getContextPath() + "/auth/licenseValidation.html" );
    }
    

    request.getPathInfo returns null if there are no extra path info : getPathInfo

    Suppose you have defined the servlet-mapping as this:

    <servlet-mapping>
        <servlet-name>SalsaValidationServlet</servlet-name>
        <url-pattern>/salsadb/*</url-pattern>
    </servlet-mapping>
    

    And you are sending the request to this URL:

    http://localhost:8080/<appName>/salsadb/some_text
    

    This time pathInfo will not be null; it will have the value as /some_text, which is the extra path info associated with the URL.

    EDIT:

    The SalsaValidationServlet is mapped to serve all the requests to the application. The first request that is coming is this :

    http://localhost:8080/salsadb/
    

    The doGet method of SalsaValidationServlet gets invoked and the value of pathInfo is /. It redirects the request to /auth/licenseValidation.html.

    Now the browser will send a new request to the redirect URL, which in this case is : /auth/licenseValidation.html. Again the doGet method of SalsaValidationServlet is getting invoked, because it is mapped with this /* : execute for any request to the application. This time the value of pathInfo is /licenseValidation.html, which is not satisfying this if condition:

    if( !path.endsWith("licenseValidation.html") )
    

    and because of this the redirection is not getting triggered this time.

    I think this will clarify the problem. If possible, change the servlet-mapping to serve only the required requests to the application.