package dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import dbc.JdbcUtil;
public class UserDao {
public boolean register(String username,String userpw,String identity)throws Exception{
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
conn = JdbcUtil.getConnection();
String sql = "select * from user where username=? and userpw=? and identity=?";
ps = conn.prepareStatement(sql);
ps.setString(1,username);
ps.setString(2, userpw);
ps.setString(3, identity);
rs = ps.executeQuery();
if (rs.next()) return true;
else return false;
}
}
`package dbc;
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();
private JdbcUtil() {
}
// ��Ƹù�����ľ�̬��ʼ�����еĴ��룬�ô�����װ����ʱִ�У���ִֻ��һ��
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");
Class.forName(driver);
} catch (Exception e) {
throw new ExceptionInInitializerError(e);
}
}
// ��ƻ�����Ӷ���ķ���getConnection()
public static Connection getConnection() throws SQLException {
return DriverManager.getConnection(url, user, password);
}
// ����ͷŽ���������ӵķ���free()
public static void free(ResultSet rs, Statement st, Connection conn) {
try {
if (rs != null) {
rs.close();
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (st != null) {
st.close();
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
}
}
}
}
}
```package servlets;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import dao.UserDao;
import vo.User;
import java.sql.*;
public class RegistServlet extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 1L;
/**
* Constructor of the object.
*/
public RegistServlet() {
super();
}
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request,response);
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String un=request.getParameter("username");
String uw=request.getParameter("userpw");
String iden=request.getParameter("identity");
User uu=new User(un,uw,iden);
UserDao c1 = null;
PrintWriter out = null;
try {
if(c1.register(uu.getUsername(), uu.getUserpw(), uu.getIdentity()))
request.getRequestDispatcher("/output.jsp").forward(request, response);
else
out.print("<script> alert(\"登录失败!\"); </script>");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// TODO Auto-generated catch block
/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}
}
```package vo;
public class User {
private String username;
private String userpw;
private String identity;
public User(String username, String userpw, String identity) {
this.identity=identity;
this.username=username;
this.userpw=userpw;
// TODO Auto-generated constructor stub
}
public String getUsername(){return username;}
public String getUserpw(){return userpw;}
public String getIdentity(){return identity;}
public void setUsername(String username){this.username=username;}
public void setUserpw(String userpw){this.userpw=userpw;}
public void setIdentity(String identity){this.identity=identity;}
}
url=jdbc:mysql://localhost:3306/LibSytem?useUnicode=true&characterEncoding=utf-8
user=root
password=960714
```<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head><title>登录页面</title> </head>
<body>
<center><h1>图书馆用户登录</h1></center>
<center>
<hr width="100%" size="1" color="black">
<form action="RegistServlet" method="post">
<table>
<tr><td>登录名:</td><td><input type="text" name="username"></td></tr>
<tr><td>登录密码:</td><td><input type="password" name="userpw"></td></tr>
<tr><td>身份:</td><td><select name="identity">
<option selected>用户</option>
<option>管理员</option>
</select></td></tr>
<tr><td><input type="submit" value="登录"></td></tr>
</table>
</form>
</center>
</body>
</html>
```<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>成功</title>
</head>
<body>
This is my JSP page. <br>
</body>
</html>
``