You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Mason Jones <ma...@clipshow.com> on 2000/07/14 20:37:08 UTC

ServletContext.setAttribute()

I'm trying to compile a servlet to test setting an attribute in the
servlet context, and getting an odd error:

formEditClip.java:31: Method setAttribute(java.lang.String,
java.lang.Object) no
t found in interface javax.servlet.ServletContext.
                context.setAttribute("formMessage", msg);

Looking in the javadoc API for ServletContext, that is certainly
the correct form of the method call... And as far as I know I have
the latest JSDK, and it's finding the class. Are the javadocs
wrong? Is there another version of the method call that anyone
out there knows about?

Thanks for any help.



________________________________________
  Mason Jones              mason(at)clipshow.com
  Director of Software Development
  ClipShow, Inc.            www.clipshow.com
  (650) 696-3164           fax (650) 696-3267


Re: ServletContext.setAttribute()

Posted by "Craig R. McClanahan" <Cr...@eng.sun.com>.
Mason Jones wrote:

> Craig McClanahan wrote:
>
> >In the JSP page, you can retrieve this by using a scriptlet, or (better) by a
> >bean with request scope:
> >
> >    <jsp:useBean id="formMessage" scope="request" class="java.lang.String"/>
> >    The message is <%= formMessage %>
>
> The one question this leaves me with is the use of <%= formMessage %>
> here instead of a getProperty from the bean. What method would
> end up being called in the jsp above? Sorry if that's an obvious
> question...
>

In the scenario above, the "bean" is actually a String itself, instead
of an
object that has a property returning a String.  So, there will not
really be any
method call to your bean method; instead, the contents of "formMessage"
will be
included in the generated page.  In other words, in the generated
servlet you
would see something like this:

    String formMessage = (String) request.getAttribute("formMessage");
    writer.print("The message is");
    writer.print(formMessage);

which relies on the fact that <jsp:useBean> does two different things
for you --
it creates or acquires references to objects in the appropriate scope,
and it also
creates a scripting variable (that is, a local variable within the
service method
of the generated servlet) with the name you specify as the "id".

>
> Thanks again.
>
> ________________________________________
>   Mason Jones              mason(at)clipshow.com

Craig

Re: ServletContext.setAttribute()

Posted by Mason Jones <ma...@clipshow.com>.
Craig McClanahan wrote:
>> ServletConfig sc = getServletConfig();
>> ServletContext context = sc.getServletContext();
>> String formmsg = (String)context.getAttribute("formMessage");
>> out.println(formmsg);
>>
>> That's kind of clunky, and it actually seems to be acting as
>> if the attribute got put into the HttpSession, because it's
>> still there if I go back to the jsp page later.
>
>This does indeed send the message to the JSP page, but there is a problem --
>the servlet context attributes are shared by *all* users of your application.
>Context attributes stay there until you remove them, or until the server is
>shut down.

Ahah, that explains why it was sticking around. That makes
sense, too.

>What you probably want to do in use request attributes, rather than servlet
>context attributes, so that the message is specific to this particular user
>and it goes away after its been used (in the JSP page).  In your servlet, do
>this:

Perfect, that's exactly what I was looking for. Thanks very much.

>In the JSP page, you can retrieve this by using a scriptlet, or (better) by a
>bean with request scope:
>
>    <jsp:useBean id="formMessage" scope="request" class="java.lang.String"/>
>    The message is <%= formMessage %>

The one question this leaves me with is the use of <%= formMessage %>
here instead of a getProperty from the bean. What method would
end up being called in the jsp above? Sorry if that's an obvious
question...

Thanks again.



________________________________________
  Mason Jones              mason(at)clipshow.com
  Director of Software Development
  ClipShow, Inc.            www.clipshow.com
  (650) 696-3164           fax (650) 696-3267


Re: ServletContext.setAttribute()

Posted by "Craig R. McClanahan" <Cr...@eng.sun.com>.
Mason Jones wrote:

> Craig McClanahan wrote:
> >A common way for this to happen is for you to have old servlet classes on
> >your compile CLASSPATH -- such as the "jsdk.jar" file that contains
> >version 2.0 classes (before the ServletContext.setAttribute() method was
> >added to the API).
>
> You're a psychic. ;-) I had jsdk.jar in my CLASSPATH, alright,
> left over from pre-Tomcat days. Oops. All is well, thanks a lot.
>

:-)

>
> While I'm at it, though, I'm curious if there's a better way to
> do what I'm trying to do than I am now. I'm still testing, just
> to see what makes the most sense.
>
> In my servlet, I'm doing the following:
>
> ServletConfig sc = getServletConfig();
> ServletContext context = sc.getServletContext();
> context.setAttribute("formMessage","You got it?");
>
> That basically sends a message to the jsp page, which
> it then redirects to. In the jsp page I have to do the
> following to pull the message out:
>
> ServletConfig sc = getServletConfig();
> ServletContext context = sc.getServletContext();
> String formmsg = (String)context.getAttribute("formMessage");
> out.println(formmsg);
>
> That's kind of clunky, and it actually seems to be acting as
> if the attribute got put into the HttpSession, because it's
> still there if I go back to the jsp page later.
>

This does indeed send the message to the JSP page, but there is a problem --
the servlet context attributes are shared by *all* users of your application.
Context attributes stay there until you remove them, or until the server is
shut down.

What you probably want to do in use request attributes, rather than servlet
context attributes, so that the message is specific to this particular user
and it goes away after its been used (in the JSP page).  In your servlet, do
this:

    String message = "You got it?";
    request.setAttribute("formMessage", message);
    RequestDispatcher rd =
      getServletContext().getRequestDispatcher("/theJspPage.jsp");
    rd.forward(request, response);
    return;

>
> Is there a way to put the attribute in the page context, and
> an easier way to pull it out in the jsp?
>

In the JSP page, you can retrieve this by using a scriptlet, or (better) by a
bean with request scope:

    <jsp:useBean id="formMessage" scope="request" class="java.lang.String"/>
    The message is <%= formMessage %>.

NOTE:  You have to use RequestDispatcher.forward() rather than
response.sendRedirect(), because the redirect causes a *second* request, so
the message wouldn't be there.  In reality, you will want to do forwards
rather than redirects anyway because they save a round trip to the client.

>
> Thanks!
>
> ________________________________________
>   Mason Jones              mason(at)clipshow.com
>   Director of Software Development
>   ClipShow, Inc.            www.clipshow.com
>   (650) 696-3164           fax (650) 696-3267

Craig



Re: ServletContext.setAttribute()

Posted by Mason Jones <ma...@clipshow.com>.
Craig McClanahan wrote:
>A common way for this to happen is for you to have old servlet classes on
>your compile CLASSPATH -- such as the "jsdk.jar" file that contains
>version 2.0 classes (before the ServletContext.setAttribute() method was
>added to the API).

You're a psychic. ;-) I had jsdk.jar in my CLASSPATH, alright,
left over from pre-Tomcat days. Oops. All is well, thanks a lot.

While I'm at it, though, I'm curious if there's a better way to
do what I'm trying to do than I am now. I'm still testing, just
to see what makes the most sense.

In my servlet, I'm doing the following:

ServletConfig sc = getServletConfig();
ServletContext context = sc.getServletContext();
context.setAttribute("formMessage","You got it?");

That basically sends a message to the jsp page, which
it then redirects to. In the jsp page I have to do the
following to pull the message out:

ServletConfig sc = getServletConfig();
ServletContext context = sc.getServletContext();
String formmsg = (String)context.getAttribute("formMessage");
out.println(formmsg);

That's kind of clunky, and it actually seems to be acting as
if the attribute got put into the HttpSession, because it's
still there if I go back to the jsp page later.

Is there a way to put the attribute in the page context, and
an easier way to pull it out in the jsp?

Thanks!



________________________________________
  Mason Jones              mason(at)clipshow.com
  Director of Software Development
  ClipShow, Inc.            www.clipshow.com
  (650) 696-3164           fax (650) 696-3267


Re: ServletContext.setAttribute()

Posted by "Craig R. McClanahan" <Cr...@eng.sun.com>.
A common way for this to happen is for you to have old servlet classes on
your compile CLASSPATH -- such as the "jsdk.jar" file that contains
version 2.0 classes (before the ServletContext.setAttribute() method was
added to the API).

Craig McClanahan


Mason Jones wrote:

> Very strange... I got the same message from the compiler
> about not finding a matching method. Must be something
> weird with my JDK, but I'm not sure how that could be.
>
> At 04:04 PM 7/14/00 -0700, you wrote:
> >This worked for me... I did something like:
> >
> >ServletConfig sc=getServletConfig();
> >ServletContext ctx=sc.getServletContext();
> >ctx.setAttribute("formMessage",msg);
> >
> >(from within a class that extended HttpServlet)
> >
> >On Fri, 14 Jul 2000, Mason Jones wrote:
> >
> >>
> >> I'm trying to compile a servlet to test setting an attribute in the
> >> servlet context, and getting an odd error:
> >>
> >> formEditClip.java:31: Method setAttribute(java.lang.String,
> >> java.lang.Object) no
> >> t found in interface javax.servlet.ServletContext.
> >>                 context.setAttribute("formMessage", msg);
> >>
> >> Looking in the javadoc API for ServletContext, that is certainly
> >> the correct form of the method call... And as far as I know I have
> >> the latest JSDK, and it's finding the class. Are the javadocs
> >> wrong? Is there another version of the method call that anyone
> >> out there knows about?
> >>
> >> Thanks for any help.
> >>
> >>
> >>
> >> ________________________________________
> >>   Mason Jones              mason(at)clipshow.com
> >>   Director of Software Development
> >>   ClipShow, Inc.            www.clipshow.com
> >>   (650) 696-3164           fax (650) 696-3267
> >>
> >>
> >>
> >
> >-Eric Harashevsky
>
> ________________________________________
>   Mason Jones              mason(at)clipshow.com
>   Director of Software Development
>   ClipShow, Inc.            www.clipshow.com
>   (650) 696-3164           fax (650) 696-3267


Re: ServletContext.setAttribute()

Posted by Mason Jones <ma...@clipshow.com>.
Very strange... I got the same message from the compiler
about not finding a matching method. Must be something
weird with my JDK, but I'm not sure how that could be.

At 04:04 PM 7/14/00 -0700, you wrote:
>This worked for me... I did something like:
>
>ServletConfig sc=getServletConfig();
>ServletContext ctx=sc.getServletContext();
>ctx.setAttribute("formMessage",msg);
>
>(from within a class that extended HttpServlet)
>
>On Fri, 14 Jul 2000, Mason Jones wrote:
>
>> 
>> I'm trying to compile a servlet to test setting an attribute in the
>> servlet context, and getting an odd error:
>> 
>> formEditClip.java:31: Method setAttribute(java.lang.String,
>> java.lang.Object) no
>> t found in interface javax.servlet.ServletContext.
>>                 context.setAttribute("formMessage", msg);
>> 
>> Looking in the javadoc API for ServletContext, that is certainly
>> the correct form of the method call... And as far as I know I have
>> the latest JSDK, and it's finding the class. Are the javadocs
>> wrong? Is there another version of the method call that anyone
>> out there knows about?
>> 
>> Thanks for any help.
>> 
>> 
>> 
>> ________________________________________
>>   Mason Jones              mason(at)clipshow.com
>>   Director of Software Development
>>   ClipShow, Inc.            www.clipshow.com
>>   (650) 696-3164           fax (650) 696-3267
>> 
>> 
>> 
>
>-Eric Harashevsky


________________________________________
  Mason Jones              mason(at)clipshow.com
  Director of Software Development
  ClipShow, Inc.            www.clipshow.com
  (650) 696-3164           fax (650) 696-3267


Error 500

Posted by Jeremy Linzer <jl...@alabanza.com>.
Sorry about that my last message had the wrong title
Re: ServletContext.setAttribute()


> Hi all,
>
> I am pretty new to this and I have what maybe a simple question. I wrote
> a simple Servlet (passes a message to a JSP and JSP outputs it). It
> compiles fine but when I try accessing it I got a 500 error:
>
> java.lang.NullPointerException:
>         at HelloWorldServlet.service(HelloWorldServlet.java:14)
>         at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
>         at
> org.apache.tomcat.core.ServletWrapper.handleRequest(ServletWrapper.java:503)
>
>         at
> org.apache.tomcat.core.ContextManager.service(ContextManager.java:559)
>         at
> org.apache.tomcat.service.http.HttpConnectionHandler.processConnection(HttpConnectionHandler.java:160)
>
>         at
> org.apache.tomcat.service.TcpConnectionThread.run(SimpleTcpEndpoint.java:338)
>
>         at java.lang.Thread.run(Thread.java)
>
> line 14 that it refers to in the servlet is rd.forward(req, res);
> rd is of type RequestDispatcher
>
> If anyone has any ideas or suggestions I would really appreciate it.
>
> THanks,
> Jeremy


Re: ServletContext.setAttribute()

Posted by Jeremy Linzer <jl...@alabanza.com>.
Thanks.
That was the problem. I used now req.getRequestDispatcher("myJSP.jsp") with req being a request object. and
it worked. I had been using getServletContext.getRequestDispatcher("myJSP.jsp") which didn't make sense to
me but it was in my JSP book (Webdevelopment with JSP by Fields & Kolb) so I figured it should work

Jeremy

"Eric H." wrote:

> Where did you get the RequestDispatcher?
> Did you do something like:
>
> (where request is the HttpServletRequest sent to your servlet and
>  response is the HttpServletResponse)
>
> RequestDispatcher rd=request.getRequestDispatcher("../path/some.jsp");
> rd.forward(request,response);
>
> On Fri, 14 Jul 2000, Jeremy Linzer wrote:
>
> > Hi all,
> >
> > I am pretty new to this and I have what maybe a simple question. I wrote
> > a simple Servlet (passes a message to a JSP and JSP outputs it). It
> > compiles fine but when I try accessing it I got a 500 error:
> >
> > java.lang.NullPointerException:
> >         at HelloWorldServlet.service(HelloWorldServlet.java:14)
> >         at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
> >         at
> > org.apache.tomcat.core.ServletWrapper.handleRequest(ServletWrapper.java:503)
> >
> >         at
> > org.apache.tomcat.core.ContextManager.service(ContextManager.java:559)
> >         at
> > org.apache.tomcat.service.http.HttpConnectionHandler.processConnection(HttpConnectionHandler.java:160)
> >
> >         at
> > org.apache.tomcat.service.TcpConnectionThread.run(SimpleTcpEndpoint.java:338)
> >
> >         at java.lang.Thread.run(Thread.java)
> >
> > line 14 that it refers to in the servlet is rd.forward(req, res);
> > rd is of type RequestDispatcher
> >
> > If anyone has any ideas or suggestions I would really appreciate it.
> >
> > THanks,
> > Jeremy
> >
> >
> >
>
> -Eric Harashevsky, USC ISD Student Consultant


Re: ServletContext.setAttribute()

Posted by "Eric H." <eh...@usc.edu>.
Where did you get the RequestDispatcher?
Did you do something like:

(where request is the HttpServletRequest sent to your servlet and
 response is the HttpServletResponse)

RequestDispatcher rd=request.getRequestDispatcher("../path/some.jsp");
rd.forward(request,response);

On Fri, 14 Jul 2000, Jeremy Linzer wrote:

> Hi all,
> 
> I am pretty new to this and I have what maybe a simple question. I wrote
> a simple Servlet (passes a message to a JSP and JSP outputs it). It
> compiles fine but when I try accessing it I got a 500 error:
> 
> java.lang.NullPointerException:
>         at HelloWorldServlet.service(HelloWorldServlet.java:14)
>         at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
>         at
> org.apache.tomcat.core.ServletWrapper.handleRequest(ServletWrapper.java:503)
> 
>         at
> org.apache.tomcat.core.ContextManager.service(ContextManager.java:559)
>         at
> org.apache.tomcat.service.http.HttpConnectionHandler.processConnection(HttpConnectionHandler.java:160)
> 
>         at
> org.apache.tomcat.service.TcpConnectionThread.run(SimpleTcpEndpoint.java:338)
> 
>         at java.lang.Thread.run(Thread.java)
> 
> line 14 that it refers to in the servlet is rd.forward(req, res);
> rd is of type RequestDispatcher
> 
> If anyone has any ideas or suggestions I would really appreciate it.
> 
> THanks,
> Jeremy
> 
> 
> 

-Eric Harashevsky, USC ISD Student Consultant


Re: ServletContext.setAttribute()

Posted by Jeremy Linzer <jl...@alabanza.com>.
Hi all,

I am pretty new to this and I have what maybe a simple question. I wrote
a simple Servlet (passes a message to a JSP and JSP outputs it). It
compiles fine but when I try accessing it I got a 500 error:

java.lang.NullPointerException:
        at HelloWorldServlet.service(HelloWorldServlet.java:14)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
        at
org.apache.tomcat.core.ServletWrapper.handleRequest(ServletWrapper.java:503)

        at
org.apache.tomcat.core.ContextManager.service(ContextManager.java:559)
        at
org.apache.tomcat.service.http.HttpConnectionHandler.processConnection(HttpConnectionHandler.java:160)

        at
org.apache.tomcat.service.TcpConnectionThread.run(SimpleTcpEndpoint.java:338)

        at java.lang.Thread.run(Thread.java)

line 14 that it refers to in the servlet is rd.forward(req, res);
rd is of type RequestDispatcher

If anyone has any ideas or suggestions I would really appreciate it.

THanks,
Jeremy


Re: ServletContext.setAttribute()

Posted by "Eric H." <eh...@usc.edu>.
This worked for me... I did something like:

ServletConfig sc=getServletConfig();
ServletContext ctx=sc.getServletContext();
ctx.setAttribute("formMessage",msg);

(from within a class that extended HttpServlet)

On Fri, 14 Jul 2000, Mason Jones wrote:

> 
> I'm trying to compile a servlet to test setting an attribute in the
> servlet context, and getting an odd error:
> 
> formEditClip.java:31: Method setAttribute(java.lang.String,
> java.lang.Object) no
> t found in interface javax.servlet.ServletContext.
>                 context.setAttribute("formMessage", msg);
> 
> Looking in the javadoc API for ServletContext, that is certainly
> the correct form of the method call... And as far as I know I have
> the latest JSDK, and it's finding the class. Are the javadocs
> wrong? Is there another version of the method call that anyone
> out there knows about?
> 
> Thanks for any help.
> 
> 
> 
> ________________________________________
>   Mason Jones              mason(at)clipshow.com
>   Director of Software Development
>   ClipShow, Inc.            www.clipshow.com
>   (650) 696-3164           fax (650) 696-3267
> 
> 
> 

-Eric Harashevsky