File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to call Jsp in servlet

 
Nitin Jain
Greenhorn
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am not able to find to call an JSP form an Servlet.
want code for this.
 
Eddy Lee Sin Ti
Ranch Hand
Posts: 135
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Nitin Jain
Greenhorn
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks stan,
My question was like that only..
instead of forward can I use include..
 
ankur rathi
Ranch Hand
Posts: 3830
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 121
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 28
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3830
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 121
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8791
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic