0


【项目】基于Servlet+jsp+mysql的资产管理系统

目录:

登录页面:

注册页面:

主页面:

资产管理界面:

截取部分界面

个人中心页面:

关于我们页面:

登陆功能实现:

1. 添加数据库驱动

2. 配置context.xml

   <Resource 
     name="jdbc/IT" 
     auth="Container"
     type="javax.sql.DataSource"
     driverClassName="com.mysql.cj.jdbc.Driver"
     url="jdbc:mysql://localhost:3306/assets?serverTimezone=UTC"
    username="root"
    password="232818"
     maxActive="100"
     maxIdle="30"
     maxWait="1000"/>

3. 完成数据库连接

package IT.org.Util;

import java.sql.Connection;
import java.sql.SQLException;

import javax.naming.InitialContext;
import javax.sql.DataSource;

public class DatabaseConnection {
    private Connection conn=null;
    private DataSource ds=null;
    private InitialContext ctx=null;
    public DatabaseConnection() {
        try {
            ctx=new InitialContext();
            ds=(DataSource)ctx.lookup("java:comp/env/jdbc/IT");
        } catch (Exception e) {
            // TODO: handle exception
        }
    }
    public Connection getConnection() {
        try {
            this.conn=ds.getConnection();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally {
            return conn;
        }
    }
    
    public void closeCon() {
        try {
            if(conn!=null) conn.close();
        } catch (Exception e) {
            // TODO: handle exception
        }
    }
    public static void main(String[] args) {
        DatabaseConnection d=new DatabaseConnection();
        System.out.println(d.getConnection());
    }
}

jsp页面:

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>IT资产管理系统登陆</title>
<link rel="stylesheet" type="text/css" href="./css/Login.css"/>
<script src="./js/jquery.js" type="text/javascript" charset="utf-8"></script>
<script src="./js/登录注册.js" type="text/javascript" charset="utf-8"></script>
<link href="img/title.ico" rel="SHORTCUT ICON" />
</head>
<body>
<section><div style="position: absolute;top: -40px;left: 35%;"><img src="img/loginlogo.png" width="550" height="200"></div>
            <div class="container" >
                <div class="user signinBx">
                    <div class="imgBX" ><img src="./img/bc1.jpg" /></div>
                    <div class="formBx">
                        <form action="Login_Register" method="post">
                            <input type="hidden" name="mytype" value="login"/>
                            <h2>登录</h2>
                            <input type="text" placeholder="账户名" id="un" name="un"> <br>
                            <label id="username"></label><br>
                            <input type="password" placeholder="密码" id="pw" name="pw"> <br>
                            <label id="password"></label> <br>
        
                            <a><input type="submit" value="登录" id="sub"></a>
                            <p class="signup">未注册账户请点击 <a href="#" id="zhuce">注册</a></p>
                        </form>
                    </div>
                </div>
        
                <div class="user signupBx">
        
                    <div class="formBx">
                        <label id="erroinfo"></label>
                        <form action="Login_Register" method="post">
                            <input type="hidden" name="mytype" value="register"/>
                            <h2>注册</h2>
                            <input type="text" placeholder="姓名" id="username" name="reun">
                            <input type="text" placeholder="邮箱" id="mainneme" name="email">
                            <input type="password" placeholder="密码" id="inputpassword" name="repw">
                            <input type="password" placeholder="再次确认密码" id="inputpassword1" onkeyup="validate()"><span id="tip"></span>
                            <input type="submit" id="zhecebtn" value="注册">
                            <p class="signup">已注册账户请点击<a href="#" id="denglu">登录</a></p>
                        </form>
                    </div>
                    <div class="imgBX"><img src="./img/bc3.jpg" /></div>
                </div>
        
            </div>
        </section>
</body>
</html>

登陆DAO部分:

文件目录

** Dao**

实现类部分:

public class IUserDAOImpl implements IUserDAO{
    private Connection conn;
    public IUserDAOImpl(Connection conn) {
        this.conn=conn;
    }
    @Override
    public boolean findLogin(TUser tuser) throws Exception {
        // TODO Auto-generated method stub
        String sql="SELECT * FROM tuser;";
        Statement st=conn.createStatement();
        ResultSet rs=st.executeQuery(sql);
        boolean isfind=false;
        while(rs.next()) {
            if(tuser.getUserName().equals(rs.getString("username").trim())&&tuser.getPassword().equals(rs.getString("password").trim())) {
                isfind=true;
                break;
            }
        }
        if(isfind) return true;
        else return false;
    }
/**
 * 注册插入
 */
    @Override
    public int register(TUser tuser) throws Exception {
        // TODO Auto-generated method stub
        String userid=String.valueOf(System.nanoTime()); 
        String sql="INSERT INTO tuser VALUE(?,?,?,'待填','待填','待填','待填',?,'待填','待填');";
        PreparedStatement ppst=conn.prepareStatement(sql);
        ppst.setString(1,userid);
        ppst.setString(2,tuser.getUserName());
        ppst.setString(3,tuser.getPassword());
        ppst.setString(4,tuser.getEmail());
        int rs = ppst.executeUpdate();
        ppst.close() ;
        return rs;
    }

Servlet部分:

package IT.org.Controller;

import java.io.IOException;
import java.util.*;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import IT.org.DAO.IUserDAO;
import IT.org.Factory.DAOFactory;
import IT.org.VO.TUser;

/**
 * Servlet implementation class Login_Register
 */
@WebServlet("/Login_Register")
public class Login_Register extends HttpServlet {
    private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Login_Register() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        response.setContentType("text/html;charset=UTF-8");
        request.setCharacterEncoding("UTF-8");
        String mytype=request.getParameter("mytype");
        String path="Login.jsp";
        
        //登录功能
        if(mytype.equals("login")) {
            String username=request.getParameter("un");
            String password=request.getParameter("pw");
            System.out.println(username);
            System.out.println(password);
            TUser tuser=new TUser(username.trim(),password.trim());
            IUserDAO dao=DAOFactory.getIUserInstance();
            try {
                if(dao.findLogin(tuser)) { 
                     ArrayList<TUser> users=DAOFactory.getIUserInstance().findAllTUser();
                     for(TUser us:users) {
                         if(us.getUserName().equals(tuser.getUserName())) {
                             tuser=us;
                             break;
                         }
                     }
                     HttpSession session=request.getSession();
                     
                     System.out.println("登录者id"+tuser.getUserID());
                     session.setAttribute("tuser",tuser);
                     request.getRequestDispatcher("index.jsp").forward(request, response); 
                }
                else  request.getRequestDispatcher(path).forward(request, response);
                
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
        }
        //注册功能
        else if(mytype.equals("register")) {
            String username=request.getParameter("reun");
            String password=request.getParameter("repw");
            String email=request.getParameter("email");
            boolean isexist=false;
            try {
                ArrayList<TUser> users=DAOFactory.getIUserInstance().findAllTUser();
                for(TUser u:users) {
                    if(u.getUserName().equals(username.trim())||u.getEmail().equals(email.trim())) {
                        isexist=true;
                        break;
                    }
                }
                if(!isexist) {
                     TUser tuser=new TUser(username, password,email);
                try {
                    DAOFactory.getIUserInstance().register(tuser);
                    request.getRequestDispatcher(path).forward(request, response);
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }else {
                System.out.println("用户名or邮箱已使用");
            }
            } catch (Exception e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
           
            
        }
        
            
         
    }

    /**
     * @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);
    }

}
标签: java-ee eclipse mysql

本文转载自: https://blog.csdn.net/m0_56233309/article/details/123619875
版权归原作者 小皮猪 所有, 如有侵权,请联系我们删除。

“【项目】基于Servlet+jsp+mysql的资产管理系统”的评论:

还没有评论