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.