用Servlet结合c3p0连接池等写一个简单的注册登录

首先,给一张截图:

在这里插入图片描述在这里插入图片描述
上面图是我的整体内容

1、先进入工具类
在这里插入图片描述
代码如下:package com.qf.util;

import javax.sql.DataSource;

import com.mchange.v2.c3p0.ComboPooledDataSource;

public class Util {
public static DataSource ds = null;

static{
	ds = new ComboPooledDataSource("mypool");
}
public static DataSource getConnection(){
	return ds;
}

}

2、在这里插入图片描述
package com.qf.bean;

public class Bean {

private int id;
private String username;
private String password;

public Bean() {
	super();
	// TODO Auto-generated constructor stub
}

public Bean(int id, String username, String password) {
	super();
	this.id = id;
	this.username = username;
	this.password = password;
}

public int getId() {
	return id;
}
public void setId(int id) {
	this.id = id;
}
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;
}

}

3、在这里插入图片描述
package com.qf.del;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.qf.jdbcserver.Server;

/**

  • Servlet implementation class Del
    */
    public class Del extends HttpServlet {

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    Server server = new Server();
    RequestDispatcher dispatcher = null;
    int row = 0;
    int id = Integer.valueOf(request.getParameter(“id”)).intValue();
    try {
    row = server.DelUser(id);
    if(row != 0){
    dispatcher = request.getRequestDispatcher("/del.html");
    }else{
    dispatcher = request.getRequestDispatcher("/error");
    }
    dispatcher.forward(request, response);
    } catch (Exception e) {
    // TODO: handle exception
    e.printStackTrace();
    }

    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    doGet(request, response);
    }

}

4、在这里插入图片描述

package com.qf.jdbcserver;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import com.qf.bean.Bean;
import com.qf.util.Util;
import com.sun.org.apache.bcel.internal.generic.NEW;

public class Server {
private QueryRunner qr = new QueryRunner(Util.getConnection());
// private final String TABLE_NAME = “user”;
private int row = 0;
public Bean Login(String username,String password) throws SQLException{
Bean user = null;
String sql = "select * from user where username = ? and password = ? ";
user = qr.query(sql,new BeanHandler(Bean.class),username,password);
return user;
}

public int Insert(Object username,Object password) throws SQLException{
	String sql = "insert into user values(null,?,?)";
	 row =qr.update(sql, username,password);
	return row;
}

public int DelUser(int id) throws SQLException{
	String sql = "delete from user where id =?";
	 row = qr.update(sql, id);
	 return row;
}
//改:根据id改密码
public int UpdateUser(int id,String username,String password) throws SQLException{
	
		String sql ="update user set username= ? , password = ? where id = ?";
		row = qr.update(sql,username, password,id);
		
		return row;
	}
public List<Bean> SelectAll() throws SQLException{
	String sql = "select * from user";
	List<Bean> beans = qr.query(sql, new BeanListHandler<Bean>(Bean.class));
	return beans;
}

}

5、
在这里插入图片描述
package com.qf.register;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.qf.bean.Bean;
import com.qf.jdbcserver.Server;

/**

  • Servlet implementation class RegisterServlet
    */
    public class RegisterServlet extends HttpServlet {

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    request.setCharacterEncoding(“utf-8”);
    Server server = new Server();
    RequestDispatcher dispatcher =null;
    int row = 0;
    String username =request.getParameter(“username”);
    String password = request.getParameter(“password”);
    try {
    row=server.Insert(username, password);
    if(row != 0){
    dispatcher = request.getRequestDispatcher("/login.jsp");
    }else{
    dispatcher = request.getRequestDispatcher("/error.jsp");
    }
    dispatcher.forward(request, response);
    } catch (Exception e) {
    // TODO: handle exception
    e.printStackTrace();
    }

    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    doGet(request, response);
    }

}

6、
在这里插入图片描述
package com.qf.selectall_information;

import java.io.IOException;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.qf.bean.Bean;
import com.qf.jdbcserver.Server;

/**

  • Servlet implementation class SelectInformation
    */
    public class SelectInformation extends HttpServlet {

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    Server server = new Server();
    List user= null;
    RequestDispatcher dispatcher = null ;
    System.out.println(“1”);
    try {
    System.out.println(“2”);
    user= server.SelectAll();
    if(user != null){
    for (Bean u : user) {
    System.out.println(“用户名ID:”+u.getId()+"\t “+“用户名:”+u.getUsername()+”\t"+“密码:”+u.getPassword());
    }
    dispatcher=request.getRequestDispatcher("/information.html");
    }else{
    dispatcher = request.getRequestDispatcher("/error.jsp");
    }
    dispatcher.forward(request, response);
    } catch (Exception e) {
    // TODO: handle exception
    e.printStackTrace();
    }

    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    doGet(request, response);
    }

}

7、在这里插入图片描述

package com.qf.servlet;

import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.qf.bean.Bean;
import com.qf.jdbcserver.Server;

/**

  • Servlet implementation class LoginServlet
    */
    public class LoginServlet extends HttpServlet {

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    request.setCharacterEncoding(“utf-8”);
    Server server = new Server();
    RequestDispatcher rd = null;
    String username = request.getParameter(“username”);
    String password = request.getParameter(“password”);

     Bean user = null;
     try {
     	
     	user = server.Login(username, password);
     	if(user!=null){
     		rd=request.getRequestDispatcher("/zhuye.html");
     	}else{
     		rd=request.getRequestDispatcher("/register.jsp");
     	}
     	rd.forward(request, response);
     } catch (SQLException e) {
     	// TODO Auto-generated catch block
     	e.printStackTrace();
     }
    

    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    doGet(request, response);
    }

}
8、在这里插入图片描述

package com.qf.update;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.qf.jdbcserver.Server;
import com.sun.rowset.internal.InsertRow;

/**

  • Servlet implementation class Update
    */
    public class Update extends HttpServlet {

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    Server server = new Server();
    RequestDispatcher dispatcher = null;
    int id =Integer.valueOf(request.getParameter(“id”)).intValue();
    String username = request.getParameter(“username”);
    String password = request.getParameter(“password”);
    int row = 0;

     try {
     	
     	 row = server.UpdateUser(id, username, password);
     	 
     	 if(row != 0){
     		
     		 dispatcher = request.getRequestDispatcher("/update.html");
     	 }else{
     		 dispatcher =request.getRequestDispatcher("/error.jsp");
     	 }
     	 dispatcher.forward(request, response);
     	
     } catch (Exception e) {
     	// TODO: handle exception
     	e.printStackTrace();
     }
    

    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    doGet(request, response);
    }

}

9、在这里插入图片描述

<?xml version="1.0" encoding="UTF-8"?> com.mysql.jdbc.Driver jdbc:mysql://localhost:3307/xx root root 5 10

名字可以改
jdbc:mysql://localhost:3307/xx 端口号,、xx这个数据局库名字,都可以改

10、在这里插入图片描述

<?xml version="1.0" encoding="UTF-8"?> project_3 LoginServlet com.qf.servlet.LoginServlet LoginServlet /login RegisterServlet com.qf.register.RegisterServlet RegisterServlet /register SelectInformation com.qf.selectall_information.SelectInformation SelectInformation /See Update com.qf.update.Update Update /update Del com.qf.del.Del Del /del

内容挺多的,你可以参考着写

11、在这里插入图片描述

Insert title here id: 回到主页

12、在这里插入图片描述

<%@ page language=“java” contentType=“text/html; charset=UTF-8”
charset=UTF-8"%>

Insert title here

你的信息有误,请检查你的错误

13、
在这里插入图片描述

Insert title here

欢迎来到员工管理界面,请注意点击哦,不要做不正当行为

员工所有信息
进入员工的信息库
	<a href="update.html"><h3>编辑用户信息根据id</h3></a>
	
	
	<a href=" del.html"><h2>请输入要删除人的id:</h2></a>

14、
在这里插入图片描述

<%@ page language=“java” contentType=“text/html; charset=UTF-8”
pageEncoding=“UTF-8”%>

Insert title here

欢迎来到登录界面,请登录

		</tr>
	</table>
</form>
用户名:
密码:

15、在这里插入图片描述

<%@ page language=“java” contentType=“text/html; charset=UTF-8”
pageEncoding=“UTF-8”%>

Insert title here <% request.setCharacterEncoding("utf-8"); %>

欢迎来到注册界面,请注册:

			<tr>
				<td><input type="submit" name="submit" id="submit" value="提交" /></td>
			</tr>
		</table>
	</form>
用户名:
密码:

16、在这里插入图片描述

Insert title here

跳到员工管理界面

17、在这里插入图片描述

Insert title here

修改信息

ID: 用户名: 密码:
	<a href="zhuye.html">回到主页</a>

18、在这里插入图片描述

<%@ page language=“java” contentType=“text/html; charset=UTF-8”
pageEncoding=“UTF-8”%>

Insert title here

修改信息

ID: 用户名: 密码:

19、在这里插入图片描述

最后,想测试的话,在这里有一个测试类,可写可不写:把注释恢复即可
在这里插入图片描述

还有,里面tomcat,servlet,d3p0等等,还需要你自己去安装和配置了!!!!!!

项目链接:https://pan.baidu.com/s/1WFoVLv1jyndxP9NzCmN6BA
提取码:lo8k

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

东方巴黎~Sunsiny

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值