• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

getRequestDispatcher error in servlets

 
Ranch Hand
Posts: 119
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is the original servlet code that I am using
package com.example.web;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;


import com.example.model.*;

public class BeerSelect extends HttpServlet {

protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {


/* response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("Beer Selection Advice <br>"); */

String c = request.getParameter("color");

BeerExpert be = new BeerExpert();
List result = be.getBrands(c);

request.setAttribute("styles",result);

/* RequestDispatcher view = getServletContext().getRequestDispatcher ("/result.jsp");
view.forward(request,response); */

//Somehow this is giving a compiler error.So, I used the above and voila, it works!

RequestDispatcher rd = request.getRequestDispatcher("result.jsp");
rd.include(request,response);



}
}


If I use RequestDispatcher rd = request.getRequestDispatcher("result.jsp"); I am getting the following compiler error

C:\HeadFirstServlets\BeerV1>javac com/example/web/BeerSelect.java
com/example/web/BeerSelect.java:34: cannot resolve symbol
symbol : method getRequestDispatcher (java.lang.String)
location: interface javax.servlet.http.HttpServletRequest
RequestDispatcher rd = request.getRequestDispatcher("result.jsp");
^
1 error

The code works if I use
RequestDispatcher view = getServletContext().getRequestDispatcher("/result.jsp");

What am I missing ? I would really appreciate somebody's help ?
Thanks.
 
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If your code is working other way, just use that. though I tried both. i am not having any compiler error in either.
[ September 22, 2004: Message edited by: adeel ansari ]
 
Ranch Hand
Posts: 295
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi...

I was getting this error too, when i was working with this. I also heared from someone that it's better to use

This will hold good when u are sending the request parameters alone to the JSP.

Any way, i came across a difference in using both the methods

That is :

The servletRequest's getRequestDispatcher() can take a relative path while
ServletContext's getRequestDispatcher() can not(can only take relative to the
current context's root).

For example
with ServletContext both
-> request.getRequestDispatcher("./jsp/jsppage.jsp") - evaluated relative to the path of the request
-> request.getRequestDispatcher("/jsp/jsppage.jsp") - evaluated relative to the root are all valid

with ServletContext only
-> context.getRequestDispatcher("/jsp/jsppage.jsp") is valid
but not context.getRequestDispatcher("./jsp/jsppage.jsp").
that is it can not evaluate a path other than context root.

But i would like to know the pros and cons of the methods....

Cheers,
Swamy
 
Ritu varada
Ranch Hand
Posts: 119
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, people..For now , I will use the ServletContext but hopefully some day I will know why request.getRequestDispacther did not work for me. Thanks for replying!
 
Adeel Ansari
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the error, you are gettin, looks like a compile time error. It doesn't mean that it didn't work, but its like why are you getting compile time error. wiered.
[ September 23, 2004: Message edited by: adeel ansari ]
 
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

The difference between the getRequestDispatcher()method of ServletContext and that of ServletRequest is that you can pass a relative path to the getRequestDispatcher() method of ServletRequest but not to the getRequestDispatcher() method of ServletContext.

Like for instance request.getRequestDispatcher("../html/login.html") is valid,and the getRequestDispatcher() method of ServletRequest will evaluatethe path relative to the path of the request.

For the getRequestDispatcher() method of ServletContext, the path parameter cannot be relative and must start
with /.

Why like this... because ServletRequest has a current request path to evaluate the relative path while ServletContext does not.

Thanks
Chandrasekhar
SCJP
 
Adeel Ansari
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanx chandra. but we know this. the problem is, why getting compile time error. should not be like that
 
Chandra Sekhar
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

It compiled for me..with out giving any error...

i have set $TOMCAT_HOME/common/lib/servlet-api.jar in my classpath.
TOMCAT VERSION is jakarta-tomcat-5.0.18

If j2ee.jar is also in the classpath...it may give compile error.

Chandrasekhar S
SCJP
[ September 23, 2004: Message edited by: Chandra Sekhar ]
 
Ranch Hand
Posts: 157
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The getRequestDispatcher(String path) method in ServletRequest was introduced in Java Servlet API 2.2

You might be compiling your servlet using an older version of the Servlet API.

Sheldon Fernandes
 
Ritu varada
Ranch Hand
Posts: 119
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, Chandra. I removed a servlet.jar in the classpath and had only the servlet-api.jar in there and it worked! Guess, somethings were clashing!
Thank you everybody for your help!
 
I am mighty! And this is a mighty small ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic