在提交响应后无法转发

时间:2014-02-11 17:12:03

标签: jsp servlets jdbc

我试图沿着设置属性从Servlet转发到JSP,但是我得到以下异常:

  

java.lang.IllegalStateException:响应后无法转发   已经承诺

请帮忙。

我的代码:

public class setPassword extends HttpServlet {

  protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException, ClassNotFoundException, SQLException {
    response.setContentType("text/html;charset=UTF-8");
    PrintWriter out = response.getWriter();

    Connection co;
    try {
        Class.forName("com.mysql.jdbc.Driver");
        co = DriverManager.getConnection("jdbc:mysql://localhost:3306/gift", "root", "");
        Statement st = co.createStatement();
        System.out.println("setpassword...........");

        ResultSet rs = st.executeQuery("select * from generaldata");

        System.out.println("setpassword...........111");
        String userid = request.getParameter("username");

        while (rs.next()) {
            String us = rs.getString(2);
            String ps = rs.getString(3);
            System.out.println("setpassword...........222");

            System.out.println(us);
            System.out.println(userid);

            if (userid.equals(us)) {

                System.out.println(ps);
                System.out.println("setpassword...........333");

                if (ps.equals("")) {
                    String pass = request.getParameter("password");
                    String pass1 = request.getParameter("password1");
                    System.out.println("setpassword...........444");

                    if (pass.equals(pass1)) {
                        rs.updateNString(3, pass);
                        RequestDispatcher rdpassword = request.getRequestDispatcher("home.jsp");
                        rdpassword.forward(request, response);
                        System.out.println("setpassword...........555");
                    } else {
                        request.setAttribute("errorPassword", "Your Password does not match...!!!");
                        RequestDispatcher rdlogin = request.getRequestDispatcher("newuser.jsp");
                        rdlogin.forward(request, response);
                        System.out.println("setpassword...........666");
                    }
                } else {
                    request.setAttribute("err", "Please note you are not a New User...!!!");
                    System.out.println("setpassword.................777");
                    RequestDispatcher rdNotNew = request.getRequestDispatcher("login.jsp");
                    rdNotNew.forward(request, response);

                }
            } else {
                request.setAttribute("err", "Not a valid User...!!!");
                System.out.println("setpassword.................788");
                RequestDispatcher rdNotValid = request.getRequestDispatcher("login.jsp");
                rdNotValid.forward(request, response);
            }
            System.out.println("setpassword.................888");
        }
        System.out.println("setpassword.................999");

        rs.close();
        st.close();
        co.close();

    } catch (Exception e) {
        System.out.println(e);
    } finally {
        out.close();
    }
}

2 个答案:

答案 0 :(得分:0)

  1. 你应该在前方后立即加return

  2. 您应该将response.setContentType("text/html;charset=UTF-8");PrintWriter out = response.getWriter();仅放在您未转发的分支机构内。

  3. 您需要找到一种在转发前关闭数据库连接的方法。

答案 1 :(得分:0)

工作代码....我已经指定了我在上面代码中进行更改的区域

public class setPassword扩展了HttpServlet {

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException, ClassNotFoundException, SQLException {
   **// response.setContentType("text/html;charset=UTF-8");**
    PrintWriter out = response.getWriter();

    Connection co;
    try {
        Class.forName("com.mysql.jdbc.Driver");
        co = DriverManager.getConnection("jdbc:mysql://localhost:3306/gift", "root", "");
        Statement st = co.createStatement();
        System.out.println("setpassword...........");

        ResultSet rs = st.executeQuery("select * from generaldata");

        System.out.println("setpassword...........111");
        String userid = request.getParameter("username");
        **int count = 0;**

        while (rs.next()) {
            String us = rs.getString(2);
            String ps = rs.getString(3);
            System.out.println("setpassword...........222");

            System.out.println(us);
            System.out.println(userid);

            if (userid.equals(us)) {

                **count++;**

                System.out.println(ps);
                System.out.println("setpassword...........333");

                if (ps.equals("")) {
                    String pass = request.getParameter("password");
                    String pass1 = request.getParameter("password1");
                    System.out.println("setpassword...........444");

                    if (pass.equals(pass1)) {
                        rs.updateNString(3, pass);
                        RequestDispatcher rdpassword = request.getRequestDispatcher("home.jsp");
                        rdpassword.forward(request, response);
                        System.out.println("setpassword...........555");
                    } else {
                        request.setAttribute("errorPassword", "Your Password does not match...!!!");
                        RequestDispatcher rdlogin = request.getRequestDispatcher("newuser.jsp");
                        rdlogin.forward(request, response);
                        System.out.println("setpassword...........666");
                        **return;**
                    }
                } else {
                    request.setAttribute("error", "Please note you are not a New User...!!!");
                    System.out.println("setpassword.................777");
                    RequestDispatcher rdNotNew = request.getRequestDispatcher("login.jsp");
                    rdNotNew.forward(request, response);
                    **return;**

                }
            }
            **else{
                continue;
            }**
            System.out.println("setpassword.................888");
        }

         **if(count == 0) {
                request.setAttribute("error", "Not a valid User...!!!");
                System.out.println("setpassword.................788");
                RequestDispatcher rdNotValid = request.getRequestDispatcher("login.jsp");
                rdNotValid.forward(request, response);
                return;
            }**

        System.out.println("setpassword.................999");

        rs.close();
        st.close();
        co.close();

    } catch (Exception e) {
        System.out.println(e);
    } finally {
        out.close();
    }
}