This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes Servlets and the fly likes Forwarding Servlet output to JSP Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Forwarding Servlet output to JSP" Watch "Forwarding Servlet output to JSP" New topic
Author

Forwarding Servlet output to JSP

saahil sinha
Ranch Hand

Joined: Apr 07, 2003
Posts: 68
Hi,
I would like ot know how i could redirect output of a servlet to a jsp page
I am using this piece of code for the servlet
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
public class test extends HttpServlet
{

public void service(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException
{
req.setAttribute("message","hello");
RequestDispatcher rd;
rd=getServletContext().getRequestDispatcher("c:\\Program Files\\ApacheGroup\\Tomcat 4.1\\webapps\\ROOT\\display.jsp");
rd.forward(req,res);
}
}
and for the jsp i am using this code:
<html>
<body>
<%String msg=(String)request.getAttribute("message");%>
<%=msg%>
</body>
</html>
but the ouput of the servlet is not being forwarded to jsp and neither is the jsp page opening wht have i missed out in this code.
Chris Smith
Ranch Hand

Joined: May 03, 2002
Posts: 42
You've forgotten that getRequestDispatcher requires a context-relative URI, not an absolute file name. Include the path starting with the context root, using a forward slash as a file separator according to the standard syntax for URIs.
 
 
subject: Forwarding Servlet output to JSP