wood burning stoves 2.0*
The moose likes Servlets and the fly likes Calling JSP from Servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Calling JSP from Servlet" Watch "Calling JSP from Servlet" New topic
Author

Calling JSP from Servlet

Amit Lonkar
Greenhorn

Joined: Dec 12, 2001
Posts: 25
Hi
I am tring to call a JSP page from the servlet. I have written the follwing code. The request is not forwared to the JSP. and the control remains in the servlet.
try {
// Set the attribute and Forward to UserLogin.jsp
RequestDispatcher rd =
getServletConfig().getServletContext).getRequestDispatcher("/jsp/UserLogin.jsp");
// forward the request and response to target
rd.forward(request,response);
return;
} catch (Exception ex) {
System.out.println("Caught Exception in Sending jpspage im Main Servlet : " + ex.getMessage());
}
Manohar Karamballi
Ranch Hand

Joined: Jul 17, 2001
Posts: 227
Hai!
Try by removing getServletConfig() part..
Rgds
Manohar
Amit Lonkar
Greenhorn

Joined: Dec 12, 2001
Posts: 25
Originally posted by Manohar Karamballi:
Hai!
Try by removing getServletConfig() part..
Rgds
Manohar

Hi manohar
This still does not work.
Amit
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

What Exception is thrown (if any)?

sanj singh
Ranch Hand

Joined: Jun 30, 2001
Posts: 129
Hi Amit
I was just going through your code.Why have you written the return statement after the rd.forward(request,response); statement.Another thing.....I do not know if its a typing error on your part while sending the post or its an error in ur code...but your getServletContext() method is missing the starting bracket.As Mike said do you get any exceptions?
Regards
sanj
[This message has been edited by sanj singh (edited December 17, 2001).]
Jason Kilgrow
Ranch Hand

Joined: May 21, 2001
Posts: 47
Originally posted by Amit Lonkar:
Hi
I am tring to call a JSP page from the servlet. I have written the follwing code. The request is not forwared to the JSP. and the control remains in the servlet.

How about trying:
String dispatchto = "MyJsp.jsp";
RequestDispatcher dispatch = null;
response.encodeURL(dispatchto);
dispatch = request.getRequestDispatcher(dispatchTo);
dispatch.forward(request, response);
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Calling JSP from Servlet