基于JSP+Servlet+JavaBean实现用户注册和登录

基于JSP+Servlet+JavaBean实现用户注册和登录

实现一个简单的用户注册登录页面。通过注册页面提交注册信息,若数据库中已存在该用户名,给出提示,重新进入注册页面,当与数据库中的已有用户名不重复时,写入数据库,转向登录页面,当符合数据库中信息时,转向主页。

  采用MVC开发模式

  结构如下图所示:

 1.总页面(deng.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>
<p>操作首页</p>
<hr>
<table>
<tr>
<td><a href="http://localhost:8080/java第十二周/a00/a01a.jsp">注册</a></td>
<td><a href="http://localhost:8080/java第十二周/a00/a02a.jsp">登录</a></td>
</tr>
</table>
</body>
</html>

2.注册页面(a01a.jsp)

  获取注册信息,转向a01a_zhuce.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>
<p>请输入你的注册信息</p>
<hr>
<% 
request.setCharacterEncoding("UTF-8");
String a=(String)request.getAttribute("tixing");
%>
<form action="../a01a_zhuce" method="post">
<table>
<tr><td>用户名:</td><td><input name="xm"></td></tr>
<tr><td>密码:</td><td><input name="mm"></td></tr>
<tr><td><input type="submit" value="提交"></td><td><input type="reset" value="取消"></td></tr>
<%if(a!=null){ %>
<tr><td><%=a%></td></tr>
<% }%>
</table>
</form>
</body>
</html>

3.数据库基本信息(db.properties)

  用以记录登录数据库的基本信息

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/bbb?useUnicode=true&characterEncoding=UTF-8
user=root
password=2411030483

4.用户信息JavaBean类(User.java)

  用以记录注册页面获取的注册信息

package a01;

import java.util.List;

public class User {
private String userName;
private String userPassword;
public User() {
	super();
	// TODO Auto-generated constructor stub
}

public User(String userName, String userPassword) {
	super();
	this.userName = userName;
	this.userPassword = userPassword;
}

public String getUserName() {
	return userName;
}
public void setUserName(String userName) {
	this.userName = userName;
}
public String getUserPassword() {
	return userPassword;
}
public void setUserPassword(String userPassword) {
	this.userPassword = userPassword;
}
public int pdUser(User u){
	int f=0;
	if(u.userName.equals(this.userName))
	{
		f=1;
	}
	return f;
}
public int pdListUser(List<User> u){
	int f=0;
	for(int i=0;i<u.size();i++)
	{
		User u2=u.get(i);
		if(this.pdUser(u2)==1)
		{
			f=1;
			break;
		}
	}
	return f;
}
}

5.数据库连接类(JdbcUtil.java)

  连接数据库

package a01;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

public class JdbcUtil {
private static String driver;
private static String url;
private static String user;
private static String password;
private static Properties pr=new Properties();
public JdbcUtil(){
	super();
}
static 
{
	try {
		pr.load(JdbcUtil.class.getClassLoader().getResourceAsStream("db.properties"));
	    driver=pr.getProperty("driver");
	    url=pr.getProperty("url");
	    user=pr.getProperty("user");
	    password=pr.getProperty("password");
	    try {
			Class.forName(driver);
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
}
public static Connection getConnection() throws SQLException
{
	return DriverManager.getConnection(url,user,password);
}
public static void free(Connection conn,Statement st,ResultSet rs) throws Exception
{
	if(conn!=null){conn.close();}
	if(st!=null){st.close();}
	if(rs!=null){rs.close();}
}
}

6.数据库连接类(UserDao.java)

  对数据库进行插入和全部查询操作

package a01;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class UserDao {
public void add(User user) throws Exception
{
	Connection conn=null;
	PreparedStatement pstmt=null;
	conn=JdbcUtil.getConnection();
	String sql="insert into yanzheng(name,password) values(?,?)";
	pstmt=conn.prepareStatement(sql);
	pstmt.setString(1, user.getUserName());
	pstmt.setString(2, user.getUserPassword());
	pstmt.executeUpdate();
	JdbcUtil.free(conn, pstmt, null);
}
public List<User> QueryAll() throws Exception
{
	Connection conn=null;
	PreparedStatement pstmt=null;
	ResultSet rs=null;
	conn=JdbcUtil.getConnection();
	List<User> userList=new ArrayList<User>();
	String sql="select * from yanzheng";
	pstmt=conn.prepareStatement(sql);
	rs=pstmt.executeQuery();
	while(rs.next())
	{
		String xm=rs.getString("name");
		String mm=rs.getString("password");
		User user=new User(xm,mm);
		userList.add(user);
	}
	JdbcUtil.free(conn, pstmt, rs);
	return userList;
}
}

7.注册信息验证操作Servlet类(a01a_zhece.jsp)

  一个Servlet类,通过调用User.java和UserDao.java,完成具体信息验证和数据库操作

package a01;

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

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

/**
 * Servlet implementation class a01a_zhuce
 */
@WebServlet("/a01a_zhuce")
public class a01a_zhuce extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public a01a_zhuce() {
        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
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		UserDao dao=new UserDao();
		String xm=request.getParameter("xm");
		String mm=request.getParameter("mm");
		User user=new User(xm,mm);
		List<User> list=null;
		try {
			list=dao.QueryAll();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		int f=user.pdListUser(list);
		if(f==1)
		{
			String a="注册失败";
			request.setAttribute("tixing", a);
			request.getRequestDispatcher("/a00/a01a.jsp").forward(request, response);
		}
		else
		{
			try {
				dao.add(user);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			request.getRequestDispatcher("/a00/a02a.jsp").forward(request,response);
		}
	}

}

8.登录页面

  获取登陆信息,转向a01a_denglu.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>
<p>请输入你的登录信息</p>
<hr>
<% 
request.setCharacterEncoding("UTF-8");
String a=(String)request.getAttribute("tixing");
%>
<form action="../a01a_denglu" method="post">
<table>
<tr><td>用户名:</td><td><input name="xm"></td></tr>
<tr><td>密码:</td><td><input name="mm"></td></tr>
<tr><td><input type="submit" value="提交"></td><td><input type="reset" value="取消"></td></tr>
<%if(a!=null){ %>
<tr><td><%=a%></td></tr>
<% }%>
</table>
</form>
</body>
</html>

9.登陆信息验证Servlet类(a01a_denglu.jsp)

  一个Servlet类,通过调用User.java和UserDao.java.完成登录验证操作

package a01;

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

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

/**
 * Servlet implementation class a01a_denglu
 */
@WebServlet("/a01a_denglu")
public class a01a_denglu extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public a01a_denglu() {
        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
		doPost(request,response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		 request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		UserDao dao=new UserDao();
		String xm=request.getParameter("xm");
		String mm=request.getParameter("mm");
		User user=new User(xm,mm);
		List<User> list=null;
		try {
			list=dao.QueryAll();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		int f=user.pdListUser(list);
		if(f==1)
		{
			request.getRequestDispatcher("/a00/a03a.jsp").forward(request, response);
		}
		else
		{
			String a="登录失败";
			request.setAttribute("tixing", a);
			request.getRequestDispatcher("/a00/a02a.jsp").forward(request,response);
		}
	}

}

10.登陆成功页面

  登陆成功

<%@ 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>
<p>恭喜你登陆成功</p>
<hr>
</body>
</html>

猜你喜欢

转载自blog.csdn.net/qq_43238335/article/details/106115304