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
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
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
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).
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.
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