aspose file tools*
The moose likes Servlets and the fly likes How to get JSP URL in Servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "How to get JSP URL in Servlet" Watch "How to get JSP URL in Servlet" New topic
Author

How to get JSP URL in Servlet

Chintan B Shah
Ranch Hand

Joined: Sep 23, 2008
Posts: 83
Hi all,

This might be a newbie question, but I am looking for something like this

1. A.jsp has form and calls Servlet B in its "action" tag.

2. Now, in Servlet B I need to know that request has come from A.jsp.

I have tried following methods with no luck

request.getPathInfo();
request.getRequestURI())
//All of the above is returning me Servlet B's location.

How do I do this?

Any help greatly appreciated.

Thanks
Chintan.


SCJA 1.0
Raf Szczypiorski
Ranch Hand

Joined: Aug 21, 2008
Posts: 383
You could try getting the "Referer" header value, but mind this one is optional and so not every client may send it.
Chintan B Shah
Ranch Hand

Joined: Sep 23, 2008
Posts: 83
Sorry, but I am newbie...do you mind posting some pseudo code?

Thanks for help.

Regards,
Chintan
Chintan B Shah
Ranch Hand

Joined: Sep 23, 2008
Posts: 83
Never mind...I googled it..Thanks for your help, Raf, much appreciated.

Regards,
Chintan
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61766
    
  67

The fact that you are relying on this information is a red flag for a poor and fragile design. What are you doing with this info?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Chintan B Shah
Ranch Hand

Joined: Sep 23, 2008
Posts: 83
Basically, I am doing authentication.

Servlet is acting as a request controller for mutiple JSP pages.

Here are the steps that I am doing.

1. Pass parameters from A.JSP(form parameters)
2. In B Servlet , call request.getParameter to get the parameters
3. Servlet delegates the authentication process to Session bean by passing those parameters.
4. Once "true" is returned from Session beans, Servlet then needs to go to either C.jsp or D.jsp depending on whether parameters were passed from A.jsp or something else.

Please let me know if I can improve this.

However, I am not able to understand one thing. HTTPServletRequest object should return all client information, so why does request.getURI() and other methods that I used before returns me Servlet Path?

Thanks
Chintan.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61766
    
  67

You still did not explain why you need to know which JSP you came from.

Also, bear in mind that the referer trick doesn't always give you a result, and that it also easy to spoof.
Chintan B Shah
Ranch Hand

Joined: Sep 23, 2008
Posts: 83
Because the Servlet processes same parameters from different pages

1. A.jsp passes Email, password.
2. B.jsp passes Email,password.

If A.jsp, go to C.jsp
If B.jsp, go to D.jsp

If I dont use this approach, then I will have to use 2 different servlets for doing same authentication. In future, if there are N jsp pages, then I will have to write N servlets.

Let me know if I am wrong somewhere.

I thought of another approach where in I pass different parameters from JSP like
1. A.jsp (Email, Password)
2. B.jsp (Email1, password1)
then verify on Servlet side which parameters are NULL, if Email and Password are NULL then check Email1 and Password1 and if they are NOT NULL then it basically means, the request came from B.jsp, so on successful authentication go to D.jsp.

Let me know if this is a good approach..however, if there are N JSP pages and each of them send 2 parameters, I believe I will run into the same problem.

So, basically what I thought was to use same parameters on all pages, and depending on where they came from...redirect them to other pages.

Thanks
Chintan
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61766
    
  67

Why are you handling authentication in a servlet rather than a filter? There are also other ways to factor out common processing (inheritance, delegation, etc). Triggering behavior based upon the referer is very fragile and a poor practice.

If worse came to worse, include a request parameter to explicitly direct the desired behavior. Relying upon implicit information like the referer is a bug waiting to happen (if you can even getting working reliably).
feng fei
Greenhorn

Joined: Mar 16, 2008
Posts: 1
you can try :

javax.servlet
Interface RequestDispatcher

use it's two methods:

void forward(ServletRequest request, ServletResponse response)
void include(ServletRequest request, ServletResponse response)

and in one servlet you can get the RequestDispatcher as :

String b = "/servlet/Servlet_B";
RequestDispatcher rd = request.getRequestDispatcher(b);
rd.forward(request, response);
Bosun Bello
Ranch Hand

Joined: Nov 06, 2000
Posts: 1510
You can even pass a token from each jsp.


Bosun (SCJP, SCWCD)
So much trouble in the world -- Bob Marley
Chintan B Shah
Ranch Hand

Joined: Sep 23, 2008
Posts: 83
Thanks for replies everyone, I will try it out.

Thanks
Chintan
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to get JSP URL in Servlet