aspose file tools*
The moose likes Servlets and the fly likes how to call Jsp in servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "how to call Jsp in servlet" Watch "how to call Jsp in servlet" New topic
Author

how to call Jsp in servlet

Nitin Jain
Greenhorn

Joined: Oct 24, 2006
Posts: 4
I am not able to find to call an JSP form an Servlet.
want code for this.
Eddy Lee Sin Ti
Ranch Hand

Joined: Oct 06, 2005
Posts: 135
Hi,

You want to pass the control to a particular jsp after the servlet finish processing or you just want to invoke a jsp and get the result back to servlet. Try to describe more, it might helps.

Cheers.


SCJP, SCWCD, SCJWS, IBM 700,IBM 701, IBM 704, IBM 705, CA Clarity Technical<br /> <br /><a href="http://eddyleesinti.blogspot.com" target="_blank" rel="nofollow">http://eddyleesinti.blogspot.com</a>
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Hi, I'm guessing you mean the browser did a GET or POST to a servlet, the servlet did some interesting things and now you'd like to use a JSP to build the response HTML. If so, you'll wind up with something like:

If you haven't done this before, spend some time in the JavaDoc for HttpServlet getServletContext(), the context object getRequestDispatcher() and the request dispatcher forward() methods.

Did that answer the right question?


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Nitin Jain
Greenhorn

Joined: Oct 24, 2006
Posts: 4
Thanks stan,
My question was like that only..
instead of forward can I use include..
ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
Originally posted by Nitin Jain:
Thanks stan,
My question was like that only..
instead of forward can I use include..


Do you want to do some more processing after that???
Marko Debac
Ranch Hand

Joined: Aug 21, 2006
Posts: 121
Hi,



but, what if I want to set and forward boolean variable to jsp (where my jsp could see that this boolean var is set to true or false and do something); so, I coudnt do
because, var must be an object and there is no such thing as request.setParameter

please help
Natasza Biecek
Greenhorn

Joined: Oct 26, 2006
Posts: 28
hi all,

so try to use Boolean class instead of raw boolean...
a piece of servlet

and jsp page:


regards,
Natasza
ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
Originally posted by Marko Debac:
Hi,



but, what if I want to set and forward boolean variable to jsp (where my jsp could see that this boolean var is set to true or false and do something); so, I coudnt do
because, var must be an object and there is no such thing as request.setParameter

please help


... and FYI, request doesn't have any setParameter() method because parameters are meant to be submitted with form.
Marko Debac
Ranch Hand

Joined: Aug 21, 2006
Posts: 121
Hi again,

boolean logicalValue = false;request.setAttribute( "myBoolVar", new Boolean( logicalValue ) );
getServletContext().getRequestDispatcher( jspName ).forward( request, response );

and jsp page:


<% Boolean inputLogicalVar = (Boolean)request.getAttribute("myBoolVar");
boolean bool1 = false; // init due to possible null from servlet if ( inputLogicalVar != null ) { bool1 = inputLogicalVar.booleanValue(); }%>


this everything is fine, but now I have new problem with that (with mine idea):
when RequestDispatcher triggers, an error runs

Maybe,(my idea: user log-in is through index.jsp, request is sent in a servlet where he do processing - if user exit create session and go somewere else, else set this boolean variable, and sent it back on index.jsp, so that user can try again, but because this boolean variable is set to true, jsp must print out "there is no such user, please try again" ), so, is there another way that I could do that (note: I must have this servlet because I call web services operation in it - to validate user)

the task seems simple, but I cant figure out
(note: everything is working except request dispatches back forwarding to index.jsp)

my index.jsp



Thanks on your help,
Marko
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Glad you found setAttribute instead of setParameter. My bad.

"javax.servlet.ServletException: WEB2651: Exceeded maximum depth for nested request dispatches: 20 ... " makes me think of unterminated recursion. Did the servlet forward to itself? Or did you forward 20 times on purpose?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to call Jsp in servlet
 
Similar Threads
jsp error page from servlet
jsp
JSP to Servlet call
dispatching to servlets and JSPs
jsp error page from servlet