代码:
/////////////////////////////////////////
LoginServlet.java
package login;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class LoginServlet extends HttpServlet {
public void init() throws ServletException {
}
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");
//从请求中取出用户名和密码的值
String username = request.getParameter("username");
String password = request.getParameter("password");
//生成一个ArrayList对象,并把用户名和密码的值存入该对象中.
ArrayList arr = new ArrayList();
arr.add(username);
arr.add(password);
//生成一个session对象
HttpSession session = request.getSession(true);
session.removeAttribute("username");
session.setAttribute("usrename", username);
//调用模型组件loginHandler,检查该用户是checkLogin否已经注册
LoginHandler login = new LoginHandler();
try {
//判断下
boolean mark = login.checkLogin(arr);
if(mark)
response.sendRedirect("main.jsp");
else response.sendRedirect("register.jsp");
} catch (Exception e) {
e.printStackTrace();
}
}
public LoginServlet() {
super();
}
public void destroy() {
super.destroy();
}
}
///////////////////////////////////////////
LoginHandler.java
package login;
import java.sql.*;
import java.util.*;
class LoginHandler {
public LoginHandler(){}
Connection conn;
PreparedStatement ps;
ResultSet rs;
//检查是否已经注册
public boolean checkLogin(ArrayList arr){
//数据库中访问组件DBPoolZ中取得连接
conn = DBPool.getConnection();
String name = (String)arr.get(0);
String password = (String)arr.get(1);
try
{
// rs = ps.executeQuery("select * from bbs.t_userinfo where username=? and password=?");
// while(rs.next())
// {
// if(name.equals(rs.getString("username"))&&password.equals(rs.getString("password"))) {
// DBPool.dbClose(conn,ps,rs);
// return true;
// }
// else return false;
// }
String sql = "select * from bbs.t_userinfo where username=? and password=?";
ps = conn.prepareStatement(sql);
ps.setString(1,name);
ps.setString(2,password);
rs = ps.executeQuery();
if(rs.next()){
DBPool.dbClose(conn,ps,rs);
return true;
}
else {
DBPool.dbClose(conn,ps,rs);
return false;
}
}
catch(SQLException e){return false;}
}
}
////////////////////////////////////////////////////////////////////
DBPool.java
package login;
import java.io.*;
import java.sql.*;
import java.util.Properties;
public class DBPool {
private static DBPool instance = null;
//取得连接
public static synchronized Connection getConnection(){
if(instance == null){
instance = new DBPool();
}
return instance._getConnection();
}
private DBPool(){super();}
private Connection _getConnection(){
try{
String sDBDriver = null;
String sConnection = null;
String sUser = null;
String sPassword = null;
Properties p = new Properties();
InputStream is = getClass().getResourceAsStream("/db.properties");
p.load(is);
sDBDriver = p.getProperty("DBDriver",sDBDriver);
sConnection = p.getProperty("Connection", sConnection);
sUser = p.getProperty("User",sUser);
sPassword = p.getProperty("Password",sPassword);
Properties pr = new Properties();
pr.put("user", sUser);
pr.put("password", sPassword);
pr.put("characterEncoding", "GB2312");
pr.put("useUnicode", "TRUE");
Class.forName(sDBDriver);
return DriverManager.getConnection(sConnection,pr);
}
catch(Exception e){
System.out.println(e);
return null;
}
}
//释放资源
public static void dbClose(Connection conn,PreparedStatement ps,ResultSet rs)
throws SQLException
{
rs.close();
ps.close();
conn.close();
}
}
//////////////////////////////////
db.properties
DBDriver = com.mysql.jdbc.Driver
Connection = jdbc:mysql://localhost:3306/bbs/username=? and password=?
User =
Password =
////////////////////////////
login.jsp
<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
用户名:
密码:
//////////////////////////////////////////////////////
两外两个页面就不写了。
求高手指导指导