1. 程式人生 > >jsp+Servlet+javaBean實現登入和註冊

jsp+Servlet+javaBean實現登入和註冊

目錄:


資料庫設計:

CREATE SCHEMA `student` ;
CREATE  TABLE `student`.`user` (
`id` INT NOT NULL AUTO_INCREMENT ,  
`user` VARCHAR(45) NOT NULL ,  
`pwd` VARCHAR(45) NOT NULL ,  
`name` VARCHAR(45) NOT NULL ,  
`xxx` VARCHAR(45) NOT NULL ,  
`age` INT NOT NULL ,  
PRIMARY KEY (`id`) );

資料庫互動介面:

web.xml

<servlet>
		
		<servlet-name>loginServlet</servlet-name>
		<servlet-class>servlet.loginServlet</servlet-class>
	</servlet>
	<servlet-mapping>
		<servlet-name>loginServlet</servlet-name>
		<url-pattern>/loginServlet</url-pattern>
	</servlet-mapping>
	<servlet>
		
		<servlet-name>addUserServlet</servlet-name>
		<servlet-class>servlet.addUserServlet</servlet-class>
	</servlet>
	<servlet-mapping>
		<servlet-name>addUserServlet</servlet-name>
		<url-pattern>/addUserServlet</url-pattern>
	</servlet-mapping>

index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>歡迎來到學生管理系統</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">

</head>

<body>
	<div align="center">
		<font size=" 2" color="#FF6633">使用者登入</font>
	</div>
	<form id="form1" name="form1" method="post" action="loginServlet">
		<table width="357" border="0" align="center">
			<tr>
				<td width="128">使用者名稱:</td>
				<td width="219"><label> <input name="user" type="text"
						id="user" /> </label></td>
			</tr>
			<tr>
				<td>密 碼:</td>
				<td><label> <input name="pwd" type="password" id="pwd" />
				</label></td>
			</tr>
			<tr>
				<td><label> <input type="submit" name="Submit"
						value="登入" /> </label></td>
				<td><label><a href="addUser.jsp">使用者註冊</a> </label></td>
			</tr>
		</table>
		<p> </p>
	</form>
</body>
</html>

介面:

addUser.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>使用者註冊</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">


</head>

<body>
	<p align="center">
		<font size=" 3" color="#FF3300">使用者註冊</font>
	</p>
	<form id="form1" name="form1" method="post" action="addUserServlet">
		<table width="340" border="0" align="center">
			<tr>
				<td width="141">使用者名稱:</td>
				<td width="189"><label> <input name="user" type="text"
						id="user" /> </label></td>
			</tr>
			<tr>
				<td>密碼:</td>
				<td><label> <input name="pwd" type="password" id="pwd" />
				</label></td>
			</tr>
			<tr>
				<td>重輸密碼:</td>
				<td><label> <input name="pwd1" type="password"
						id="pwd1" /> </label></td>
			</tr>
			<tr>
				<td>姓名:</td>
				<td><label> <input name="name" type="text" id="name" />
				</label></td>
			</tr>
			<tr>
				<td>性別:</td>
				<td><label> <input name="xxx" type="radio" value="男"
						checked="checked" /> 男 <input type="radio" name="xxx" value="女" />
						女</label></td>
			</tr>
			<tr>
				<td>年齡:</td>
				<td><label> <input name="age" type="text" id="age" />
				</label></td>
			</tr>
			<tr>
				<td><label> <input type="submit" name="Submit"
						value="提交" /> </label></td>
				<td><label> <input type="reset" name="Submit2"
						value="重置" /> </label></td>
			</tr>
		</table>
	</form>
</body>
</html>

介面:

User.java

package bean;

public class User {
	private String user;
	private String pwd;
	private String name;
	private String xxx;
	private int age;

	public String getUser() {
		return user;
	}

	public void setUser(String user) {
		this.user = user;
	}

	public String getPwd() {
		return pwd;
	}

	public void setPwd(String pwd) {
		this.pwd = pwd;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getxxx() {
		return xxx;
	}

	public void setxxx(String xxx) {
		this.xxx = xxx;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}
}

DAO.java

package dao;

import getConnection.GetConnection;

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

import com.mysql.jdbc.*;

import bean.User;

public class DAO {
	private Connection conn;
	private PreparedStatement pstat;
	String sql = "";

	/**
	 * 
	 * 使用者登入
	 */
	public boolean logoin(User user) throws SQLException {
		conn = GetConnection.getConnection();
		boolean i = false;
		sql = "select * from user where user=? and pwd=?";

		pstat = conn.prepareStatement(sql);

		pstat.setString(1, user.getUser());
		pstat.setString(2, user.getPwd());

		ResultSet rs1 = (ResultSet) pstat.executeQuery();
		if (rs1.next()) {
			i = true;
			rs1.close();
			pstat.close();
		} else {
			i = false;
			rs1.close();
			pstat.close();
		}
		conn.close();
		return i;
	}

	/**
	 * 使用者註冊
	 */
	public void addUser(User user) {
		conn = GetConnection.getConnection();
		sql = "insert into user values(?,?,?,?,?,?)";
		try {
			pstat = conn.prepareStatement(sql);
			pstat.setString(1, null);
			pstat.setString(2, user.getUser());
			pstat.setString(3, user.getPwd());
			pstat.setString(4, user.getName());
			pstat.setString(5, user.getxxx());
			pstat.setInt(6, user.getAge());
			pstat.executeUpdate();
			pstat.close();
			conn.close();

		} catch (SQLException e) {
			e.printStackTrace();
		}

	}
}


GetConnection.java

package getConnection;

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

/**
 * 
 * 用JDBC的方法獲得資料庫的連線
 * 
 */
public class GetConnection {
	// 通過靜態方法註冊驅動,獲得連線

	public static Connection getConnection() {
		String driver = "com.mysql.jdbc.Driver";
		String url = "jdbc:mysql://localhost/student";
		Connection con = null;
		try {
			Class.forName(driver);
			try {
				con = DriverManager.getConnection(url, "root", "root");
			} catch (SQLException e) {
				e.printStackTrace();
			}
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		System.out.println("已獲得資料庫的連線");
		return con;
	}
	/*
	 * public static void main(String args[]){ getConnection(); }
	 */
}

loginServlet.java

package servlet;

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

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

import dao.DAO;

import bean.User;

public class loginServlet extends HttpServlet {
	public void destory() {
		super.destroy();
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		String name = new String(request.getParameter("user").getBytes(
				"ISO8859_1"), "GBK");
		String pwd = new String(request.getParameter("pwd").getBytes(
				"ISO8859_1"), "utf-8");
		User user = new User();
		user.setUser(name);
		user.setPwd(pwd);
		DAO dao = new DAO();
		boolean isLogin;
		try {
			isLogin = dao.logoin(user);
			if (isLogin) {
				response.sendRedirect("MYJSP.jsp");
			} else {
				response.sendRedirect("index.jsp");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}

	public void init() throws ServletException {

	}
}

addUserServlet.java

package servlet;

import java.io.IOException;

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

import dao.DAO;

import bean.User;

public class addUserServlet extends HttpServlet {

	/**
	 * Destruction of the servlet.
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log

		// Put your code here

	}

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html");
		doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html");
		response.setCharacterEncoding("utf-8");
		String user1 = request.getParameter("user");
		String pwd = request.getParameter("pwd");
		String name = new String(request.getParameter("name").getBytes(
				"ISO8859_1"), "utf-8");
		String xxx = new String(request.getParameter("xxx").getBytes(
				"ISO8859_1"), "utf-8");
		String age1 = request.getParameter("age");
		User user = new User();
		user.setUser(user1);
		user.setPwd(pwd);
		user.setName(name);
		user.setxxx(xxx);
		int age;
		if (age1 != null) {
			age = Integer.parseInt(age1);
			user.setAge(age);
		}
		DAO dao = new DAO();
		dao.addUser(user);

		request.setAttribute("info", new String(" 新增成功!恭喜!!" + " "));
		request.setAttribute("id", new String("a"));
		request.setAttribute("denglu", new String(" 登陸"));

		request.getRequestDispatcher("info.jsp").forward(request, response);
	}

	public void init() throws ServletException {
		// Put your code here
	}

}