sometimes have submitted dozens of times without error . Sometimes submitted once an error.
Code:
servlet
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//String path = req.getContextPath() + "/";
String basePath = req.getScheme() + "://" + req.getServerName() + ":" + req.getServerPort();
/** 登录 */
String username = req.getParameter("username");
String password = req.getParameter("password");
String yzcode = req.getParameter("yzcode");
if ("".equals(username) || username == null || "".equals(password) || password == null) {
req.getSession().setAttribute("error", "用户名或密码不能为空!");
//req.getRequestDispatcher("/login.jsp").forward(req, resp);
resp.sendRedirect(basePath + "/login.html");
return;
}
jsp:
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
String error = (String) request.getSession().getAttribute("error");
String show = "";
if (error == null)
error = " ";
else
show = "khkmsg.show('errorContent','"+error+"',false)";
request.getSession().removeAttribute("error");
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
...(以下都是HTML标签了)
first submission , and can display an error message , and the second directly error. Tried forward, the same effect , do not understand how it is, ask experts to answer 12. .
submitted about 10 times with a forward only after an error.
------ Solution ------------------------------------ --------
Cannot call sendRedirect () after the response has been committed
message is very clear that if the response has already submitted , and it can not be sent sendRedirect it.
Because redirection, in fact, HTTP-302, if you have previously written data , then the default is HTTP-200, browsers have received HTTP-Head information , and no opportunity to do redirected.
So , during :
resp.sendRedirect ()
, you must first ensure that no output , including:
1, Cookie;
2, resp.getWriter () to write text messages ;
3, has sent a redirect .
focus on examination , these issues now , if you write a Filter, or Servlet has been done forward, we should pay attention to see.
------ Solution ---------------------------------------- ----
note that :
same thread , response can not step twice responding to client
------ For reference only ---------------------- -----------------
I know ah. You say these I have to pay attention. Filter is only a pseudo-static , but I put the pseudo-static cancel , the same error.
strange that process is not always an error. Seemingly random.
jsp page is submitted to the servlet, servlet good judgment and returned to the same jsp page . Process is like this , but many times after the submission of an error occurs.
trouble to help further analysis chant. ~ ~ To cry it. . .
------ For reference only -------------------------------------- -
find the problem.
is Kaptcha this verification code control out of the ghost . .
how it will conflict .
------ For reference only -------------------------------------- -
find the problem.
is Kaptcha this verification code control out of the ghost . .
how it will conflict .
------ For reference only -------------------------------------- -
Research on Kaptcha nothing . . . Sorry, can not help you . . .
------ For reference only -------------------------------------- -
/ / comment out the following code has no such mistake.
req.getSession (). setAttribute ("error", " user name or password can not be empty ! " ) ;
------ For reference only ---------------------------------- -----
没有评论:
发表评论