不会从servlet中的init()继续到doGET()

时间:2013-12-20 13:51:40

标签: java servlets

我正在编写一个管理产品的网络应用程序(用于学校)。设计如下:

在我用servlet检查登录后,我转发到另一个名为ManageMainPageServlet的servlet,其中doGet a)发布html,其中包含两个用于搜索产品的表单,以及b)将产品插入数据库。

填写第一个表单(搜索产品表单)后,它将传递给同一个servlet中的post方法,后者又将其重定向到SearchProduct servlet。

我在那里定义了init()来注册所有用于连接数据库的变量(我已经测试并运行)。

在init(我知道第一次只运行一次)后,我假设代码转到doGet方法,开始管理查询。这对我不起作用,它会返回到ManageMainPageServlet。

我尝试使用dispatcher.forward,我现在写的是会发生什么,如果我尝试将其更改为response.redirect,它甚至不会进入SearchProduct中的init()函数。

我缺少什么?

这是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);
    }

这是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();
           }
       }
    }
}

0 个答案:

没有答案