Wrox Home  
Search P2P Archive for: Go

  Return to Index  

j2ee thread: Urgent:How to set HttpServletRequest's setAttribute method in init() method of Servlet???


Message #1 by Edward <zhangsc@n...> on Thu, 13 Jun 2002 14:48:19 +0800
you always mark your emails as urgent - why is that?

anyway, dont store it in the request object, store else where - in the
session, or something. An init() can hardly set values in the request since
no request has been received? I would be better for you to throw an
UnavailableException since that is what is in the servlet specifications
though - if you do that, you will get a Error 500 Server error which should
tell you all you need to know. By the way, how can your forward to a page
when there hasnt been any request yet? that doesnt make sense/ The init
method gets called way before the request has been passed to the servlet
(and I suspect that eventually you will be wanting to LoadOnStartup the
servlet anyway since its db setup stuff, right?

chanoch


----- Original Message -----
From: "Edward" <zhangsc@n...>
To: "Java 2 Enterprise Edition" <j2ee@p...>
Sent: Thursday, June 13, 2002 7:48 AM
Subject: [j2ee] Urgent:How to set HttpServletRequest's setAttribute method
in init() method of Servlet???


> I have a question about Servlet.I want to call JSP in init() method of
Servlet,so I need use request.setAttribute method.I know that in
generally,using request.setAttribute method need in doGet or doPost method
in Servlet.But my program is particular,I need do it in init() method of
Servlet,not in doGet or doPost method.Because I need visit Database and get
data and put data into the Servlet's context,when visiting Database,if it
occures errors,I need put the errors transfer to JSP.So I do that.But when I
compile my program,it raise errors:
>
> Error #: 553 : variable request might not have been initialized at line
71, column 17
> Error #: 553 : variable response might not have been initialized at line
72, column 20
>
> My program is follows:
> ......
> public void init() throws ServletException {
>     context=getServletContext();
>     try
>     {
>         //do Database operation
>     }
>     catch(SQLException e)
>     {
>                 System.out.println("PL/SQL Error!");
>                 HttpServletRequest request;   //---------71 line
>                 HttpServletResponse response;  //--------72 line
>                 String url="/error.jsp";
>                 request.setAttribute("jsp","Error while connecting DB!");
>                 RequestDispatcher
dispatcher=getServletContext().getRequestDispatcher(url);
>                 dispatcher.forward(request,response);
>     }
>  ......
> }
>
> How to realize my purpose of set HttpServletRequest's setAttribute method
in init method of Servlet???
> Any idea will be appreciated!
> Thanks!!!
> Edward
>
>


  Return to Index