Murphy316
Murphy316

Reputation: 766

How to handle paths in this case

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

Upvotes: 0

Views: 40

Answers (1)

Uchenna Nwanyanwu
Uchenna Nwanyanwu

Reputation: 3204

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.

Upvotes: 1

Related Questions