1

I am calling a Servlet using its URL address. This is the URL I am typing

http://localhost:7001/ryan/olympics?action=selectCatalog&id=1

This is the Servlet's URL for sure; if I change the address I get

page not found

This is the code for the Servlet.

public class Servlet extends javax.servlet.http.HttpServlet
        implements javax.servlet.Servlet {

private static final long serialVersionUID = 1L;

public Servlet() {
    super();
}

public void init(ServletConfig config) throws ServletException {
    System.out.println("*** initializing controller servlet.");
    super.init(config);
}

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


String action = request.getParameter("action");



if (action.equals("selectCatalog")) {
      String categoryId = request.getParameter("id");

      ProductModelDAO dao4 = new ProductModelDAOImpl("jpac");
      if (categoryId != null && !categoryId.trim().equals("")) {
          CategoryDAO dao1 = new CategoryDAOImpl("jpac");

    try {
        Category category = dao1.getCategoryName(categoryId);
        request.setAttribute("category", category);
        } catch (Exception e) {
        e.printStackTrace();
        }
    }

    try {
        @SuppressWarnings("unchecked")
        List<Product> products = dao4
            .getProductsByCategory(categoryId);
        request.setAttribute("products", products);
    } catch (Exception e) {
        e.printStackTrace();
    }

    url = "SelectCatalog.jsp";

 RequestDispatcher requestDispatcher =
  getServletContext().getRequestDispatcher(url);
requestDispatcher.forward(request, response);

I get the NullPointerException pointing to the RequestDispatcher's line. Any help?

Makoto
  • 104,088
  • 27
  • 192
  • 230
RonaDona
  • 912
  • 6
  • 13
  • 30

3 Answers3

3

Try changing "SelectCatalog.jsp" to "/SelectCatalog.jsp", because, as I understand, You want to use an absolute path.

CAMOBAP
  • 5,523
  • 8
  • 58
  • 93
0

If you want to use a relative path you have to use:

request.getRequestDispatcher(url);

in place of:

getServletContext().getRequestDispatcher(url);
thedayofcondor
  • 3,860
  • 1
  • 19
  • 28
0

request.getParameter("action");

code is written in doPost method. Are you invoking this servlet from doPost method of calling servlet? URL parameters will not be used by doPost method.