目前,在大多数Browser/Server结构的WEB应用中,,在中间层上采用基于模型视图控制器(MVC.Model-View-Controller)的设计模式。Model层用来实现业务逻辑,View层用来显示用户界面,Controller层主要负责View层和Model层之间的控制关系,具体实现时,把Servlet用作应用程序的控制器,把JSP文档作为前台视图,JavaBeans被用来表示模型。
给一个实现注册功能的mvc具体例子:
View(视图层)Register.jsp
核心实现代码:Controller层,即Servlet。实现代码:UserRegServlet.java
核心代码:- protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- request.setCharacterEncoding("GBK");
- response.setCharacterEncoding("GBK");
- HttpSession session = request.getSession();
- String str = (String)session.getAttribute("random");
- String yanzhnegcode=request.getParameter("textcode");
- String user_name=request.getParameter("textusername");
- String user_password=request.getParameter("textpassword");
- String user_password_question=request.getParameter("textpasswordquestion");
- String user_password_answer=request.getParameter("textpasswordanswer");
- String user_nickname=request.getParameter("textnickname");
- String user_sex=request.getParameter("textsex");
- String user_edu=request.getParameter("textedu");
- String user_email=request.getParameter("textemail");
- String user_regtime=getFormatedDateTime(dt);
- UserDataBean regdata=new UserDataBean();
- UserBean userreg=new UserBean();
- regdata.setUser_name(user_name);
- regdata.setUser_password(user_password);
- regdata.setUser_password_question(user_password_question);
- regdata.setUser_password_answer(user_password_answer);
- regdata.setUser_nickname(user_nickname);
- regdata.setUser_sex(user_sex);
- regdata.setUser_edu(user_edu);
- regdata.setUser_email(user_email);
- regdata.setUser_regtime(user_regtime);
- PrintWriter out = response.getWriter();
- if(yanzhnegcode.equals(str)){
- boolean result1=userreg.checkreg(regdata);
- if(result1==true){
- out.print("<script>window.location.href='register/RegFailed.jsp';</script>");
- }
- else
- {
- boolean result2=userreg.adduser(regdata);
- if(result2==true)
- {
- out.print("<script>window.location.href='register/RegSuccess.jsp';</script>");
- }
- else
- {
- out.print("<script>window.location.href='register/RegFailed.jsp';</script>");
- }
- }
- }
- else{
- out.print("<script>alert('您的验证码输入错误!请重新输入!');window.location.href='javascript:history.go(-1);';</script>");
- }
- }
- }
复制代码 Model层,即javabean,实现代码:UserBean.java
UserBean.java- public boolean adduser(UserDataBean regdata)
- {
- ConDB db=new ConDB();
- String user_name=regdata.getUser_name();
- .................................
- String user_regtime=regdata.getUser_regtime();
- String sql="insert into bluerain_user(user_name,user_password,user_password_question,user_password_answer,user_nickname,user_sex,user_edu,user_email,user_phone,user_qq,user_regtime) values('"+user_name+"','"+user_password+"','"+user_password_question+"','"+user_password_answer+"','"+user_nickname+"','"+user_sex+"','"+user_edu+"','"+user_email+"','"+user_phone+"','"+user_qq+"','"+user_regtime+"')";
- return db.executeInsert(sql);
- }
- public boolean checkreg(UserDataBean regdata)
- {
- boolean flag = false;
- ConDB db=new ConDB();
- String user_name=regdata.getUser_name();
- String user_nickname=regdata.getUser_nickname();
- String user_email=regdata.getUser_email();
- String sql="select * from bluerain_user where user_name='" + user_name + "' or user_nickname='" + user_nickname + "' or user_email='" + user_email + "'";
- flag = db.executeQuery1(sql);
- return flag;
- }
- public boolean checklogin(UserDataBean regdata)
- {
- boolean flag = false;
- ConDB db=new ConDB();
- String user_name=regdata.getUser_name();
- String user_password=regdata.getUser_password();
- String sql="select * from bluerain_user where user_name='" + user_name + "' and user_password='" + user_password + "'";
- flag = db.executeQuery1(sql);
- return flag;
- }
复制代码
|