基於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>