• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to get JSP URL in Servlet

 
Chintan B Shah
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Raf Szczypiorski
Ranch Hand
Posts: 383
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, but I am newbie...do you mind posting some pseudo code?

Thanks for help.

Regards,
Chintan
 
Chintan B Shah
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Never mind...I googled it..Thanks for your help, Raf, much appreciated.

Regards,
Chintan
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64617
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Chintan B Shah
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64617
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64617
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1510
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can even pass a token from each jsp.
 
Chintan B Shah
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for replies everyone, I will try it out.

Thanks
Chintan
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic