JSP+JAVABEAN+SERVLET模式的注册实例实现

本文涉及的产品
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
RDS MySQL Serverless 高可用系列,价值2615元额度,1个月
简介:

MVC模式即Model-View-Controller模式。

Servlet用来处理请求的事务,充当控制器(Controller即C)的角色,Servlet负责响应用户对业务逻辑的请求并根据用户的请求行为,决定将哪个JSP页面发送给客户。

JSP页面处于表现层,也就是视力(View即V)的角色。

JavaBean则负责数据的处理,也就是模型(Model即M)的角色。

初始的请求由Servlet来处理,Servlet调用商业逻辑和数据处理代码,并创建Bean来表示相应的结果(模型)。然后Servlet确定哪个页面适合于表达这些特定的结果,并将请求转发到相应的页面(JSP页面即为视图),由Servlet确定哪个业务逻辑适用,应该用哪个JSP页面相应结果(Servlet就是控制器)。

 

 

注册实例:

配置文件配置SERVLET的调用

/PetStore/WebRoot/WEB-INF/web.xml

 

 
  1. <servlet>  
  2. <servlet-name>reg</servlet-name>  
  3. <servlet-class>cn.wy.CtrlServlet.reg</servlet-class>  
  4. </servlet>  
  5. <servlet-mapping>  
  6. <servlet-name>reg</servlet-name>  
  7. <url-pattern>/reg</url-pattern>  
  8. </servlet-mapping> 

前端jsp页面

/PetStore/WebRoot/register.jsp

 

 
  1. 代码  
  2.  
  3. <%@ page contentType="text/html; charset=gb2312" language="java" import="cn.wy.Pet.User" errorPage="" %> 
  4. <jsp:useBean id="user" scope="page" class="cn.wy.Pet.User"/> 
  5. <%   
  6. //权限默认不是超级管理员  
  7. boolean isAdmin = false ;  
  8. String actionStr = "";  
  9. if (session.getAttribute("user") != null)  
  10.     {  
  11.         user = (User)session.getAttribute("user");  
  12.         //取出权限  
  13.         if (user.getUPopedom() ==0)  
  14.             isAdmin = true ;  
  15.     }  
  16.     String qFile = request.getRequestURI();  
  17.     System.out.println(qFile);  
  18.     //说明是超级管理员,并在后台添加用户  
  19.     if ("/admin/default.jsp".equals(qFile))  
  20.     {  
  21.         actionStr = "../" ;  
  22.     }  
  23.       
  24. %> 
  25. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
  26. <html xmlns="http://www.w3.org/1999/xhtml"> 
  27. <head> 
  28. <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> 
  29. <title>宠物商店会员注册</title> 
  30. <style type="text/css"> 
  31. <!--  
  32. .STYLE1 {  
  33.     color: #FF0000;  
  34.     font-weight: bold;  
  35. }  
  36. .STYLE2 {color: #FF0000}  
  37. .STYLE3 {  
  38.     font-size: 18px;  
  39.     font-weight: bold;  
  40. }  
  41. --> 
  42. </style> 
  43. </head> 
  44.  
  45. <body style="font-size:12px"> 
  46. <form id="form1" name="form1" method="post" action="<%=actionStr%>reg"> 
  47.   <p align="center"><br /> 
  48.     <span class="STYLE3">用户注册</span></p> 
  49.   <table width="582" height="302" border="1" align="center" cellpadding="0" cellspacing="0" bordercolor="#BCACD2"> 
  50.     <tr> 
  51.       <td width="80" align="right">用户名:</td> 
  52.       <td width="496" align="left"><input name="userName" type="text" id="userName" size="16" maxlength="16" /> 
  53.       <span class="STYLE1">*</span> 3~16位字母或者数字(如:8hack)</td> 
  54.     </tr> 
  55.     <tr> 
  56.       <td align="right">密码:</td> 
  57.       <td align="left"><input name="password1" type="text" id="password1" size="16" maxlength="16" /> 
  58.       <span class="STYLE1">*  </span> 3~16位字母或者数字(如:abc123)</td> 
  59.     </tr> 
  60.     <tr> 
  61.       <td align="right">确认密码:</td> 
  62.       <td align="left"><input name="password2" type="text" id="password2" size="16" maxlength="16" /> 
  63.       <span class="STYLE1">*</span> 必须和上面输入的密码相同</td> 
  64.     </tr> 
  65.     <tr> 
  66.       <td align="right">电子邮件:</td> 
  67.       <td align="left"><input name="email" type="text" id="email" maxlength="20" /> 
  68.       <span class="STYLE1">*</span> 找回密码和联系用(如:8hack@163.com)</td> 
  69.     </tr> 
  70.     <tr> 
  71.       <td align="right">联系电话:</td> 
  72.       <td align="left"><input name="tel" type="text" id="tel" size="20" maxlength="20" /> 
  73.       如(0871-8888888,13888853113)</td> 
  74.     </tr> 
  75.     <tr> 
  76.       <td align="right">联系地址:</td> 
  77.       <td align="left"><input name="address" type="text" id="address" maxlength="50" /></td> 
  78.     </tr> 
  79.         <%  
  80.         if (isAdmin)//如果是管理员就显示出下拉列表  
  81.         {  
  82.         %> 
  83.     <tr> 
  84.       <td height="26" align="right">权限:</td> 
  85.       <td align="left"> 
  86.         <select name="popedom" id="popedom"> 
  87.             <option value="0">超级管理员</option>          
  88.             <option value="1">会员</option> 
  89.         </select> 
  90.     </td> 
  91.     </tr> 
  92.         <%  
  93.         }else//否则就插入一个蕴藏域  
  94.         {  
  95.         %> 
  96.         <input type="hidden" name="popedom" value="1"> 
  97.         <%  
  98.         }  
  99.         %> 
  100.  
  101.     <tr> 
  102.       <td height="42" align="right">&nbsp;</td> 
  103.       <td align="left"><span class="STYLE2">为了方便联系和管理请认真填写你的信息,带星号的必须填写</span></td> 
  104.     </tr> 
  105.     <tr> 
  106.       <td height="40" colspan="2" align="center"><input type="submit" name="Submit" value="确认注册" /> 
  107.       &nbsp;&nbsp;&nbsp;&nbsp;  
  108.       <input type="reset" name="Submit2" value="重新填写" /></td> 
  109.     </tr> 
  110.   </table> 
  111. </form> 
  112. </body> 
  113. </html> 

servlet类

/PetStore/src/cn/wy/CtrlServlet/reg.java

 

 
  1. 代码  
  2.  
  3.  
  4.  
  5. package cn.wy.CtrlServlet;  
  6.  
  7. import cn.wy.DBConnection;  
  8. import java.io.*;  
  9. import java.sql.*;  
  10. import javax.servlet.ServletException;  
  11. import javax.servlet.http.*;  
  12.  
  13. // Referenced classes of package cn.wy.CtrlServlet:  
  14. //            login  
  15.  
  16. public class reg extends HttpServlet  
  17. {  
  18.  
  19.     public reg()  
  20.     {  
  21.     }  
  22.  
  23.     protected void processRequest(HttpServletRequest request, HttpServletResponse response)  
  24.         throws ServletException, IOException  
  25.     {  
  26.         PrintWriter out;  
  27.         DBConnection dbc=null;  
  28.         String userName;  
  29.         String psd;  
  30.         String email;  
  31.         String tel;  
  32.         String address;  
  33.         int popedom;  
  34.         response.setContentType("text/html;charset=UTF-8");  
  35.         out = response.getWriter();  
  36.         try{  
  37.         dbc = new DBConnection();  
  38.         PreparedStatement ps = null;  
  39.         userName = request.getParameter("userName");  
  40.         psd = login.encrypt(request.getParameter("password1").toString());  
  41.         email = request.getParameter("email");  
  42.         tel = request.getParameter("tel");  
  43.         address = request.getParameter("address");  
  44.         popedom = Integer.parseInt(request.getParameter("popedom"));  
  45.         if (userName != null && psd != null && email != null)  
  46.         {  
  47.         ps = dbc.getCon().prepareStatement("insert into [User](UName,Upass,UEmail,UTel,UAddress,UPopedom) values(?,?,?,?,?,?)");  
  48.         ps.setString(1, userName);  
  49.         ps.setString(2, psd);  
  50.         ps.setString(3, email);  
  51.         ps.setString(4, tel);  
  52.         ps.setString(5, address);  
  53.         ps.setInt(6, popedom);  
  54.         ps.execute();  
  55.         System.out.print("新用户注册:" + request.getParameter("userName") + "  ");  
  56.         out.print("<script>alert('恭喜您:注册成功!现已经登录到网站!');history.go(-1)</script>");  
  57.         }  
  58.         if (dbc != null)  
  59.             dbc.dbClose();  
  60.         }  
  61.         catch(SQLException ex)  
  62.         {  
  63.         out.print("<script>alert('注册失败!数据库连接错误!');history.go(-1)</script>");  
  64.         ex.printStackTrace();  
  65.         if (dbc != null)  
  66.             dbc.dbClose();  
  67.         }  
  68.           
  69.     }  
  70.  
  71.     protected void doGet(HttpServletRequest request, HttpServletResponse response)  
  72.         throws ServletException, IOException  
  73.     {  
  74.         processRequest(request, response);  
  75.     }  
  76.  
  77.     protected void doPost(HttpServletRequest request, HttpServletResponse response)  
  78.         throws ServletException, IOException  
  79.     {  
  80.         processRequest(request, response);  
  81.     }  
  82.  
  83.     public String getServletInfo()  
  84.     {  
  85.         return "Short description";  
  86.     }  

数据库连接类

/PetStore/src/cn/wy/DBConnection.java

 

 
  1. 代码  
  2.  
  3.  
  4.  
  5. package cn.wy;  
  6.  
  7. import java.io.PrintStream;  
  8. import java.sql.Connection;  
  9. import java.sql.DriverManager;  
  10.  
  11. public class DBConnection  
  12. {  
  13.  
  14.     private String mySqlDriver;  
  15.     private String mySqlUrl;  
  16.     private String mdbDrStr;  
  17.     private String mdbUrl;  
  18.     long time;  
  19.     private String SQLDRIVER;  
  20.     private String SQLURL;  
  21.     public Connection conn;  
  22.     boolean Conectde;  
  23.  
  24.     public DBConnection()  
  25.     {  
  26.         mySqlDriver = "org.gjt.mm.mysql.Driver";  
  27.         mySqlUrl = "jdbc:mysql://localhost/petstore?user=root&password=5455&useUnicode=true&characterEncoding=8859_1";  
  28.         mdbDrStr = "sun.jdbc.odbc.JdbcOdbcDriver";  
  29.         mdbUrl = "jdbc:odbc:pet";  
  30.         SQLDRIVER = "com.microsoft.sqlserver.jdbc.SQLServerDriver";  
  31.         SQLURL = "jdbc:sqlserver://127.0.0.1:1433;DatabaseName=PetStore";  
  32.         conn = null;  
  33.         Conectde = false;  
  34.         time = System.currentTimeMillis();  
  35.         try 
  36.         {  
  37.             Class.forName(SQLDRIVER).newInstance();  
  38.             if (conn == null)  
  39.             {  
  40.                 conn = DriverManager.getConnection(SQLURL, "sa""123456");  
  41.                 Conectde = true;  
  42.                 System.out.print("<");  
  43.             }  
  44.         }  
  45.         catch (Exception e)  
  46.         {  
  47.             e.printStackTrace();  
  48.         }  
  49.     }  
  50.  
  51.     public Connection getCon()  
  52.     {  
  53.         return conn;  
  54.     }  
  55.  
  56.     public void dbClose()  
  57.     {  
  58.         try 
  59.         {  
  60.             if (Conectde)  
  61.             {  
  62.                 conn.close();  
  63.                 Conectde = false;  
  64.                 System.out.print(System.currentTimeMillis() - time);  
  65.                 System.out.print(">  ");  
  66.             }  
  67.         }  
  68.         catch (Exception e)  
  69.         {  
  70.             e.printStackTrace();  
  71.         }  
  72.     }  

JavaBean类

/PetStore/src/cn/wy/Pet/User.java

 

 
  1. 代码  
  2.  
  3.  
  4. package cn.wy.Pet;  
  5.  
  6.  
  7. public class User  
  8. {  
  9.  
  10.     private int userID;  
  11.     private String UName;  
  12.     private String UPass;  
  13.     private String UEmail;  
  14.     private String UTel;  
  15.     private String UAddress;  
  16.     private String URegDate;  
  17.     private String ALastTime;  
  18.     private int UPopedom;  
  19.     private int UAppearCount;  
  20.     private int URestoreCount;  
  21.     private boolean login;  
  22.  
  23.     public User()  
  24.     {  
  25.         login = false;  
  26.     }  
  27.  
  28.     public boolean isLogin()  
  29.     {  
  30.         return login;  
  31.     }  
  32.  
  33.     public void setLogin(boolean isLogin)  
  34.     {  
  35.         login = isLogin;  
  36.     }  
  37.  
  38.     public void setURestoreCount(int URestoreCount)  
  39.     {  
  40.         this.URestoreCount = URestoreCount;  
  41.     }  
  42.  
  43.     public int getURestoreCount()  
  44.     {  
  45.         return URestoreCount;  
  46.     }  
  47.  
  48.     public void setUAppearCount(int UAppearCount)  
  49.     {  
  50.         this.UAppearCount = UAppearCount;  
  51.     }  
  52.  
  53.     public int getUAppearCount()  
  54.     {  
  55.         return UAppearCount;  
  56.     }  
  57.  
  58.     public void setUserID(int userID)  
  59.     {  
  60.         this.userID = userID;  
  61.     }  
  62.  
  63.     public void setUPopedom(int UPopedom)  
  64.     {  
  65.         this.UPopedom = UPopedom;  
  66.     }  
  67.  
  68.     public void setALastTime(String ALastTime)  
  69.     {  
  70.         this.ALastTime = ALastTime;  
  71.     }  
  72.  
  73.     public void setURegDate(String URegDate)  
  74.     {  
  75.         this.URegDate = URegDate;  
  76.     }  
  77.  
  78.     public void setUAddress(String UAddress)  
  79.     {  
  80.         this.UAddress = UAddress;  
  81.     }  
  82.  
  83.     public void setUTel(String UTel)  
  84.     {  
  85.         this.UTel = UTel;  
  86.     }  
  87.  
  88.     public void setUEmail(String UEmail)  
  89.     {  
  90.         this.UEmail = UEmail;  
  91.     }  
  92.  
  93.     public void setUPass(String UPass)  
  94.     {  
  95.         this.UPass = UPass;  
  96.     }  
  97.  
  98.     public void setUName(String UName)  
  99.     {  
  100.         this.UName = UName;  
  101.     }  
  102.  
  103.     public int getUserID()  
  104.     {  
  105.         return userID;  
  106.     }  
  107.  
  108.     public int getUPopedom()  
  109.     {  
  110.         return UPopedom;  
  111.     }  
  112.  
  113.     public String getALastTime()  
  114.     {  
  115.         return ALastTime;  
  116.     }  
  117.  
  118.     public String getURegDate()  
  119.     {  
  120.         return URegDate;  
  121.     }  
  122.  
  123.     public String getUAddress()  
  124.     {  
  125.         return UAddress;  
  126.     }  
  127.  
  128.     public String getUTel()  
  129.     {  
  130.         return UTel;  
  131.     }  
  132.  
  133.     public String getUEmail()  
  134.     {  
  135.         return UEmail;  
  136.     }  
  137.  
  138.     public String getUPass()  
  139.     {  
  140.         return UPass;  
  141.     }  
  142.  
  143.     public String getUName()  
  144.     {  
  145.         return UName;  
  146.     }  

 



本文转自linzheng 51CTO博客,原文链接:http://blog.51cto.com/linzheng/1080779

相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
6天前
|
Java
学院管理系统【JSP+Servlet+JavaBean】(Java课设)
学院管理系统【JSP+Servlet+JavaBean】(Java课设)
26 3
学院管理系统【JSP+Servlet+JavaBean】(Java课设)
|
6天前
|
Java
排课系统【JSP+Servlet+JavaBean】(Java课设)
排课系统【JSP+Servlet+JavaBean】(Java课设)
19 5
|
6天前
|
Java
仓库管理系统【JSP+Servlet+JavaBean】(Java课设)
仓库管理系统【JSP+Servlet+JavaBean】(Java课设)
16 1
|
6天前
|
Java
学校教师管理系统【JSP+Servlet+JavaBean】(Java课设)
学校教师管理系统【JSP+Servlet+JavaBean】(Java课设)
18 2
|
6天前
|
开发框架 缓存 Java
JSP 教程 之 Eclipse JSP/Servlet 环境搭建 1
本教程指导如何使用Eclipse搭建JSP/Servlet开发环境。需下载Eclipse J2EE和Tomcat,将Tomcat解压至无中文和空格的D盘目录。启动Tomcat后,服务器运行,通过浏览器访问`http://localhost:8080/`。在Tomcat的webapps\ROOT目录创建test.jsp文件,输入代码并访问`http://localhost:8080/test.jsp`以验证环境配置成功。
8 1
|
6天前
|
Java
校园帮【JSP+Servlet+JavaBean】(Java课设)
校园帮【JSP+Servlet+JavaBean】(Java课设)
9 1
|
6天前
|
Java
新闻发布系统【JSP+Servlet+JavaBean】(Java课设)
新闻发布系统【JSP+Servlet+JavaBean】(Java课设)
16 2
|
6天前
|
Java
学校人员管理系统【JSP+Servlet+JavaBean】(Java课设)
学校人员管理系统【JSP+Servlet+JavaBean】(Java课设)
13 2
|
6天前
|
Java
人事管理系统【JSP+Servlet+JavaBean】(Java课设)
人事管理系统【JSP+Servlet+JavaBean】(Java课设)
23 0
|
6天前
|
Java
学生信息管理系统【JSP+Servlet+JavaBean】(Java课设)
学生信息管理系统【JSP+Servlet+JavaBean】(Java课设)
17 1