• javaSE web开发 登录思路代码


    准备工作:

    1.在WebContent  WEB-INF   lib下添加c3p0-0.9.5.2.jar     mchange-common-java-0.2.11.jar   ojdbc6.jar

    2.java Resources中的src平级的添加c3p0-config.xml

    Java Resources

    src

    三个包

    com.hanqi.dao       数据库有关

    BankCard.java实体类    属性 getter setter toString 有参构造 无参构造 

    BankCardDAO.java   功能类  数据库增删改查

    DBHelper.java      单例的数据库工具类  返回数据连接   连接池 

    com.hanqi.service

    BankCardService.java  业务逻辑类

    com.hanqi.web      

    CheckLoginServlet.java  

    MyFilter.java        过滤器

    WebContent

    Login.jsp 登录界面

    Main.jsp 主页面

    Login.jsp

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Insert title here</title>
    </head>
    <body>
    
    
    <form action="CheckLoginServlet" method="POST">
    
    卡号:<input type="text" name="cardid" value=""><br>
    密码:<input type="password" name="password"><br>
    
    <input type="submit" value="登录">
    
    </form>
    
    </body>
    </html>

    CheckLoginServlet.java

    package com.hanqi.web;
    
    import java.io.IOException;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import javax.servlet.http.HttpSession;
    
    import com.hanqi.dao.BankCard;
    import com.hanqi.service.BankCardService;
    
    /**
     * Servlet implementation class CheckLoginServlet
     */
    public class CheckLoginServlet extends HttpServlet {
        private static final long serialVersionUID = 1L;
           
        /**
         * @see HttpServlet#HttpServlet()
         */
        public CheckLoginServlet() {
            super();
            // TODO Auto-generated constructor stub
        }
    
        /**
         * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
         */
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    
            // 接收参数
            String cardid = request.getParameter("cardid");
            String password = request.getParameter("password");    
            
            // 对get,post同时有效
            //cardid = new String(cardid.getBytes("ISO-8859-1"), "UTF-8");
                
            // 检查参数
            if (cardid == null || password == null
                    || cardid.trim().equals("") 
                    || password.trim().equals(""))
            {
                response.getWriter().println("请正确登录");
                
                response.setHeader("refresh", "3;url=Login.jsp");
            }
            else
            {            
                // 验证参数
                // 调用Service层
                BankCard bc = new BankCardService().checkLogin(cardid, password);
                
                if (bc != null)
                {                
                    // 创建session
                    HttpSession hs = request.getSession();
                    
                    hs.setAttribute("bankcard", bc);
                    
                    // 跳转
                    response.sendRedirect("Main.jsp");
                }
                else
                {
                    response.getWriter().println("卡号或密码错误" + " cardid = " + cardid);
                    
                    response.setHeader("refresh", "3;url=Login.jsp");
                }            
            }
            
            
        }
    
        /**
         * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
         */
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            // TODO Auto-generated method stub
            doGet(request, response);
        }
    
    }

    BankCard.java

    package com.hanqi.dao;
    
    import java.util.Date;
    
    // 实体类
    public class BankCard {
        
        private String cardID;
        private String userID;
        private String userName;
        private String password;
        private double balance;
        private String state;
        private Date createTime;
        public String getCardID() {
            return cardID;
        }
        public void setCardID(String cardID) {
            this.cardID = cardID;
        }
        public String getUserID() {
            return userID;
        }
        public void setUserID(String userID) {
            this.userID = userID;
        }
        public String getUserName() {
            return userName;
        }
        public void setUserName(String userName) {
            this.userName = userName;
        }
        public String getPassword() {
            return password;
        }
        public void setPassword(String password) {
            this.password = password;
        }
        public double getBalance() {
            return balance;
        }
        public void setBalance(double balance) {
            this.balance = balance;
        }
        public String getState() {
            return state;
        }
        public void setState(String state) {
            this.state = state;
        }
        public Date getCreateTime() {
            return createTime;
        }
        public void setCreateTime(Date createTime) {
            this.createTime = createTime;
        }
        public BankCard(String userID, String userName, String password) {
            super();
            this.userID = userID;
            this.userName = userName;
            this.password = password;
        }
        public BankCard() {
            super();
        }
        @Override
        public String toString() {
            return "BankCard [cardID=" + cardID + ", userID=" + userID + ", userName=" + userName + ", password=" + password
                    + ", balance=" + balance + ", state=" + state + ", createTime=" + createTime + "]";
        }
    
        
    }

    BankCardDAO.java

    package com.hanqi.dao;
    
    import java.sql.Connection;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    
    // 数据库操作类
    public class BankCardDAO {
        
        //// 单条查询
        public BankCard getBankCard(String cardid)
        {
            BankCard rtn = null;
            
            // 查询
            Connection conn = null;
            PreparedStatement ps = null;
            ResultSet rs = null;
            
            try {
                conn = DBHelper.getDBHelper().getConnection();
                
                String sql = "select * from t_bankcard where card_id = ?";
                
                ps = conn.prepareStatement(sql);
                
                ps.setString(1, cardid);
                
                rs = ps.executeQuery();
                
                if (rs.next())
                {
                    rtn = new BankCard();
                    
                    rtn.setCardID(cardid);
                    rtn.setBalance(rs.getDouble("Balance"));
                    rtn.setCreateTime(rs.getDate("create_time"));
                    rtn.setPassword(rs.getString("password"));
                    rtn.setState(rs.getString("state"));
                    rtn.setUserID(rs.getString("user_id"));
                    rtn.setUserName(rs.getString("user_name"));
                }            
                
            } catch (SQLException e) {
                e.printStackTrace();
            }
            finally {
                
                try {
                    rs.close();
                } catch (SQLException e2) {
                }
                
                try {
                    ps.close();
                } catch (SQLException e1) {
                }
                
                try {
                    conn.close();
                } catch (SQLException e) {
                }
                
            }        
            
            return rtn;
        }
        
        //////
    
    }

    DBHelper.java

    package com.hanqi.dao;
    
    import java.sql.Connection;
    import java.sql.SQLException;
    
    import com.mchange.v2.c3p0.ComboPooledDataSource;
    
    // 单例模式的数据库工具类
    // 返回数据库连接
    public class DBHelper {
        
        // 1 隐藏默认构造方法
        private DBHelper()
        {
            
        }
        
        // 2 静态实例
        private static DBHelper dh;
        
        // 3 返回实例的静态方法
        public static DBHelper getDBHelper()
        {
            if (dh == null)
            {
                dh = new DBHelper();
            }
            
            return dh;
        }
        
        private ComboPooledDataSource cpds = new ComboPooledDataSource("helloc3p0");
        
        // 返回连接的方法
        public Connection getConnection() throws SQLException
        {
            return cpds.getConnection();
        }
        
    }

    MyFilter.java

    package com.hanqi.web;
    
    import java.io.IOException;
    import javax.servlet.Filter;
    import javax.servlet.FilterChain;
    import javax.servlet.FilterConfig;
    import javax.servlet.ServletException;
    import javax.servlet.ServletRequest;
    import javax.servlet.ServletResponse;
    
    /**
     * Servlet Filter implementation class MyFilter
     */
    public class MyFilter implements Filter {
    
        /**
         * Default constructor. 
         */
        public MyFilter() {
            // TODO Auto-generated constructor stub
        }
    
        /**
         * @see Filter#destroy()
         */
        public void destroy() {
            // TODO Auto-generated method stub
        }
    
        /**
         * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
         */
        public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
    
            // 设置编码
            request.setCharacterEncoding("UTF-8");
            
            response.setContentType("text/html");
            response.setCharacterEncoding("UTF-8");
            
            chain.doFilter(request, response);
        }
    
        /**
         * @see Filter#init(FilterConfig)
         */
        public void init(FilterConfig fConfig) throws ServletException {
            // TODO Auto-generated method stub
        }
    
    }

    BankCardService.java

    package com.hanqi.service;
    
    import com.hanqi.dao.BankCard;
    import com.hanqi.dao.BankCardDAO;
    
    // 业务逻辑类
    public class BankCardService {
        
        // 验证登录方法
        public BankCard checkLogin(String cardid, String password)
        {
            BankCard rtn = null;
            
            // 得到卡信息
            BankCard bc = new BankCardDAO().getBankCard(cardid);
            
            if (bc != null)
            {
                // 验证数据有效性
                if (bc.getState().equals("1") 
                        && bc.getPassword().equals(password))
                {
                    rtn = bc;
                }
            }        
            
            return rtn;
        }
    
    }

    Main.jsp

    <%@page import="com.hanqi.dao.BankCard"%>
    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Insert title here</title>
    </head>
    <body>
    
    主页面  
    <br>
    <%
    
    Object obj = session.getAttribute("bankcard");
    
    if (obj == null)
    {
        out.print("会话超时或未登录");
        
        response.setHeader("refresh", "3;url=Login.jsp");
    }
    else
    {
        BankCard bc = (BankCard)obj;
        
        out.print("欢迎登录!" + bc.getUserName());
    }
    
    %>
    
    
    </body>
    </html>
  • 相关阅读:
    wince 操作sqlite数据库
    c#数据结构(第二章)
    C#数据结构(第三章)
    近期学习(收藏地址)
    c#数据结构(第四章)
    PowerDesigner 数据库设计
    wince操作远程sqlserver数据库
    一个有趣的算法
    c#数据结构(第一章)
    c#实现显示图片的动态效果
  • 原文地址:https://www.cnblogs.com/hanruyue/p/6024267.html
Copyright © 2020-2023  润新知