一个简单的登陆注册系统

package com.zoufang.servlet;

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

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

import com.zoufang.bean.User;
import com.zoufang.dao.UserDao;

public class Register extends HttpServlet{

	public void doGet(HttpServletRequest req,HttpServletResponse resp) throws IOException, ServletException{
		resp.setContentType("text/html;charset=uft-8");
		String name=req.getParameter("name");
		String pwd=req.getParameter("password");
		UserDao uDao=new UserDao();
		User user = null;
		try {
			user = uDao.findByName(name);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println(user.getName());
		System.out.println("判断user是否存在");
		if(name.equals(user.getName())){
			String msg=name+"该用户名已经使用!请重新注册!";
			req.setAttribute("msg", msg);
			req.getRequestDispatcher("register.jsp").forward(req, resp);
		}else{
			System.out.println("检验数据库没有该用户,存入用户信息!");
			User user1=new User();
			user1.setName(name);
			user1.setPassword(pwd);
			try {
				uDao.saveUser(user1);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			String msg=name+"注册成功,请重新登陆!";
			req.setAttribute("msg", msg);
			req.getRequestDispatcher("login.jsp").forward(req, resp);
		}
	}

    public void doPost(HttpServletRequest req,HttpServletResponse resp) throws IOException, ServletException{
		doGet(req, resp);
	}
}

1、bean层,创建一个对象

package com.zoufang.bean;

import java.io.Serializable;

public class User implements Serializable{

	private String name;
	private String password;

	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
}

2、dao层连接数据库(此处为mysql)

package com.zoufang.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.zoufang.bean.User;

public class UserDao {

	private static String driver = "com.mysql.jdbc.Driver";
	private static String url = "jdbc:mysql://localhost:3306/test";
	private static String username = "zoufang";
	private static String password = "123";
	private static Connection connection = null;
	private static PreparedStatement ps = null;

	public Connection getConnection() throws ClassNotFoundException, SQLException{
		Class.forName(driver);
		connection=DriverManager.getConnection(url, username, password);
		return connection;
	}

	public void closeConnection() throws SQLException{
		if(connection!=null)
			 connection.close();
			if(ps!=null)
				ps.close();
	}

	public void saveUser(User user) throws SQLException{
		try {
			connection=getConnection();
			String sql="insert into user values(?,?)";
			ps=connection.prepareStatement(sql);
			ps.setString(1, user.getName());
			ps.setString(2, user.getPassword());
			ps.executeUpdate();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			closeConnection();
		}
	}

	public User findByName(String name) throws SQLException{
		ResultSet rs = null;
		User user=new User();
		try {
			connection=getConnection();
			System.out.println("开始读取数据");
			String sql="select * from user where name='"+name+"'";
			ps=connection.prepareStatement(sql);
			rs=ps.executeQuery(sql);
				 while(rs.next()) {
					 System.out.println(rs.getString(1)+"\t"+rs.getString(2));
					 user.setName(rs.getString(1));
					 user.setPassword(rs.getString(2));
				 return user;
				 }
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			closeConnection();
		}
		return user;
	}

	public void deleteUser(String name) throws ClassNotFoundException, SQLException{
		Connection connection=getConnection();
		String sql="delete from user where name='"+name+"'";
		ps=connection.prepareStatement(sql);
		ps.execute(sql);
		closeConnection();
	}

	public void changUser(User user) throws SQLException, ClassNotFoundException{
		Connection connection=getConnection();
		String sql="update user set password='"+user.getPassword()+"' where name='"+user.getName()+"' ";
		ps=connection.prepareStatement(sql);
		ps.execute(sql);
		closeConnection();
	}

}

3、业务逻辑层

package com.zoufang.servlet;

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

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.zoufang.bean.User;
import com.zoufang.dao.UserDao;

public class Change extends HttpServlet{
 public void doGet(HttpServletRequest req,HttpServletResponse resp) throws ServletException,IOException{
	resp.setContentType("text/html;charset=uft-8");
	HttpSession hs=req.getSession();
	String name=(String) hs.getAttribute("1");
	System.out.println("当前登陆用户名"+name);
	String password=(String) hs.getAttribute("2");
	String password1=req.getParameter("password1");//新密码
	String password2=req.getParameter("password2");//重新输入一次密码
	if(password.equals(password1)){
		String msg1="新输入的密码与旧密码一样,请重新输入";
		req.setAttribute("msg1", msg1);
		req.getRequestDispatcher("change.jsp").forward(req,resp);
	}else if(!password2.equals(password1)){
		String msg2="密码不一致!请重新输入!";
		req.setAttribute("msg2", msg2);
		req.getRequestDispatcher("change.jsp").forward(req,resp);
	}else  if(password2.equals(password1)){
		System.out.println(",,,,,,,,,,,,,,,,,,,");
		UserDao userDao=new UserDao();
		User user=new User();
		user.setName(name);
		user.setPassword(password1);
		try {
			userDao.changUser(user);
		} catch (ClassNotFoundException | SQLException e) {
			e.printStackTrace();
		}
		String msg="密码修改成功,请重新登陆!";
		req.setAttribute("msg", msg);
		req.getRequestDispatcher("login.jsp").forward(req,resp);
	}

 }
 public void doPost(HttpServletRequest req,HttpServletResponse resp) throws ServletException, IOException{
	 doGet(req, resp);

 }

}
package com.zoufang.servlet;

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

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.zoufang.bean.User;
import com.zoufang.dao.UserDao;

public class Delete extends HttpServlet{
 public void doGet(HttpServletRequest req,HttpServletResponse resp) throws ServletException,IOException{
	resp.setContentType("text/html;charset=uft-8");
	HttpSession hs=req.getSession();
	String name=(String) hs.getAttribute("1");
	UserDao userDao=new UserDao();
	try {
		userDao.deleteUser(name);
	} catch (ClassNotFoundException | SQLException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	String msg="用户删除成功!";
	req.setAttribute("msg", msg);
	req.getRequestDispatcher("login.jsp").forward(req, resp);
 }
 public void doPost(HttpServletRequest req,HttpServletResponse resp) throws ServletException, IOException{
	 doGet(req, resp);

 }

}
package com.zoufang.servlet;

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

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.zoufang.bean.User;
import com.zoufang.dao.UserDao;

public class Login extends HttpServlet{
 public void doGet(HttpServletRequest req,HttpServletResponse resp) throws ServletException,IOException{
	resp.setContentType("text/html;charset=uft-8");
	String name=req.getParameter("name");
	String pwd=req.getParameter("password");
	HttpSession hs=req.getSession();
	hs.setAttribute("1",name);
	hs.setAttribute("2", pwd);
	UserDao userDao =new UserDao();
	User user = null;
	try {
		user = userDao.findByName(name);
	} catch (SQLException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	if(name!=null&&pwd!=null&&name.equals(user.getName())&&pwd.equals(user.getPassword())){
		resp.sendRedirect("success.jsp");
	}else{
		String msg="你所输入的用户名不存在或者密码不正确,请重新输入!";
		req.setAttribute("msg", msg);
		req.getRequestDispatcher("login.jsp").forward(req, resp);
	}

 }
 public void doPost(HttpServletRequest req,HttpServletResponse resp) throws ServletException, IOException{
	 doGet(req, resp);

 }

}
package com.zoufang.servlet;

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

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

import com.zoufang.bean.User;
import com.zoufang.dao.UserDao;

public class Register extends HttpServlet{

	public void doGet(HttpServletRequest req,HttpServletResponse resp) throws IOException, ServletException{
		resp.setContentType("text/html;charset=uft-8");
		String name=req.getParameter("name");
		String pwd=req.getParameter("password");
		UserDao uDao=new UserDao();
		User user = null;
		try {
			user = uDao.findByName(name);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println(user.getName());
		System.out.println("判断user是否存在");
		if(name.equals(user.getName())){
			String msg=name+"该用户名已经使用!请重新注册!";
			req.setAttribute("msg", msg);
			req.getRequestDispatcher("register.jsp").forward(req, resp);
		}else{
			System.out.println("检验数据库没有该用户,存入用户信息!");
			User user1=new User();
			user1.setName(name);
			user1.setPassword(pwd);
			try {
				uDao.saveUser(user1);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			String msg=name+"注册成功,请重新登陆!";
			req.setAttribute("msg", msg);
			req.getRequestDispatcher("login.jsp").forward(req, resp);
		}
	}

    public void doPost(HttpServletRequest req,HttpServletResponse resp) throws IOException, ServletException{
		doGet(req, resp);
	}
}

4、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>修改密码</title>
</head>
<body>
	<form action="change" method="get">
		<center>
			<table>
				<tr>
					<td><input type="text" value="请输入新的密码" name="password1"/><a >${msg1}</a></td>
				</tr>
				<tr>
					<td><input type="text" value="请重新输入一次密码" name="password2"/><a >${msg2}</a></td>
				</tr>
				<tr>
					<td><input type="submit" value="提交"/></td>
				</tr>
			</table>
		</center>
	</form>
</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>
</head>
<body>
	<center>
	<form action="delete" method="get">
		确定要删除此用户!</br>
		<input type="submit" value="确定">
	</form>
	</center>
</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>
</head>
<body>
	<center>
	<a >${msg}</a>
	<form action="login" method="get">
		<table border="1" cellspacing="0" >
			<tr>
				<td align="center" colspan="2">用户登录</td>
			</tr>
			<tr>
				<td>用户名:</td>
				<td><input type="text" name="name" ></td>
			</tr>
			<tr>
				<td>密 码:</td>
				<td><input type="text" name="password"></td>
			</tr>
			<tr>
				<td></td>
				<td>
				<input type="submit" value="提交">
				<input type="button" onclick="window.location.href='register.jsp'" value="注册"/>
				</td>
			</tr>
		</table>
	</form>
	</center>
</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>
</head>
<body>
	<center>
	<a>欢迎注册!</a>
	<a>${msg}</a>
	<form action="register" method="get">
		<table border="1" cellspacing="0">
			<tr>
				<td align="center" colspan="2">用户登录</td>
			</tr>
			<tr>
				<td>用户名:</td>
				<td><input type="text" name="name" ></td>
			</tr>
			<tr>
				<td>密 码:</td>
				<td><input type="text" name="password"></td>
			</tr>
			<tr>
				<td colspan="2"><input type="submit" value="提交"></td>
			</tr>
		</table>
	</form>
	</center>
</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>
</head>
<body>
	<center>
		<table border="1" cellspacing="1" style="width:200px;height:100px">
			<tr>
				<td align="center" colspan="2">用户登录成功</td>
			</tr>
			<tr>
				<td  align="center">
				<input type="button" onclick="window.location.href='change.jsp'" value="修改"/>
				<input type="button" onclick="window.location.href='delete.jsp'" value="注销"/>
				</td>
			</tr>
		</table>
	</center>
</body>
</html>
</body>
</html>

5、web中的配置文件

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>LoginDemo</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>

  <servlet>
  	<servlet-name>Login</servlet-name>
  	<servlet-class>com.zoufang.servlet.Login</servlet-class>
  </servlet>
  <servlet-mapping>
  	<servlet-name>Login</servlet-name>
	<url-pattern>/login</url-pattern>
  </servlet-mapping>

   <servlet>
  	<servlet-name>Register</servlet-name>
  	<servlet-class>com.zoufang.servlet.Register</servlet-class>
  </servlet>
  <servlet-mapping>
  	<servlet-name>Register</servlet-name>
	<url-pattern>/register</url-pattern>
  </servlet-mapping>

  <servlet>
  	<servlet-name>Delete</servlet-name>
  	<servlet-class>com.zoufang.servlet.Delete</servlet-class>
  </servlet>
  <servlet-mapping>
  	<servlet-name>Delete</servlet-name>
	<url-pattern>/delete</url-pattern>
  </servlet-mapping>

  <servlet>
  	<servlet-name>Change</servlet-name>
  	<servlet-class>com.zoufang.servlet.Change</servlet-class>
  </servlet>
  <servlet-mapping>
  	<servlet-name>Change</servlet-name>
	<url-pattern>/change</url-pattern>
  </servlet-mapping>

</web-app>

6、需要加入一个mysql驱动包

mysql-connector-java-5.1.7-bin.jar

经验:开始没有加这个包,数据库连接不上

时间: 2024-10-08 19:15:12

一个简单的登陆注册系统的相关文章

(二)简单的登陆注册系统--增加验证码部分

login.html <html> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <h1>登陆系统</h1> <form action="login1.php" method="post"> 用户名:<input type="text"  name=&q

一个简单的登陆注册的页面

首先声明,这行代码不用于开发. 1.login.html <!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <title>登陆页面</title> </head> <style> .box{ height: 30px; width: 300px; position: absolute; top:5

登陆注册系统python初步实现

这几天学了MySQLdb的操作,觉得意犹未尽,python也已经学了有段时间,所以考虑着通过与数据库的集成,写一个简单的登陆.注册和更改密码的系统. 最终代码是写了一百二十多行,基本的功能已经实现,就是界面不太友好,下一步是打算学一些GUI的库,完善界面. 写这个系统的初衷,源于在廖雪峰老师的网站上http://www.liaoxuefeng.com/wiki/001374738125095c955c1e6d8bb493182103fac9270762a000/0013868328251266d

一个简单的情感识别系统的实现

情感识别,主要就是正和负的识别,偶尔也有中性.类别比较少,所以相对于广义的文本分类来说,看起来要简单很多,特别是很多词汇都有很强的倾向性.当然这是针对某一特定的领域.一般的话,情感识别主要用于商品评论,因为不同的商品会有不同的术语之类的专门的某一词汇,所以如果将某一领域的数据集上学到的模型应用在另一个领域的话,可能效果会不很理想. Stephan Raaijmakers等的这篇文章[1]提出的是一种基于阀值投票的方法,即使用独立的分类器去分各个类别的数据,最后使用新领域的数据对分类结果进行投票校

TCP学习之建立一个简单的客户/服务器系统--回射系统

TCP学习之建立一个简单的客户/服务器系统--回射系统 相关的资料可以参考Unix网络编程,这个只是修改版,不需要依赖任何文件,可以独立编译通过,而且是在两台不同的主机上进行的. fggets和fputs这两个函数来自标准I/O函数库,writen和readline见my_unp.h头文件 //my_unp.h #include<stdarg.h> #include<syslog.h> #include<stdio.h> #include<netinet/in.h

【Unity Shaders】Reflecting Your World —— 在Unity3D中创建一个简单的动态Cubemap系统

本系列主要参考<Unity Shaders and Effects Cookbook>一书(感谢原书作者),同时会加上一点个人理解或拓展. 这里是本书所有的插图.这里是本书所需的代码和资源(当然你也可以从官网下载). ========================================== 分割线 ========================================== 写在前面 我们已经学了很多关于反射的内容,但是我们现在的反射并不能实时反射,即当反射物体移动时它们不

一个简单的多用户交互系统的实现

需求如下:创建管理员.教师.学员这三个视图,实现一个简单的课程操作交互 具体实现如下: Homework:│ ├─bin│──────start.py #程序的入口│├─conf│──────config.py #程序用到的文件的路径以及其他关系映射信息│├─core #│──────logger.py#记录日志的逻辑│──────main.py#实现三类用户的登陆逻辑,并利用反射调用不同类的具体方法│──────manager.py#实现了管理员类的各个功能│──────my_pickle.p

【sehll学习】linux运维一个简单shell脚本监控系统内存

学习shell脚本入门后,慢慢要尝试编写一些脚本练练手,在这先简单的学习写个系统内存的监控. 1.首先先要确定一下截取一下需要关注的内存使用值,可使用free 命令来操作 free -m 显示 一般在监控内存是我们都是截取 第三行(-/+ buffers/cache)的值.确定后可以使用管道线和grep命令来获取这个的值. free -m | grep - | awk  '{print $4}' 获取到他的值为  858 当中 grep -  就是匹配一下要选取的内容,不太熟悉的可以学习一下gr

android示例:一个简单的登陆程序

最近写了个简单的登陆程序,有几点收获: 1.懂得如何在LinearLayout中嵌套LinearLayout,完善布局的行列: 2.用android:layout_weight控制控件的比重: 3.用getText()获取EditText内容: 4.熟悉控件的编写,不用再照着书抄写了=.= 代码如下: LoginActivity.java import android.os.Bundle; import android.app.Activity; import android.content.I