日期:2014-05-17 浏览次数:20646 次
package com.bstore.Controller;基于MVC 设计模式, 比如让用户注册成功后 ,跳转到全局消息页面message.jsp 显示什么.. "恭喜你注册成功",几秒后跳到首页.., 我现在就是显示完消息之后弄不回首页, 郁闷00, 新手入门者,请教各位朋友前辈仁兄。。 怎么弄,百度google了,还是找不到。。
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.bstore.domain.RegisterForm;
import com.bstore.domain.User;
import com.bstore.exception.UserExistException;
import com.bstore.serviceImpl.ServiceImpl;
import com.bstore.util.WebUtils;
public class DealRegisterServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//首先设定好编码!!!!!!
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
//1.对提交的表单数据进行合法性检验(把表单的数据封装到formbean)
RegisterForm form= WebUtils.request2Bean(request, RegisterForm.class);
boolean b=form.validate();
//2.如果检验失败,回到表单页面,并且回显原因信息
if(!b){
request.setAttribute("form", form);
request.getRequestDispatcher("/WEB-INF/jsp/registerPage.jsp").forward(request,response);
return;
}
//3.如果检验成功,则调用mode层来处理注册请求
User user= new User();
WebUtils.copyBean(form, user);
//4.
ServiceImpl service= new ServiceImpl();
try {
service.register(user);//跳转不成功。。????
request.setAttribute("message","注册成功,浏览器将在3秒后跳转到首页!<meta http-equiv='refresh' content='3';url=/WebBookStore/WebRoot/WEB-INF/jsp/loginPage.jsp'>");
request.getRequestDispatcher("/message.jsp").forward(request, response);
return;
} catch ( UserExistException e){
//5.如果mode层处理不成功,并且原因是其他问题,则返回
form.getErrors().put("userName", "非常抱歉,您注册的用户名已存在!");
request.setAttribute("form",form);
request.getRequestDispatcher("/WEB-INF/jsp/registerPage.jsp").forward(request, response);
return;
}
catch (Exception e){
e.printStackTrace();
request.setAttribute("message", "服务器出现未知错误!");
request.getRequestDispatcher("/message.jsp").forward(request, response);
return;
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
//response.setContentType("text/html");
}
}