Win a copy of Rust Web Development this week in the Other Languages forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

how to call Jsp in servlet

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am not able to find to call an JSP form an Servlet.
want code for this.
 
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks stan,
My question was like that only..
instead of forward can I use include..
 
Ranch Hand
Posts: 3851
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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???
 
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 3851
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
WHAT is your favorite color? Blue, no yellow, ahhhhhhh! Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic