jsp+servlet+mysql实现登录注册

登录界面:

<%@ 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>登录</title>
<link rel="stylesheet" type="text/css" href="css/login.css">
<script type="text/javascript" src="js/jquery-2.1.1.min.js"></script>
</head>
<body>
<div class = "loginbox">
	<a class = "logo">登录</a><br><br>
	<form action="LoginServlet" method="post">
		<div class = "zhanghao">
			账号:<input type="text" id="zhan" name="username" /><br><br>
			密码:<input type="password" id="mima" name="password" >
		</div>
		<font color="red">
				<%
					if(request.getAttribute("message")!= null){
						out.print(request.getAttribute("message"));
					}
				%>
			</font>
		<br>
		<input type="checkbox" id="check" >记住密码&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
		<input type="checkbox" id="check" >自动登录
		<div class = "denglu">
	
			<input type="submit" name="submit" value ="登录" id="deng"/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
			<input type="button" value = "重置" id="kong"><br><br>
			没有账号?<a href="zhuce.jsp">去注册!</a>
		</div>
	</form>
</div>
<script type="text/javascript">
	$(document).ready(function(){
    	$("#kong").click(function(){
            $("#zhan").val("");
            $("#mima").val("");
    	});
	});
</script>
</body>
</html>

注册界面:

<%@ 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>注册</title>
<link rel="stylesheet" type="text/css" href="css/zhuce.css">
<script type="text/javascript" src="js/jquery-2.1.1.min.js"></script>
</head>
<body>
<div class = "loginbox">
	
	<a class = "logo">注册</a><br><br>
	<form action="RegisterServlet" method="post">
		<div class = "zhanghao">
			账号:<input type="text" id = "zhan" name="username" /><br><br>
			密码:<input type="password" id = "mima" name="password" />
		</div>
		<font color="red">
				<%
					if(request.getAttribute("message")!= null){
						out.print(request.getAttribute("message"));
					}
				%>
		</font>
	<br>
	<input type="checkbox" id="check" >记住账号
	<div class = "denglu">
		<input type="submit" name="submit" value ="注册"/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
		<input type="button" value = "重置" id = "kong"><br><br>
		已有账号?<a href="login.jsp">去登录</a>
	</div>
	</form>
</div>
<script type="text/javascript">
	$(document).ready(function(){
    	$("#kong").click(function(){
            $("#zhan").val("");
            $("#mima").val("");
            $("#bumen").val("");
    	});
	});
</script>
</body>
</html>

loginservlet:

package com.smxy.zjq.dao;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

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 com.smxy.zjq.db.DbPool;

/**
 * Servlet implementation class LoginServlet
 */
//@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	static Connection conn = DbPool.getConnectionFromPool();
	static PreparedStatement pstmt = null;
	private static ResultSet rs = null;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public LoginServlet() {
        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
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		String userName = request.getParameter("username");
		String password = request.getParameter("password");
		System.out.println("账号:"+userName+"密码:"+password);
		//System.err.println(userName+";"+password);
		int i;
		try {
			i = search(userName,password);
			if(i == 1) {
				request.getRequestDispatcher(
						"/index.jsp").forward(request, response);
			}else if( i == -1){
				request.setAttribute("message", "账密错误,请重新登录");
				request.getRequestDispatcher("/Login.jsp").forward(request, response);
			}else if(i == 0){
				request.setAttribute("message", "账号不存在!");
				request.getRequestDispatcher("/Login.jsp").forward(request, response);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	public int search(String name,String pass) throws SQLException//此方法用于判断密码是否正确,用户名是否存在
	{
		String sql="select * from user where username = '"+name+"'";
		pstmt= (PreparedStatement)conn.prepareStatement(sql);
		rs=pstmt.executeQuery();
		if(rs==null)
		{
			return 0;
		}
		if(rs.next()) {
			String pas=rs.getString("password");
			if(pas.equals(pass)) {
				return 1; 
			}
			else 
			{
				return -1;
			}
				
		}
		return 0;
	}

}

registerservlet:

package com.smxy.zjq.dao;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.Calendar;

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 com.smxy.zjq.Class.User;
import com.smxy.zjq.db.DbPool;

/**
 * Servlet implementation class RegisterServlet
 */
//@WebServlet("/RegisterServlet")
public class RegisterServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	static Connection conn = DbPool.getConnectionFromPool();
	static PreparedStatement pstmt = null;
	private static ResultSet rs = null;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public RegisterServlet() {
        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
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		String userName = request.getParameter("username");
		String password = request.getParameter("password");
		System.out.println("账号:"+userName+"密码:"+password);
		
		User user = new User(userName,password);
		
		if(savedata(user)){
			request.getRequestDispatcher(
					"/index.jsp").forward(request, response);
		}else{
			request.setAttribute("message", "注册失败!");
			request.getRequestDispatcher("/Register.jsp").forward(request, response);
		}
	}
	
	public static boolean  savedata(User user) {

		String insertsql = "insert into user(username,password) values(?,?)";// 预处理的语句对象
		try {
			int i = 1;
			pstmt = conn.prepareStatement(insertsql);
			pstmt.setString(i++, user.getUsername());
			pstmt.setString(i++, user.getPassword());
			int val = pstmt.executeUpdate();
			if (val > 0) {
				return true;
			} else {
				return false;
			}

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return false;
	}

}

dbpool:

package com.smxy.zjq.db;

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

import javax.sql.DataSource;

import com.mchange.v2.c3p0.ComboPooledDataSource;



public class DbPool {

static DataSource dataSource;
	
	static {
		//如果没有参数,默认加载c3p0参数文件:文件名称、位置是固定的
		dataSource=new ComboPooledDataSource();
	}
	
	public static Connection getConnectionFromPool(){
		try {
			Connection  connection=dataSource.getConnection();
		    return connection;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

}

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值