You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by "Harding, David" <dh...@Thomcomp.com> on 2000/11/01 19:10:34 UTC

How do you pass data from a servlet to a jsp page?

is there some undocumented trick to this?

i tried it and it does not work.

i get Attribute <att_name> does not have a value errors when i am sure that
i called setAttribute in the servlet with a valid value.

why am i getting this error?

cheers,
   david j harding

Re: How do you pass data from a servlet to a jsp page?

Posted by "Harding, David" <dh...@Thomcomp.com>.
on 11/1/00 1:52 PM, Craig R. McClanahan at Craig.McClanahan@eng.sun.com
wrote:

> "Harding, David" wrote:
> 
>> odd, getServletContext() returns null for me....
> 
> The 99.9% cause of this one is having an init(ServletConfig config) method
> that
> fails to call super.init(config) somewhere.
> 
> I use the init() method that takes no arguments to avoid this kind of problem.
> 
> Craig McClanahan
> 

my init looks like this:

public void init(ServletConfig config)
        throws ServletException {
    super.init(config);
    some_param = config.getInitParameter("a_parameter");
    // gets some more of these and stores them in private members
}



hrmmmm....so this does not appear to be the problem.

cheers,
   david

Re: How do you pass data from a servlet to a jsp page?

Posted by "Craig R. McClanahan" <Cr...@eng.sun.com>.
"Harding, David" wrote:

> odd, getServletContext() returns null for me....

The 99.9% cause of this one is having an init(ServletConfig config) method that
fails to call super.init(config) somewhere.

I use the init() method that takes no arguments to avoid this kind of problem.

Craig McClanahan



Re: How do you pass data from a servlet to a jsp page?

Posted by "Harding, David" <dh...@Thomcomp.com>.
on 11/1/00 1:31 PM, Bill Pfeiffer at wsplist1@tampabay.rr.com wrote:

> Are you using the request dispatcher to pass the servlet request to the jsp?
> I do this all the time, setAttribute in the servlet (controller), call
> forward() to forward the request to the jsp, and do request.getAttribute().
> 
> Servlet:
> 
> RequestDispatcher rd;
> req.setAttribute("attr1", "hello");
> rd = getServletContext().getRequestDispatcher("jsp/MyJsp.jsp");

odd, getServletContext() returns null for me....saw that one in the archives
too...the answer was to use request.getRequestDispatcher(url)

> rd.forward(req, res);
> 
> MyJsp.jsp:
> 
> <%= request.getAttribute("attr1")%>
> 

otherwise, that is what i am doing...

Re: How do you pass data from a servlet to a jsp page?

Posted by Bill Pfeiffer <ws...@tampabay.rr.com>.
Are you using the request dispatcher to pass the servlet request to the jsp?
I do this all the time, setAttribute in the servlet (controller), call
forward() to forward the request to the jsp, and do request.getAttribute().

Servlet:

  RequestDispatcher rd;
  req.setAttribute("attr1", "hello");
  rd = getServletContext().getRequestDispatcher("jsp/MyJsp.jsp");
  rd.forward(req, res);

MyJsp.jsp:

<%= request.getAttribute("attr1")%>

HTH,

Bill Pfeiffer


----- Original Message -----
From: "Harding, David" <dh...@Thomcomp.com>
To: <to...@jakarta.apache.org>
Sent: Wednesday, November 01, 2000 1:10 PM
Subject: How do you pass data from a servlet to a jsp page?


> is there some undocumented trick to this?
>
> i tried it and it does not work.
>
> i get Attribute <att_name> does not have a value errors when i am sure
that
> i called setAttribute in the servlet with a valid value.
>
> why am i getting this error?
>
> cheers,
>    david j harding
>