0

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

Community
  • 1
  • 1
Pratik Patel
  • 1,305
  • 1
  • 17
  • 44
  • whats the request URL that you are using to invoke your servlet? – Debojit Saikia Dec 22 '15 at 09:58
  • http://localhost:8080/salsadb// – Pratik Patel Dec 22 '15 at 10:03
  • 1
    Your URL pattern is wrong. It catches ALL requests, including the ones you're redirecting to. Basically, servlet is invoking itself again, but no one condition matches and it does nothing with the response, so all you get is blank response. For the right URL pattern see the answer in duplicate. For an explanation of how those URL patterns work, see the "See also" link in bottom of answer in duplicate. – BalusC Dec 22 '15 at 12:50
  • @BalusC Thanks! This was the problem. I can accept it as correct answer, if you add one.I did not know that you must either return a response or redirect from a servlet. I was under the impression that request would follow its own course if we don't do anything in the servlet. – Pratik Patel Dec 22 '15 at 13:34

3 Answers3

1

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.

Debojit Saikia
  • 10,532
  • 3
  • 35
  • 46
1

I think your url-pattern tag is the problem please change it to something other than /*.

laksys
  • 3,228
  • 4
  • 27
  • 38
0

I didn't understand why you are trying to redirect to a image? However if you still want to see the image then add the salsa-icon.png in a HTML/jsp page and redirect to it.

Ketu
  • 1,608
  • 2
  • 14
  • 30
  • As I stated, I am redirecting to image only to simplify code for debugging. I want to redirect to an HTML page but behavior is same for an HTML page as well. I first tried with a simple minimal HTML page but instead of that a blank page was being rendered, just the way I have described. As such redirecting to an image or HTML page should be no different right? so I replaced the page by image in order to eliminate possibility of any bug in HTML code. – Pratik Patel Dec 22 '15 at 09:55
  • Can you post your HTML code here? Also path of the image file is it correct? just try to append that path in front of your context path in browser and you will find out. http://:port/images/salsa-icon.png. See if you are able to see the image file.. – Ketu Dec 22 '15 at 09:59
  • Yes the path is correct. I can see image/page if no servlet is applied. The problem occurs only after I apply servlet. – Pratik Patel Dec 22 '15 at 10:10