• Post Reply Bookmark Topic Watch Topic
  • New Topic

Getting name of the JSP on servlet

 
Abhishekshri Shrivastava
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I want to see the name of the JSP on servlet, through which the request has been sent to the servlet.
or
in more details :

I have a number of JSPs and a common servlet for all those JSPs.
Servlet gets request from JSP. and i wann to get the name of JSP from which the servlet has recieved the request. my servlet wann to serve the request based upon JSP name.

thanks,please suggest me the various approach and probable solution.

:ABHISHEK SHRIVASTAVA
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65518
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That would be a poor design.

Rather, use a hidden element to pass a parameter that the servlet can explicitly use for differentiation rather than something implicit like the name of a JSP.
 
xsunil kumar
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Agree to use Hidden parameter. Assign the value of the hidden parameter as the name of the JSP. This you can do it dynamically by using Java script.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65518
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the value that each different JSP will use is fixed, there's no need for JavaScript. Just set the value.

And I would not use the name of the JSP. That's fragile and a good example of needless coupling. Use parameters that make sense from the point of view of the servlet. What is the value really telling the servlet? Almost think of it as a method argument.
 
Abhishekshri Shrivastava
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
i do agree with your thoughts...
always we do the same as suggested by you people. but i have another target related to it...
i have 500 jsps with me....
i have to check all the HTML controls and their types, their values and have to do certain validations....if the validation becomes false then i have to again get back (forward ) to the JSP from which i got the request..
i have to make application generic...such that only a few lines of code will work for all JSPs... to get all those html elements details on servlet i am using JERICHO parser....
i don't wann to define 500 hidden parameter.. i wann single approach working for all those jsps...

now come to the point ..i can forward to the jsp only when if i know the name of jsp from which i have got an request..that's why i do wann to know the name of the jsp from which i got the request,..
is there any way..please suggest me.... i am some r&d type of work..so for that we have to go through all the possibilities...!!!
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65518
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you need to forward back to a specific JSP, then the URL to redirect back to could also be provided as a hidden parameter.
 
Shashank Ag
Ranch Hand
Posts: 88
Java Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well as you asked me to solve this I am writing it, but this is very bad and tricky solution. In hindi I say it big Jugaad
But as I say, for desperate times desparate measures are to be taken. So here it goes.

Basic concept first:
You cannot determine name of jsp or view component as I call it.
It is because there are many logical paradox, like a jsp could be included within another jsp using <jsp:include />.

But there is a hope.
You can determine the requested url by client on server end.
This means if you mapped a jsp in web.xml for url pattern say .../gottcha.page -> gottcha.jsp
then you can get url ".../gottcha.page" in servlet.

But that being said, whenever your servlet's url is requested then, you can get only that servlet's url in you servlet.
So, what's use of all this? Now, here it gets tricky.

1. You create a request filter. Google it if you don't know how.
In this get the requested url via request.getRequestURI() this will give path of requested url after context root.
Save this url in session as attribute.

2. Now in web.xml make the filter mapping of above filter for pattern *.jsp
3. In your servlet get the url from session and use it.

NOTE: I am making some assumptions here, but most are practical one so let me know if solution needs some tweaking.
Don't forget add (+1) if like my replies.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!