Jsp 提交响应后无法转发
我正试图通过设置属性从Servlet转发到JSP,但出现以下异常: java.lang.IllegalStateException:在响应结束后无法转发 犯下 请帮忙 我的代码:Jsp 提交响应后无法转发,jsp,servlets,jdbc,Jsp,Servlets,Jdbc,我正试图通过设置属性从Servlet转发到JSP,但出现以下异常: java.lang.IllegalStateException:在响应结束后无法转发 犯下 请帮忙 我的代码: public class setPassword extends HttpServlet { protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws Servlet
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();
}
}
返回
response.setContentType(“text/html;charset=UTF-8”)
和PrintWriter out=response.getWriter()代码>仅在不转发的分支内
工作代码。。。。我已经指定了我在上述代码中进行更改的区域 公共类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();
}
}
请粘贴输出如果请求在循环终止前转发一次,则即使在转发请求后,循环仍将继续,该请求将尝试再次从另一个(或可能是相同的,取决于对其中一个条件的评估)
if-else
块,导致抛出笨拙的java.lang.IllegalStateException
(这似乎不寻常)。您可能希望适当地使用break
语句或if else if
梯形图中的标志变量来指示身份验证的状态,并使用该变量在循环外转发请求。如果您正在检查用户身份验证,则可以使用带有prepare语句的单个查询,如select*从user_表,其中user_email=?和user_password=?
,然后如果(rs.next()){//用户凭据正确。因此重定向到主页}否则{//显示指示凭据错误的消息}
。无需与循环共舞:)感谢您提出的“准备声明”想法………仅添加return不起作用,但我删除了“response.setContentType(“text/html;charset=UTF-8”);”并添加了return;效果很好。