这里用到的软件分别如下:
Tomcat:apache-tomcat-6.0.29.zip
Mysql:mysql-essential-5.1.50-win32.msi
JDBC:mysql-connector-java-5.1.13-bin.jar
Myeclipse:myeclipse-8.6.0-win32.exe
这里要做的配置如下:
mysql默认安装,建立root账户,密码root
mysql-connector-java-5.1.13-bin.jar放到Web应用的WEB-INF/lib/目录下或者是tomcat的lib/目录下
这里要制作的效果如下:
登录页面:
注销页面:
这里用到的关键代码如下:
SQL:
createdatabasetest;usetest;CREATETABLEtest.user(
pidvarchar(45)defaultNULL,
usernamevarchar(45)NOTNULL,
passwordvarchar(45)NOTNULL)DEFAULTCHARSET=GB2312;INSERTuser(pid, username, password)VALUES('1','root','root');INSERTuser(pid, username, password)VALUES('2','admin','admin');
首页(视图):
1
2 3 欢迎您回来:4 注销登录(控制器):
1 publicvoiddoGet(HttpServletRequest request, HttpServletResponse response)2 throwsServletException, IOException {3 4 HttpSession session=request.getSession();5 ServletContext application=this.getServletContext();6 7 if(null==session.getAttribute("username")) {8 try{9 //获得表单数据10 String username=request.getParameter("username");11 String password=request.getParameter("password");12 13 //建立数据库连接14 Connection conn=DBUtil.getConnection();15 16 //定义查询语句17 String sql="select * from user where `username`=? and `password`=?";18 PreparedStatement pstmt=conn.prepareStatement(sql);19 pstmt.setString(1, username);20 pstmt.setString(2, password);21 22 //查询获得结果集23 ResultSet rs=pstmt.executeQuery();24 if(rs.next()) {25 session.setAttribute("username", rs.getString("username"));26 //session.setAttribute("password", rs.getString("password"));27 intolMember=((Integer) application.getAttribute("onlineMember")).intValue();28 application.setAttribute("onlineMember", olMember+1);29 }30 31 //关闭结果集,查询语句,数据库连接32 rs.close();33 pstmt.close();34 conn.close();35 }catch(InstantiationException e) {36 //TODO Auto-generated catch block37 e.printStackTrace();38 }catch(IllegalAccessException e) {39 //TODO Auto-generated catch block40 e.printStackTrace();41 }catch(ClassNotFoundException e) {42 //TODO Auto-generated catch block43 e.printStackTrace();44 }catch(SQLException e) {45 //TODO Auto-generated catch block46 e.printStackTrace();47 }48 }49 50 //返回上一个页面51 response.sendRedirect(request.getHeader("Referer"));52 }
注销(控制器):
1 publicvoiddoGet(HttpServletRequest request, HttpServletResponse response)2 throwsServletException, IOException {3 4 HttpSession session=request.getSession();5 ServletContext application=this.getServletContext();6 7 //如果此用户没有登录过8 if(null!=session.getAttribute("username")) {9 intolMember=((Integer)application.getAttribute("onlineMember")).intValue();10 application.setAttribute("onlineMember", olMember-1);11 //session.invalidate();12 session.removeAttribute("username");13 }14 15 //返回上一个页面16 response.sendRedirect(request.getHeader("Referer"));17 }
数据库(模型):
1 publicclassDBUtil {2 privatestaticString username="root";3 privatestaticString password="root";4 privatestaticString driver="com.mysql.jdbc.Driver";5 privatestaticString url="jdbc:mysql://localhost:3306/test";6 7 //得到数据库连接8 publicstaticConnection getConnection()throwsInstantiationException, IllegalAccessException, ClassNotFoundException, SQLException {9 Class.forName(driver).newInstance();10 returnDriverManager.getConnection(url, username, password);11 }12 13 }
Web配置页面:
1 2 5 6 listener.WebListener7 8 9 ServletWebInitializer10 servlet.ServletWebInitializer11 112 13 14 ServletLogin15 servlet.ServletLogin16 17 18 ServletLogout19 servlet.ServletLogout20 21 22 23 ServletWebInitializer24 /servlet/ServletWebInitializer25 26 27 ServletLogin28 /servlet/ServletLogin29 30 31 ServletLogout32 /servlet/ServletLogout33 34 35 index.jsp36 37
Web初始化:
1 publicvoidinit()throwsServletException {2 //Put your code here3 ServletContext app=this.getServletContext();4 app.setAttribute("onlineMember",newInteger(0));//在线会员人数5 app.setAttribute("onlineNumber",newInteger(0));//当前在线人数6 app.setAttribute("totalNumber",newInteger(0));//历史访问人数7 }
Web监听器:
1 publicclassWebListenerimplementsHttpSessionListener {2 3 publicWebListener() {4 //TODO Auto-generated constructor stub5 }6 7 publicvoidsessionCreated(HttpSessionEvent se) {8 //TODO Auto-generated method stub9 10 ServletContext app=se.getSession().getServletContext();11 intolCount=((Integer)app.getAttribute("onlineNumber")).intValue();12 app.setAttribute("onlineNumber", olCount+1);13 14 intttlCount=((Integer)app.getAttribute("totalNumber")).intValue();15 app.setAttribute("totalNumber", ttlCount+1);16 }17 18 publicvoidsessionDestroyed(HttpSessionEvent se) {19 //TODO Auto-generated method stub20 ServletContext app=se.getSession().getServletContext();21 intolCount=((Integer)app.getAttribute("onlineNumber")).intValue();22 app.setAttribute("onlineNumber", olCount-1);23 }24 25 }
总结如下:
1. 如果要在Web应用中初始化一些值,那么可以采用在web.xml加入1个或者多个特殊的servlet,并设置对应的servlet配置:
ServletWebInitializer
servlet.ServletWebInitializer
x(x>=1,顺序越小启动优先级越高)
将要初始化的内容写在这些特殊的servlet的init()方法内:
public void init() throws ServletException
2. 如果在一个新会话的开始,或者一个会话的结束时要进行某些计算,比如统计在线人数,那么可以在web.xml加入1个或者多个监听器:
listener.WebListener
对应的计算放在sessionCreated和sessionDestroyed里面:
public class WebListener implements HttpSessionListener
public void sessionCreated(HttpSessionEvent se)
public void sessionDestroyed(HttpSessionEvent se)
3. mysql数据库采用jdbc连接的步骤如下:
1 publicclassDBUtil {2 privatestaticString username="root";3 privatestaticString password="root";4 privatestaticString driver="com.mysql.jdbc.Driver";5 privatestaticString url="jdbc:mysql://localhost:3306/test";6 7 //得到数据库连接8 publicstaticConnection getConnection()throwsInstantiationException, IllegalAccessException, ClassNotFoundException, SQLException {9 Class.forName(driver).newInstance();10 returnDriverManager.getConnection(url, username, password);11 }12 13 }
// 建立数据库连接
Connection conn = DBUtil.getConnection();
// 定义查询语句
String sql = "select * from user where `username`=? and `password`=?";
PreparedStatement pstmt = conn.prepareStatement(sql);
// 查询获得结果集
ResultSet rs = pstmt.executeQuery();
// 关闭结果集,查询语句,数据库连接
rs.close();
pstmt.close();
conn.close();
4. jsp页面除了少量的if语句,基本上都是输出语句,将数据计算放到对应的JavaBean或者控制器内部完成。