permaculture playing cards*
The moose likes Servlets and the fly likes calling html and jsp from Servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Servlets
Bookmark "calling html and jsp from Servlet" Watch "calling html and jsp from Servlet" New topic
Author

calling html and jsp from Servlet

s srikanth
Greenhorn

Joined: Jul 06, 2001
Posts: 14
Hi ,
how to call a html from a servlet ?
i dont want to write html code in the servlet.
i want to call the seperate html file.
also how can i call jsp from a servlet?
sample code is welcome .
thanx in advance ,
Srikanth
zb cong
Ranch Hand

Joined: Jan 14, 2002
Posts: 416
see follow:
getServletContext().getRequestDispatcher("/xxx.jsp or /xxx.html").forward(req,resp);
or.........................include(req,resp);
or resp.sendRedirect("xxx.jsp or xxx.html");
the three method all can implement your requirement,but the forward(....) and include(....) method run in serverside,the sendRedirect() method send the response to your browser to redirect to other page
s srikanth
Greenhorn

Joined: Jul 06, 2001
Posts: 14
Thanx cong
Tazzmission
Greenhorn

Joined: Jan 16, 2002
Posts: 20
Can you pls tell me what is the difference between 'forward' and 'include'? I read the doc but i dont understand

Originally posted by zb cong:
see follow:
getServletContext().getRequestDispatcher("/xxx.jsp or /xxx.html").forward(req,resp);
or.........................include(req,resp);
or resp.sendRedirect("xxx.jsp or xxx.html");
the three method all can implement your requirement,but the forward(....) and include(....) method run in serverside,the sendRedirect() method send the response to your browser to redirect to other page
Roy Ben Ami
Ranch Hand

Joined: Jan 13, 2002
Posts: 732
with the forward command you simply pass on the request and the response to another servlets or jsp to take care of.
with the include command you include the servlets or jsp output in your page after it has done its stuff.
basically most of the time you get the same result, its only semantics.
most of time you use include so you can embedd a lot of servlets or jsp together.
you must remember that if u use forward you ae not allowed to output anything to the client before you call forward (meaning you can't use the response in your servlets if u use forward).
Tazzmission
Greenhorn

Joined: Jan 16, 2002
Posts: 20
Thanks.
So 'forward' just pass the control to another Servlet? (Also, pass all the request parameter of from original servlet to another servlet too?)
And it seems that include is more flexible?
Thank You.
Roy Ben Ami
Ranch Hand

Joined: Jan 13, 2002
Posts: 732
correct
btw, in jsp you have two kind of include.
you can use the include attribute
<% @include
or the include tag :
<jsp:include
the difference is that the first one is included in compile time (only once) while the second is at runtime when the page is loaded, so it is even more flexible.
hope it helped to clear this stuff a bit
Tazzmission
Greenhorn

Joined: Jan 16, 2002
Posts: 20
Thanks very much.
I will try them later when I've time.
 
GeeCON Prague 2014
 
subject: calling html and jsp from Servlet