monster
monster

Reputation: 1782

JSP - Servlet not forwarding to JSP correctly

I have a Controller Servlet that has to forward a request to a specific JSP. However, whenever I forward the request it loads the JSP but the URL in the browser does something quite strange.

For example:

Via index.jsp I click: <a href="/login" />

In the Controller I have a method

if(request.getPathInfo().equals("/login"){
  dispatcher = this.getServletContext().getRequestDispatcher("/login.jsp");
  dispatcher.forward(request, response);
}

The URL in the browser will say:

http://mywebsite.com/my_work/do/login

and not:

http://mywebsite.com/my_work/login.jsp

The login.jsp page will be shown, just the URL is wrong. This causes problems, as I have to include 2 x References to Javascript and CSS files in each JSP as the relative URL changes depending on if I accessed the page manually by typing http://mywebsite.com/my_work/login.jsp into the browser, or whether it was accessed via a forward from the Controller servlet.

My web.xml:

    <?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
    <servlet>
        <servlet-name>Controller</servlet-name>
        <servlet-class>controller.Controller</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>Controller</servlet-name>
        <url-pattern>/do/*</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <resource-ref>
        <res-ref-name>jdbc/LessonDatabase</res-ref-name>
        <res-type>javax.sql.DataSource</res-type>
        <res-auth>Container</res-auth>
        <res-sharing-scope>Shareable</res-sharing-scope>
    </resource-ref>
</web-app>

Screenshot of the Servlet tab

Any help would be great. I've got everything to work, but I'm guessing that this is not correct!

It should be noted that this occurs for any forwarding.

Upvotes: 1

Views: 4307

Answers (3)

Suleman
Suleman

Reputation: 1

Just make sure the address for the URL pattern exactly matches the pattern. **These patterns are case sensitive. Took me hours to figure out a similar issue. **

Upvotes: 0

Alan Hay
Alan Hay

Reputation: 23226

Doing a redirect rather than forward (and dealing with the implications of that) is the completely wrong approach to fixing an issue with the way static resources are referenced.

Unless you understand the implications of doing a redirect then the best (and easiest) solution is to fix the way your static resources are referenced.

You can do this in a framework agnostic way as follows (although your framework may offer other means) by computing and setting the base path.

http://www.w3schools.com/tags/tag_base.asp

In the following, JS and CSS files will always be referenced relative to the root of the webapp regardless of the path of the current resource.

<%@page contentType="text/html" pageEncoding="UTF-8"%>

<!--
  To avoid scriptlets and use JSTL see: 
  http://stackoverflow.com/questions/6271075/how-to-get-the-base-url-from-jsp-request-object
-->
<%
    String path = request.getContextPath();
    String basePath = request.getScheme()
            + "://"
            + request.getServerName()
            + ":"
            + (request.getServerPort() != 80
                    ? request.getServerPort()
                    : "") + path + "/";
%>


<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>ABC</title>

<base href="<%=basePath%>" />

<link href="css/styles.css" rel="stylesheet"
    media="screen" />

<script src="js/jquery-1.8.3.js"></script>

</head>

Upvotes: 1

gprathour
gprathour

Reputation: 15333

It is because you are forwarding the request to your JSP.

if(request.getPathInfo().equals("/login"){
  dispatcher = this.getServletContext().getRequestDispatcher("/login.jsp");
  dispatcher.forward(request, response);
}

When you forward a request to the next JSP or Servlet it keeps on showing the URL pattern of the Servlet in browser's address bar from where you are forwarding.

If you want the URL to be changed, you should redirect it.

response.sendRedirect("YOUR_JSP");

Therefore, your code should be,

if(request.getPathInfo().equals("/login"){
  response.sendRedirect("/login.jsp");
}

NOTE

The difference between Forwarding a request (i.e. using RequestDispatcher) and redirecting the request (i.e. using sendRedirect) is that if you forward a request to next resource then your same request object is passed to the next resource, therefore all its attributes & parameters are sent to the next JSP or Servlet.

Instead if you redirect your request then the client (browser) is asked to send a new request to the next resource. Therefore all your previous parameters and attributes in the request are not accessible in the new request at next resource.

However the information (parameters & attributes) stored in session or context is not affected by redirecting the request.

Upvotes: 4

Related Questions