getting the request and response within a class instantiated in a JSP page
Hi
i have a jsp page . In this page i am instantaiating a class (say TaskProcessor) . I need to access the request and response objects of the jsp page with in the class TaskProcessor (without passing the request and response explicitly to the class).
Can i use the JspFactory.getPageContext() to get the page context for this or should i use getServletConfig().getServletContext()
Please so help me out on this it's urgent.
Thanks,
Babita
[478 byte] By [
babita_sun] at [2007-9-30 19:04:18]

why would you not like to pass it?it would be much simpler that way
The JSPFactory class is used by the JSP Container to make JSPs. It specifically says in the api:
" JspFactory objects should not be used by JSP page authors. "
Besides, the getPageContext method requires a request and response as parameters, so I don't see how that will help.
I do not see any methods in the ServletContext that would provide request and response either.
You best best is to pass them into the class. Or, make TaskProcessor a TagHandler, which will 1) make it easier to use, and 2) provide a Pagecontext that will have access to the request and response.
There are a number of implicit variables in JSP pages. Amongst these are one each of type javax.servlet.http.HttpServletRequest and javax.servlet.http.HttpServletResponse.Good luck.
Here is a simple JSP to illustrate this.
<html>
<body>
<%
String remoteAddr = request.getRemoteAddr();
response.setContentType("text/html;charset=ISO-8859-1");
%>
Hello user! I see your IP Address is <%= remoteAddr%>
</body>
</html>
How about some Dukes?
Hi,
Thanks everybody for the replies....
But... i think i need to reframe my question or say detail it a bit more...
so here's it:
We have our framework (java) which we use for development.
In our applications we often have classes those do lots of data churning.
We have made a class CMProcess for the same.
All the processing classes are inherited from this class.
CMProcess holds lots of generic code and properties.
One common feature of all the processor classes is that they need the request and response of the JSP page.
One way that i know is to have properties for these in the class and set the request and response from the jsp page , other is to pass the request and response as parameters to the constructor.
But we do not want to do it... :-( for some reasons ....
So now i need the request and response in the classes.
As far as my wisdom tells me.. i think the jsp that is ultimately converted to a servlet and the class that i use in the jsp page run/or are executed in the same context, so we should somehow be able to access the request , response objects /intannces within the instances of the processor class.
am sure there should be some way out , just that i do not have that much time to search..
but anyway am searching...
so if anybody could help me out still then thanks in advance :-)... am waiting to give away the dukes...
Thanks,
Babita
The JSP gets the request and response via method perameters. The actual method call that runs you JSP looks like this:
public void _jspService (HttpServletRequest request, HttpServletResponse response)
If you have a method in a normal Java program with the method:
public void someMethod(String needThis)
Then, in the method, you create a new object called NeededBean, how can NeededBean get access to the needThis String object?
NeededBean nb = new NeededBean(); //hmm, I want access to needThis. How to?
The only way is to pass it in. You can do it 1 of two ways:
1) pass the request/response objects into the object, either through setters, constructor call, or as parameters to the method you want to call
2) pass the pageContext object available in the _jspService method to the class (in one of the manners above) and get the request and response from that.
you are suggesting the two options those i said i know but i don't want to / can't use
> you are suggesting the two options those i said i know
> but i don't want to / can't use
Do you program in Java? Do you understand how it works? If you do, then you will understand this is impossible. If not, I would start with looking into the Java Tutorials and try to learn the language before trying to program in it.
yes i do code in java and do understand how it works , i may not be an expert in it. Else why would i post a question in the forum.
I do have get that much time out of my job/cofding just to post questions on the forum to test people out ....
I think the forum is to help people out and give them proper and decent replies and make them understand politely and not poke indirect comments on others skills...
And what if i do not code in java ....?
can't i post a question regarding the doubts i have ...?
> yes i do code in java and do understand how it works ,
> i may not be an expert in it. Else why would i post a
> question in the forum.
> I do have get that much time out of my job/cofding
> just to post questions on the forum to test people out
> ....
> I think the forum is to help people out and give them
> proper and decent replies and make them understand
> politely and not poke indirect comments on others
> skills...
> And what if i do not code in java ....?
> can't i post a question regarding the doubts i have
> ...?
I am sorry if that is how it came out. If you do not code in java than this problem would be more understandable (JSPs are often coded by web-developers rather than Java programers), and I would point you to some tutorials that would hopefully help you to understand that local method variables are not accessible to other methods/objects instantiated in those methods without explicitly passing them in. If you understand Java, I assume you would understand this most basic question, unless you are just beginning.
It is frustrating because you were told three times you could not do what you want, and we suggested different means to do it. But basically you are saying this isn't a good answer because it is not what you want to hear. Don't mix a 'negative' reply with an indecent reply.
Anyway so i have got answers that i already know about as i had written before.Since there is no other solution i think i should close this question and i do not think i can give the dollars to anybody :-( sorry.