需要引入接口
ServletResponseAware
public class Login extends ActionSupport implements SessionAware,ServletResponseAware
{ private HttpServletResponse response = null;
....
public void setServletResponse(HttpServletResponse response) {
// TODO Auto-generated method stub
this.response = response;
} public String execute()
{
Person person = new Person();
PersonDAO personDAO = new PersonDAO();
if(personDAO.findById(id)==null)
{
System.out.println("用户不存在");
try {
response.setContentType("text/html; charset=UTF-8");
response.setCharacterEncoding("UTF-8"); //防止乱码;
PrintWriter out = response.getWriter();
out.print("<Script>alert('用户不存在!')</Script>");
out.flush();
out.close();
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
....
}
}
不过很多网页,像淘宝之类的,他们的页面出错不是这么处理的,这种方法弹出提示框以后不能再返回页面