0

I am writing a web app (for school) that manages products. The design is as follows:

After I checked login with a servlet, I'm forwarded to another servlet called ManageMainPageServlet, in which the doGet a) posts the html which consists of two forms for searching a product, and b) inserts the product into the database.

After the first form is filled in (the search product form), it is passed to the post method in the same servlet, which in turn redirects it to SearchProduct servlet.

I defined the init() there to register all the variables for connecting to the database (which I already tested and works).

After the init (which I know is run only once at first time) I presume the code goes to the doGet method, to start managing the query. This does not work for me, and it goes back to the ManageMainPageServlet.

I tried with a dispatcher.forward, and what I wrote right now is what happens, if I try to change it to response.redirect, it wont even get to the init() function in SearchProduct.

What am i missing?

This is code for ManageMainPageServlet:

@WebServlet(name = "ManageMainPageServlet", urlPatterns = {"/ManageMainPageServlet"})
public class ManageMainPageServlet extends HttpServlet {
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        HttpSession session = request.getSession(false);
        if(session==null || (Boolean)session.getAttribute("logged") == false)
            response.sendRedirect("index.html");
        out.println("<!DOCTYPE html>"+
                        "<html>\n"+
                        "<head><title>Welcome to Main Page</title></head>\n"+
                        "<body>\n"+
                        "<h1> Welcome to Main Page</h1>\n"+
                        "<form action=\"ManageMainPageServlet\" method=\"POST\">\n"+
                        " Enter product name and\\or product id to search:<br>\n"+
                        "Product Name: <input type=\"text\" name=\"productName\" /><br>\n"+
                        "Product ID:<input type=\"text\" name=\"productId\" /> <br>\n"+
                        " <input type=\"hidden\" name=\"direction\" value=\"/SearchProduct\">\n"+
                        " <input type=\"submit\" />\n"+
                        "</form>\n"+
                        " <form action=\"ManageMainPageServlet\" method=\"POST\">\n"+
                        "Fill in the fields to enter new product into DB.\n"+
                        " <br>Product Name: <input type=\"text\" name=\"productName\" />\n"+
                        " <br>Product ID: <input type=\"text\" name=\"productId\" />\n"+
                        "<br>Price:  <input type=\"text\" name=\"price\" />\n"+
                        "<br>Quantity: <input type=\"text\" name=\"productId\" />\n"+
                        " <br>Product Description: <br><textarea rows=\"4\" cols=\"50\"  name=\"description\"></textarea>\n"+
                        " <input type=\"hidden\"  name=\"direction\" value=\"\">\n"+
                        "  <br> <input type=\"submit\" />\n"+
                        "  </form>\n"+
                        " <form action=\"LogOutServlet\" method=\"GET\">\n"+
                        " <input type=\"submit\" value=\"logout\"/>\n"+
                        " </form>\n"+
                        "</body></html>");
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String direction = request.getParameter("direction");
        //response.sendRedirect(direction);
        RequestDispatcher rd = request.getRequestDispatcher(direction);
        rd.forward(request, response);
    }

This is code for SearchProduct servlet:

@WebServlet(name = "SearchProduct", urlPatterns = {"/SearchProduct"})
public class SearchProduct extends HttpServlet {
    private Connection con=null;
    private String url=null;
    private String user=null;
    private String password=null;
    private String odbcDriver=null;

    /**
     * initializes servlet with db connection and throws back error if the 
     * connection failed for some reason
     */
   @Override
   public void init(ServletConfig servletConfig) throws ServletException {
       try {
           this.url = servletConfig.getInitParameter("dbUrl");
           this.user = servletConfig.getInitParameter("user");
           this.odbcDriver = servletConfig.getInitParameter("odbcDriver");

           Class.forName(odbcDriver);

        } catch (Exception e) {
            //decide how to handle the exception
            System.out.println("Failed to load the driver");
            return;
        }

   }

    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        String productName = request.getParameter("productName");
        String productID = request.getParameter("productID");

        try {
            try {
                // Create the connection to the database    
                con = DriverManager.getConnection(url,user,password);
                System.out.println("Made a connection to the database");
            } catch (Exception e) {
                System.out.println("Exception was thrown:\n"); 
                e.printStackTrace();
            }
            // Get a statement object from the connection
            //
            Statement statement = con.createStatement();
            if(productID==null);

       } catch (Exception e) {
           System.out.println("Exception was thrown:\n"); 
           e.printStackTrace();
       } finally {
           try {
               if (con != null) { con.close(); }
           }catch (SQLException e) {
               e.printStackTrace();
           }
       }
    }
}
Pieter
  • 895
  • 11
  • 22
igal kreimer
  • 51
  • 1
  • 4
  • 1
    Wow, that's a lot of code. Can you porvide an [SSCCE](http://sscce.org/)? – Mureinik Dec 20 '13 at 14:04
  • Does this question help you? http://stackoverflow.com/questions/46582/response-redirect-with-post-instead-of-get – clay Dec 20 '13 at 14:25
  • 2
    Why do you presume control then goes to `doGet()`? `doGet()` is called when the servlet's mapped URL is requested via HTTP. `init()` is called at servlet load time. – Dave Newton Dec 20 '13 at 15:35

0 Answers0