在做登陆的时候出现这个错误:Cannot forward after response has been committed
请高手指教
代码如下:
package cn.com.blog.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import cn.com.blog.model.User;
import cn.com.blog.service.IUserService;
import cn.com.blog.service.ServiceFactory;
import cn.com.blog.util1.SessionUtil;
public class LoginAction extends HttpServlet {
protected static Log log = LogFactory.getLog("processLog");
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
try{
log.debug("接受用户请求!");
String username=request.getParameter("name");
String password=request.getParameter("password");
//把数据存储到持久化中对象
User user = new User();
user.setName(username);
user.setPassword(password);
//调用Service层接口的处理类处理
IUserService userService=ServiceFactory.getUserService();
//获得Session对象
HttpSession session=request.getSession();
//获得输出流对应的writer对象
PrintWriter out = response.getWriter();
if(userService.login(user)){
User u = userService.getUser(user.getName());
SessionUtil.saveUser( request, u );
this.getServletContext().getRequestDispatcher("/HomePageAction").forward(request,response);
}else{
out.println("<center><font color='red'>用户名或密码错误!</font><a href='#' onclick='history.back();'>返回</a></center>");
}
}
catch(Exception e){
e.printStackTrace();
log.debug("用户请求处理出错!");
request.setAttribute("errMsg","登录出错!");
this.getServletContext().getRequestDispatcher("/Blog/myerror.jsp").forward(request,response);
return;
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response);
}
}