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

首先,给一张截图:

用Servlet结合c3p0连接池等写一个简单的注册登录_第1张图片用Servlet结合c3p0连接池等写一个简单的注册登录_第2张图片
上面图是我的整体内容

1、先进入工具类
用Servlet结合c3p0连接池等写一个简单的注册登录_第3张图片
代码如下: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、用Servlet结合c3p0连接池等写一个简单的注册登录_第4张图片
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、用Servlet结合c3p0连接池等写一个简单的注册登录_第5张图片
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、用Servlet结合c3p0连接池等写一个简单的注册登录_第6张图片

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 SelectAll() throws SQLException{
	String sql = "select * from user";
	List beans = qr.query(sql, new BeanListHandler(Bean.class));
	return beans;
}

}

5、
用Servlet结合c3p0连接池等写一个简单的注册登录_第7张图片
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、
用Servlet结合c3p0连接池等写一个简单的注册登录_第8张图片
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、用Servlet结合c3p0连接池等写一个简单的注册登录_第9张图片

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、用Servlet结合c3p0连接池等写一个简单的注册登录_第10张图片

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、用Servlet结合c3p0连接池等写一个简单的注册登录_第11张图片

com.mysql.jdbc.Driver jdbc:mysql://localhost:3307/xx root root 5 10

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

10、用Servlet结合c3p0连接池等写一个简单的注册登录_第12张图片

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、用Servlet结合c3p0连接池等写一个简单的注册登录_第13张图片

Insert title here id: 回到主页

12、用Servlet结合c3p0连接池等写一个简单的注册登录_第14张图片

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

Insert title here

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

13、
用Servlet结合c3p0连接池等写一个简单的注册登录_第15张图片

Insert title here

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

员工所有信息

进入员工的信息库
	

编辑用户信息根据id

请输入要删除人的id:

14、
用Servlet结合c3p0连接池等写一个简单的注册登录_第16张图片

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

Insert title here

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

		
	

用户名:
密码:

15、用Servlet结合c3p0连接池等写一个简单的注册登录_第17张图片

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

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

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

			
				
			
		
	
用户名:
密码:

16、用Servlet结合c3p0连接池等写一个简单的注册登录_第18张图片

Insert title here

跳到员工管理界面

17、用Servlet结合c3p0连接池等写一个简单的注册登录_第19张图片

Insert title here

修改信息

ID: 用户名: 密码:
	回到主页

18、用Servlet结合c3p0连接池等写一个简单的注册登录_第20张图片

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

Insert title here

修改信息

ID: 用户名: 密码:

19、用Servlet结合c3p0连接池等写一个简单的注册登录_第21张图片

最后,想测试的话,在这里有一个测试类,可写可不写:把注释恢复即可
用Servlet结合c3p0连接池等写一个简单的注册登录_第22张图片

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

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

你可能感兴趣的:(编程,JAVA,HTML)