I want to see the name of the JSP on servlet, through which the request has been sent to the servlet.
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.
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.
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...!!!
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.