0

I have the following sturcture in my app

--Controller
  |_ServletA
  |_ServletB
--Webpages
  |_Secure
      |_PageA
      |_PageB
  |_PageC

Now the application starts with PageC which posts to ServletB.The ServletB forwards to PageA.

Now from PageA a link is clicked which points to ServletB . The servletB does some work and forwards to PageB.At this stage the address on the url is http://localhost:8080/MyApp-war/Secure/PageB.jsp.

Here is the problem now a link on PageB points back to ServletB. The link is

<a href="ServletB"> 

Therefore the browser points to http://localhost:8080/MyApp-war/Secure/ServletB which is wrong as it should be http://localhost:8080/MyApp-war/ServletB. How can I fix this problem without changing the link to ServletB from pageB, Since it works okay in first attempt but when the relative address changes it fails ?

EDIT :

In short what I want to know is what should i place in the link

<a href="ServletB"> 

so that if the relative address is http://localhost:8080/MyApp-war/Secure/ it points to http://localhost:8080/MyApp-war/ServletB instead of http://localhost:8080/MyApp-war/Secure/ServletB and if the relative address is http://localhost:8080/MyApp-war/ it takes it to the same location http://localhost:8080/MyApp-war/ServletB

Murphy316
  • 766
  • 3
  • 13
  • 29
  • I don't seem to understand your question? How are you mapping your servlet url, the url should be static. – Uchenna Nwanyanwu Aug 21 '12 at 15:49
  • I just added an edit. Let me know if that makes sense – Murphy316 Aug 21 '12 at 15:53
  • I know but the relative address is causing an issue initially it was being called from http:.../MyApp-war/ which works but then its being called from MyApp-war/Secure/ which then gives an error – Murphy316 Aug 21 '12 at 15:55
  • possible duplicate of [Browser can't access CSS and images when calling a Servlet which forwards to a JSP](http://stackoverflow.com/questions/3655316/browser-cant-access-css-and-images-when-calling-a-servlet-which-forwards-to-a-j) – BalusC Aug 21 '12 at 15:58

1 Answers1

1

What you need to do here is to add the context path of your web application to the link. You can do something like this;

This means, if your servlet definition in your web.xml is like this

<servlet>
    <servlet-name>ServletB</servlet-name>
    <servlet-class>com.myfullpackage.MyServletB</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>ServletB</servlet-name>
    <url-pattern>/ServletB</url-pattern>
</servlet-mapping>

You will do something like this

<a href='<%=request.getContextPath()%>/ServletB'>

or you use expression language form

<a href='${pageContext.request.contextPath}/ServletB'>

Let me know if this solves your issue.

Uchenna Nwanyanwu
  • 3,174
  • 3
  • 35
  • 59
  • Thanks that did the trick. Could you tell me what exactly is the difference b/w context path and relative path ? – Murphy316 Aug 21 '12 at 16:03
  • You are welcome. Relative path in my own understanding deals with the location of a resource based on another resource. For example, if the path of `file1` is `/opt/myfiles/file1` and `file2` is in `/opt/myfiles/web/file2`. The location of `file1` relative to `file2` is `../file1`. This means you are not referring to the absolute location of `file1` which is `/opt/myfiles/file1`. – Uchenna Nwanyanwu Aug 21 '12 at 16:11
  • For context path, this is the root path of your web application. It is the base path used to locate other resources resources in a web application. – Uchenna Nwanyanwu Aug 21 '12 at 16:14