zoukankan      html  css  js  c++  java
  • JSP+JAVABEAN+SERVLET模式的注册实例实现

    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

    代码
    <servlet>
    <servlet-name>reg</servlet-name>
    <servlet-class>cn.wy.CtrlServlet.reg</servlet-class>
    </servlet>
    <servlet-mapping>
    <servlet-name>reg</servlet-name>
    <url-pattern>/reg</url-pattern>
    </servlet-mapping>

    前端jsp页面

    /PetStore/WebRoot/register.jsp

    代码
    <%@ page contentType="text/html; charset=gb2312" language="java" import="cn.wy.Pet.User" errorPage="" %>
    <jsp:useBean id="user" scope="page" class="cn.wy.Pet.User"/>
    <%
    //权限默认不是超级管理员
    boolean isAdmin = false ;
    String actionStr = "";
    if (session.getAttribute("user") != null)
    {
    user
    = (User)session.getAttribute("user");
    //取出权限
    if (user.getUPopedom() ==0)
    isAdmin
    = true ;
    }
    String qFile = request.getRequestURI();
    System.out.println(qFile);
    //说明是超级管理员,并在后台添加用户
    if ("/admin/default.jsp".equals(qFile))
    {
    actionStr
    = "../" ;
    }

    %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
    <title>宠物商店会员注册</title>
    <style type="text/css">
    <!--
    .STYLE1
    {
    color
    : #FF0000;
    font-weight
    : bold;
    }
    .STYLE2
    {color: #FF0000}
    .STYLE3
    {
    font-size
    : 18px;
    font-weight
    : bold;
    }
    -->
    </style>
    </head>

    <body style="font-size:12px">
    <form id="form1" name="form1" method="post" action="<%=actionStr%>reg">
    <p align="center"><br />
    <span class="STYLE3">用户注册</span></p>
    <table width="582" height="302" border="1" align="center" cellpadding="0" cellspacing="0" bordercolor="#BCACD2">
    <tr>
    <td width="80" align="right">用户名:</td>
    <td width="496" align="left"><input name="userName" type="text" id="userName" size="16" maxlength="16" />
    <span class="STYLE1">*</span> 3~16位字母或者数字(如:8hack)</td>
    </tr>
    <tr>
    <td align="right">密码:</td>
    <td align="left"><input name="password1" type="text" id="password1" size="16" maxlength="16" />
    <span class="STYLE1">* </span> 3~16位字母或者数字(如:abc123)</td>
    </tr>
    <tr>
    <td align="right">确认密码:</td>
    <td align="left"><input name="password2" type="text" id="password2" size="16" maxlength="16" />
    <span class="STYLE1">*</span> 必须和上面输入的密码相同</td>
    </tr>
    <tr>
    <td align="right">电子邮件:</td>
    <td align="left"><input name="email" type="text" id="email" maxlength="20" />
    <span class="STYLE1">*</span> 找回密码和联系用(如:8hack@163.com)</td>
    </tr>
    <tr>
    <td align="right">联系电话:</td>
    <td align="left"><input name="tel" type="text" id="tel" size="20" maxlength="20" />
    如(0871-8888888,13888853113)
    </td>
    </tr>
    <tr>
    <td align="right">联系地址:</td>
    <td align="left"><input name="address" type="text" id="address" maxlength="50" /></td>
    </tr>
    <%
    if (isAdmin)//如果是管理员就显示出下拉列表
    {
    %>
    <tr>
    <td height="26" align="right">权限:</td>
    <td align="left">
    <select name="popedom" id="popedom">
    <option value="0">超级管理员</option>
    <option value="1">会员</option>
    </select>
    </td>
    </tr>
    <%
    }
    else//否则就插入一个蕴藏域
    {
    %>
    <input type="hidden" name="popedom" value="1">
    <%
    }
    %>

    <tr>
    <td height="42" align="right">&nbsp;</td>
    <td align="left"><span class="STYLE2">为了方便联系和管理请认真填写你的信息,带星号的必须填写</span></td>
    </tr>
    <tr>
    <td height="40" colspan="2" align="center"><input type="submit" name="Submit" value="确认注册" />
    &nbsp;&nbsp;&nbsp;&nbsp;
    <input type="reset" name="Submit2" value="重新填写" /></td>
    </tr>
    </table>
    </form>
    </body>
    </html>

    servlet类

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

    代码


    package cn.wy.CtrlServlet;

    import cn.wy.DBConnection;
    import java.io.*;
    import java.sql.*;
    import javax.servlet.ServletException;
    import javax.servlet.http.*;

    // Referenced classes of package cn.wy.CtrlServlet:
    // login

    public class reg extends HttpServlet
    {

    public reg()
    {
    }

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException
    {
    PrintWriter out;
    DBConnection dbc
    =null;
    String userName;
    String psd;
    String email;
    String tel;
    String address;
    int popedom;
    response.setContentType(
    "text/html;charset=UTF-8");
    out
    = response.getWriter();
    try{
    dbc
    = new DBConnection();
    PreparedStatement ps
    = null;
    userName
    = request.getParameter("userName");
    psd
    = login.encrypt(request.getParameter("password1").toString());
    email
    = request.getParameter("email");
    tel
    = request.getParameter("tel");
    address
    = request.getParameter("address");
    popedom
    = Integer.parseInt(request.getParameter("popedom"));
    if (userName != null && psd != null && email != null)
    {
    ps
    = dbc.getCon().prepareStatement("insert into [User](UName,Upass,UEmail,UTel,UAddress,UPopedom) values(?,?,?,?,?,?)");
    ps.setString(
    1, userName);
    ps.setString(
    2, psd);
    ps.setString(
    3, email);
    ps.setString(
    4, tel);
    ps.setString(
    5, address);
    ps.setInt(
    6, popedom);
    ps.execute();
    System.out.print(
    "新用户注册:" + request.getParameter("userName") + " ");
    out.print(
    "<script>alert('恭喜您:注册成功!现已经登录到网站!');history.go(-1)</script>");
    }
    if (dbc != null)
    dbc.dbClose();
    }
    catch(SQLException ex)
    {
    out.print(
    "<script>alert('注册失败!数据库连接错误!');history.go(-1)</script>");
    ex.printStackTrace();
    if (dbc != null)
    dbc.dbClose();
    }

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException
    {
    processRequest(request, response);
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException
    {
    processRequest(request, response);
    }

    public String getServletInfo()
    {
    return "Short description";
    }
    }

    数据库连接类

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

    代码


    package cn.wy;

    import java.io.PrintStream;
    import java.sql.Connection;
    import java.sql.DriverManager;

    public class DBConnection
    {

    private String mySqlDriver;
    private String mySqlUrl;
    private String mdbDrStr;
    private String mdbUrl;
    long time;
    private String SQLDRIVER;
    private String SQLURL;
    public Connection conn;
    boolean Conectde;

    public DBConnection()
    {
    mySqlDriver
    = "org.gjt.mm.mysql.Driver";
    mySqlUrl
    = "jdbc:mysql://localhost/petstore?user=root&password=5455&useUnicode=true&characterEncoding=8859_1";
    mdbDrStr
    = "sun.jdbc.odbc.JdbcOdbcDriver";
    mdbUrl
    = "jdbc:odbc:pet";
    SQLDRIVER
    = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
    SQLURL
    = "jdbc:sqlserver://127.0.0.1:1433;DatabaseName=PetStore";
    conn
    = null;
    Conectde
    = false;
    time
    = System.currentTimeMillis();
    try
    {
    Class.forName(SQLDRIVER).newInstance();
    if (conn == null)
    {
    conn
    = DriverManager.getConnection(SQLURL, "sa", "123456");
    Conectde
    = true;
    System.out.print(
    "<");
    }
    }
    catch (Exception e)
    {
    e.printStackTrace();
    }
    }

    public Connection getCon()
    {
    return conn;
    }

    public void dbClose()
    {
    try
    {
    if (Conectde)
    {
    conn.close();
    Conectde
    = false;
    System.out.print(System.currentTimeMillis()
    - time);
    System.out.print(
    "> ");
    }
    }
    catch (Exception e)
    {
    e.printStackTrace();
    }
    }
    }

    JavaBean类

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

    代码

    package cn.wy.Pet;


    public class User
    {

    private int userID;
    private String UName;
    private String UPass;
    private String UEmail;
    private String UTel;
    private String UAddress;
    private String URegDate;
    private String ALastTime;
    private int UPopedom;
    private int UAppearCount;
    private int URestoreCount;
    private boolean login;

    public User()
    {
    login
    = false;
    }

    public boolean isLogin()
    {
    return login;
    }

    public void setLogin(boolean isLogin)
    {
    login
    = isLogin;
    }

    public void setURestoreCount(int URestoreCount)
    {
    this.URestoreCount = URestoreCount;
    }

    public int getURestoreCount()
    {
    return URestoreCount;
    }

    public void setUAppearCount(int UAppearCount)
    {
    this.UAppearCount = UAppearCount;
    }

    public int getUAppearCount()
    {
    return UAppearCount;
    }

    public void setUserID(int userID)
    {
    this.userID = userID;
    }

    public void setUPopedom(int UPopedom)
    {
    this.UPopedom = UPopedom;
    }

    public void setALastTime(String ALastTime)
    {
    this.ALastTime = ALastTime;
    }

    public void setURegDate(String URegDate)
    {
    this.URegDate = URegDate;
    }

    public void setUAddress(String UAddress)
    {
    this.UAddress = UAddress;
    }

    public void setUTel(String UTel)
    {
    this.UTel = UTel;
    }

    public void setUEmail(String UEmail)
    {
    this.UEmail = UEmail;
    }

    public void setUPass(String UPass)
    {
    this.UPass = UPass;
    }

    public void setUName(String UName)
    {
    this.UName = UName;
    }

    public int getUserID()
    {
    return userID;
    }

    public int getUPopedom()
    {
    return UPopedom;
    }

    public String getALastTime()
    {
    return ALastTime;
    }

    public String getURegDate()
    {
    return URegDate;
    }

    public String getUAddress()
    {
    return UAddress;
    }

    public String getUTel()
    {
    return UTel;
    }

    public String getUEmail()
    {
    return UEmail;
    }

    public String getUPass()
    {
    return UPass;
    }

    public String getUName()
    {
    return UName;
    }
    }
  • 相关阅读:
    STM32F10x_ADC三通道逐次转换(单次、单通道软件触发)
    STM32F10x_RTC日历
    STM32F4_TIM输入波形捕获(脉冲频率)
    详解 C 语言开发五子棋游戏以及游戏中的重要算法与思路
    平安银行 深度解析梧州模式 或许是国内医药分开最好的模板!
    屏蔽双绞线和非屏蔽双绞线之间的区别
    如何刷新本地的DNS缓存?
    无线网络发射和接收的物理原理!
    wifi基本原理
    大润发创始人黄明端挥泪离场:我战胜了所有对手,却输给了时代!
  • 原文地址:https://www.cnblogs.com/linzheng/p/1930813.html
Copyright © 2011-2022 走看看