• 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
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

problem with RequestDispatcher

 
Ranch Hand
Posts: 360
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
What's wrong with RequestDispatcher in the below code ?
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException
{
response.setHeader("Cache-Control","no-cache");
response.setHeader("Pragma","no-cache");
PrintWriter out = response.getWriter();

String LASTNAME = request.getParameter("lastname");
String FIRSTNAME = request.getParameter("firstname");
String EMAIL = request.getParameter("email");
String LANID = request.getParameter("lanid");

out.println("connecting to database....");
String dbURL = "jdbc dbc:Review";
String user = "dbo";
String password = "dbo";
String SQL;
String userid;
userid = null;
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection CONN = DriverManager.getConnection(dbURL,user,password);
Statement STAT = CONN.createStatement();
SQL = "select user_id from tuser where user_last_nm = '" + LASTNAME + "'"
+ " and user_first_nm = '" + FIRSTNAME + "'"
+ " and EMAIL_ADDRESS_TX = '" + EMAIL + "'"
+ " and USER_LAN_ID = '" + LANID + "'";
out.println(SQL);

ResultSet RS = STAT.executeQuery(SQL);
while (RS.next()) {
userid = RS.getString("user_id");
}
}
catch (Exception e) {
e.printStackTrace();
}

if (userid.length() > 0) {
out.println("forwarding....");
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("http://localhost:8080/examples/JSP/Menu.jsp");
dispatcher.forward(request,response);
}

}
[This message has been edited by Dilipkumar Kalyankar (edited November 09, 2000).]
 
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Dileep,
If you see the Servlet API for the method getRequestDispatcher, it goes as follows
----------------------------------------------------------
getRequestDispatcher
public RequestDispatcher getRequestDispatcher(java.lang.String path)
Returns a RequestDispatcher object that acts as a wrapper for the resource located at the given path. A RequestDispatcher object can be used to forward a request to the resource or to include the resource in a response. The resource can be dynamic or static.
The pathname must begin with a "/" and is interpreted as relative to the current context root. Use getContext to obtain a RequestDispatcher for resources in foreign contexts. This method returns null if the ServletContext cannot return a RequestDispatcher.
Parameters:
path - a String specifying the pathname to the resource
Returns:
a RequestDispatcher object that acts as a wrapper for the resource at the specified path
See Also:
RequestDispatcher, getContext(java.lang.String)
-------------------------------------------------------------
Found the problem in your code? It should be relative path, not absolute path according to specification. Use it like this
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/examples/JSP/Menu.jsp");


------------------
Prasad
 
Dilip kumar
Ranch Hand
Posts: 360
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Prasad,
Thank you for explaination.
I changed the code to
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/examples/jsp/Menu.jsp");
dispatcher.forward(request,response);
But problem is not yest solved. For some reason it's not loading Menu.jsp though I have this file in C:\jakarta-tomcat\webapps\examples\jsp
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Dilip,
Since you are already adding some content to your response object, try using
dispatcher.include(request,response);
This will append the content of jsp file to your existing response.
 
author
Posts: 621
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not sure why you're using the requestDispatcher in the first place. You should only really use it if you want to pass the original request and response objects. I realise that you're setting the "NoCache" pragma, but that would only be neccessary if you actually delivered content from this Servlet. Correct me if I'm wrong, but this situation looks to be a simple redirect(especially since you are redirecting to a jsp page, not a servlet). Why not use
response.sendRedirect("someurl.com");
Hope this helps.
Sean
 
Dilip kumar
Ranch Hand
Posts: 360
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
As Sean mentioned I need simple redirect ( like reponse.redirect in ASP ).
I changed the code to
if (userid.length() > 0) {
out.println("forwarding....");
response.sendRedirect("http://localhost:8080/examples/JSP/Menu.jsp");
}
Address field in IE is changing to http://localhost:8080/examples/JSP/Menu.jsp
But the application is crashing immediately.
jasper.log
------------
Scratch dir for the JSP engine is: C:\jakarta-tomcat\work\localhost_8080%2Fadmin</JASPER_LOG>
IMPORTANT: Do not modify the generated servlets</JASPER_LOG>
JspEngine --> /jsp/logon.jsp</JASPER_LOG>
ServletPath: /jsp/logon.jsp</JASPER_LOG>
PathInfo: null</JASPER_LOG>
RealPath: C:\jakarta-tomcat\webapps\examples\jsp\logon.jsp</JASPER_LOG>
RequestURI: /examples/jsp/logon.jsp</JASPER_LOG>
QueryString: null</JASPER_LOG>
Request Params: </JASPER_LOG>
Classpath according to the Servlet Engine is: C:\jakarta-tomcat\webapps\examples\WEB-INF\classes</JASPER_LOG>
JspEngine --> /JSP/Menu.jsp</JASPER_LOG>
ServletPath: /JSP/Menu.jsp</JASPER_LOG>
PathInfo: null</JASPER_LOG>
RealPath: C:\jakarta-tomcat\webapps\examples\JSP\Menu.jsp</JASPER_LOG>
RequestURI: /examples/JSP/Menu.jsp</JASPER_LOG>
QueryString: null</JASPER_LOG>
Request Params: </JASPER_LOG>
Classpath according to the Servlet Engine is: C:\jakarta-tomcat\webapps\examples\WEB-INF\classes</JASPER_LOG>
servlet.log
-----------
Context log path="/admin" :tomcat.errorPage: init
Context log path="/admin" :jsp: init
Context log path="/admin" :default: init
Context log path="/examples" :tomcat.errorPage: init
Context log path="/examples" :jsp: init
Context log path="/examples" :default: init
Context log path="/test" :tomcat.errorPage: init
Context log path="/test" :jsp: init
Context log path="/test" :default: init
Context log path="" :tomcat.errorPage: init
Context log path="" :jsp: init
Context log path="" :default: init
Context log path="/examples" :invoker: init
Context log path="/examples" :VerifyUser: init
tomcat.log
-------------
Context log: path="" File not found C:\jakarta-tomcat\webapps\ROOT\WEB-INF\web.xml, using only defaults
Starting endpoint port="8080" handler="org.apache.tomcat.service.http.HttpConnectionHandler"
Starting endpoint port="8007" handler="org.apache.tomcat.service.connector.Ajp12ConnectionHandler"
 
Sean MacLean
author
Posts: 621
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hmmmn. Looks like the problem is on the server/jsp side of things, not the servlet doing the redirect. I assume the Menu.jsp works fine when you access it normally. And if so, what url do you use to hit the Menu page?
Sean
 
reply
    Bookmark Topic Watch Topic
  • New Topic