You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by lightbulb432 <ve...@hotmail.com> on 2007/07/01 21:47:47 UTC

HttpSession questions

Why is it that you need an HttpSession in order to get a ServletContext? I
tried to obtain a ServletContext using an HttpServletRequest, but this isn't
possible in the API, unless you do a
request.getSession().getServletContext().

This creates a session, however, and I'm not sure I need a session to be
created at the point in time where I am obtaining the ServletContext - all I
want to do is have access to something I put in application scope earlier
on.

If I do a request.getSession(), then a session is started. If I do a
request.getSession(false), then a NullPointerException will be thrown if I
call getServletContext() on it. Why has the API been designed in such a way
- after all, since it's possible to know what servlet context each request
is going to, then shouldn't you be able to get the servlet context from the
request?

Thanks.
-- 
View this message in context: http://www.nabble.com/HttpSession-questions-tf4008885.html#a11385003
Sent from the Tomcat - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


RE: HttpSession questions

Posted by "Caldarale, Charles R" <Ch...@unisys.com>.
> From: lightbulb432 [mailto:veerukrishnan@hotmail.com] 
> Subject: RE: HttpSession questions
> 
> If however, I were to change my code and pass the return 
> value of Servlet's getServletContext() to my class, would
> a session still be created

No, no session is created.

> Out of curiosity, why is there a getServletContext() on 
> HttpSession?

You're not asking a Tomcat question here, but rather one that concerns
the servlet spec.  Such questions and comments should really be directed
to the appropriate JSR working group.

> But is there some design reason you can think of that the 
> method is in the session but not request object?

Requests have relatively short lifetimes, while sessions and contexts
typically exist orders of magnitude longer.  Since there's a trivial
solution to your problem, I don't think it's a big deal.

 - Chuck


THIS COMMUNICATION MAY CONTAIN CONFIDENTIAL AND/OR OTHERWISE PROPRIETARY
MATERIAL and is thus for use only by the intended recipient. If you
received this in error, please contact the sender and delete the e-mail
and its attachments from all computers.

---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: HttpSession questions

Posted by Bill Barker <wb...@wilshire.com>.
"lightbulb432" <ve...@hotmail.com> wrote in message 
news:11385434.post@talk.nabble.com...
>
> I did notice the getServletContext() of the servlet classes, but in this 
> case
> the code is in a custom class that was forwarded to by the servlet - I 
> don't
> have access to the servlet in this class.
>
> If however, I were to change my code and pass the return value of 
> Servlet's
> getServletContext() to my class, would a session still be created, as
> happens using the request.getSession().getServletContext() approach?
>

Charles has already answered this, but another option would be to add the 
ServletContext as a Request attribute in your Servlet/Filter.  I don't know 
your app, so I don't know which is the better design.

> Out of curiosity, why is there a getServletContext() on HttpSession? 
> Pardon
> my ignorance, but that just doesn't seem "right" - if it's there as
> convenience, then shouldn't it be on the HttpServletRequest class as well?
> If that's just the way it is, then that's fine... But is there some design
> reason you can think of that the method is in the session but not request
> object? Am I missing something here?
>

Well, no, the Servlet, Filter, and Session Objects all belong to a single 
ServletContext, so it makes sense that they should be able to access it. 
However, the Request can (and in TC without a sandbox will) be potentially 
reused for requests to many different Contexts.

>
>
>
> Caldarale, Charles R wrote:
>>
>>> From: lightbulb432 [mailto:veerukrishnan@hotmail.com]
>>> Subject: HttpSession questions
>>>
>>> Why is it that you need an HttpSession in order to get a
>>> ServletContext?
>>
>> You don't - since your code is in a class that extends HttpServlet, just
>> call getServletContext() directly; it's defined under
>> javax.servlet.GenericServlet.
>>
>>  - Chuck
>>
>>
>> THIS COMMUNICATION MAY CONTAIN CONFIDENTIAL AND/OR OTHERWISE PROPRIETARY
>> MATERIAL and is thus for use only by the intended recipient. If you
>> received this in error, please contact the sender and delete the e-mail
>> and its attachments from all computers.
>>
>> ---------------------------------------------------------------------
>> To start a new topic, e-mail: users@tomcat.apache.org
>> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
>> For additional commands, e-mail: users-help@tomcat.apache.org
>>
>>
>>
>
> -- 
> View this message in context: 
> http://www.nabble.com/HttpSession-questions-tf4008885.html#a11385434
> Sent from the Tomcat - User mailing list archive at Nabble.com.
>
>
> ---------------------------------------------------------------------
> To start a new topic, e-mail: users@tomcat.apache.org
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
>
> 




---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


RE: HttpSession questions

Posted by lightbulb432 <ve...@hotmail.com>.
I did notice the getServletContext() of the servlet classes, but in this case
the code is in a custom class that was forwarded to by the servlet - I don't
have access to the servlet in this class.

If however, I were to change my code and pass the return value of Servlet's
getServletContext() to my class, would a session still be created, as
happens using the request.getSession().getServletContext() approach?

Out of curiosity, why is there a getServletContext() on HttpSession? Pardon
my ignorance, but that just doesn't seem "right" - if it's there as
convenience, then shouldn't it be on the HttpServletRequest class as well?
If that's just the way it is, then that's fine... But is there some design
reason you can think of that the method is in the session but not request
object? Am I missing something here?




Caldarale, Charles R wrote:
> 
>> From: lightbulb432 [mailto:veerukrishnan@hotmail.com] 
>> Subject: HttpSession questions
>> 
>> Why is it that you need an HttpSession in order to get a 
>> ServletContext?
> 
> You don't - since your code is in a class that extends HttpServlet, just
> call getServletContext() directly; it's defined under
> javax.servlet.GenericServlet.
> 
>  - Chuck
> 
> 
> THIS COMMUNICATION MAY CONTAIN CONFIDENTIAL AND/OR OTHERWISE PROPRIETARY
> MATERIAL and is thus for use only by the intended recipient. If you
> received this in error, please contact the sender and delete the e-mail
> and its attachments from all computers.
> 
> ---------------------------------------------------------------------
> To start a new topic, e-mail: users@tomcat.apache.org
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
> 
> 
> 

-- 
View this message in context: http://www.nabble.com/HttpSession-questions-tf4008885.html#a11385434
Sent from the Tomcat - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


RE: HttpSession questions

Posted by "Caldarale, Charles R" <Ch...@unisys.com>.
> From: lightbulb432 [mailto:veerukrishnan@hotmail.com] 
> Subject: HttpSession questions
> 
> Why is it that you need an HttpSession in order to get a 
> ServletContext?

You don't - since your code is in a class that extends HttpServlet, just
call getServletContext() directly; it's defined under
javax.servlet.GenericServlet.

 - Chuck


THIS COMMUNICATION MAY CONTAIN CONFIDENTIAL AND/OR OTHERWISE PROPRIETARY
MATERIAL and is thus for use only by the intended recipient. If you
received this in error, please contact the sender and delete the e-mail
and its attachments from all computers.

---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: HttpSession questions

Posted by David Smith <dn...@cornell.edu>.
It's also available via the servlet class.  See

http://tomcat.apache.org/tomcat-5.5-doc/servletapi/javax/servlet/GenericServlet.html#getServletContext()

If you are trying this via jsp, it should be available via PageContext.  See

http://tomcat.apache.org/tomcat-5.5-doc/jspapi/javax/servlet/jsp/PageContext.html#getServletContext()

--David

lightbulb432 wrote:
> Why is it that you need an HttpSession in order to get a ServletContext? I
> tried to obtain a ServletContext using an HttpServletRequest, but this isn't
> possible in the API, unless you do a
> request.getSession().getServletContext().
>
> This creates a session, however, and I'm not sure I need a session to be
> created at the point in time where I am obtaining the ServletContext - all I
> want to do is have access to something I put in application scope earlier
> on.
>
> If I do a request.getSession(), then a session is started. If I do a
> request.getSession(false), then a NullPointerException will be thrown if I
> call getServletContext() on it. Why has the API been designed in such a way
> - after all, since it's possible to know what servlet context each request
> is going to, then shouldn't you be able to get the servlet context from the
> request?
>
> Thanks.
>   


---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org