DBUtil工具类
package org.work.util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class DBUtil {
static {
try {
// 加载jdbc驱动程序
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public static Connection getConn() {
Connection conn = null;
try {
// 使用DriverManager建立数据库连接
conn = DriverManager.getConnection(
"jdbc:sqlserver://127.0.0.1:1433;databaseName=consumer",
"sa", "1");
} catch (SQLException e) {
System.err.println("连接失败");
}
return conn;
}
public static void getClose(PreparedStatement ps, ResultSet rs,
Connection conn) {
// 关闭资源
try {
if (ps != null) {
ps.close();
}
if (rs != null) {
rs.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
draw.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>My JSP 'draw.jsp' starting page</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">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<%
Integer score = (Integer) request.getAttribute("score");
String msg = "";
if (score != null) {
switch (score) {
case -1:
msg = "喜提25遍";
break;
case 0:
msg = "喜提25遍";
break;
case 1:
msg = "喜提20遍";
break;
case 2:
msg = "喜提15遍";
break;
case 3:
msg = "喜提10遍";
break;
case 4:
msg = "喜提5遍";
break;
case 5:
msg = "玩耍";
break;
}
}
%>
<%=msg%><br />
<form action="new.jsp" method="post">
<input type="submit" value="抽奖">
</form>
</body>
</html>
new.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>My JSP 'new.jsp' starting page</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">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<%
//从application内置对象中获取map
HashMap<String, Integer> scoreMap = (HashMap<String, Integer>) application
.getAttribute("scoreMap");
//如果application中没有map,新建一个map
if (scoreMap == null) {
scoreMap = new HashMap<String, Integer>();
}
String ip = request.getRemoteAddr();
//从map中查找是否有数据,如果没有数据就代表第一次访问,如果有数据就代表已经访问过
Integer score = scoreMap.get(ip);
if (score == null) {
score = new Random().nextInt(7) - 1;
scoreMap.put(ip, score);
}
//将上边操作过的map存入(更新)到application中
application.setAttribute("scoreMap", scoreMap);
request.setAttribute("score", score);
request.getRequestDispatcher("draw.jsp").forward(request, response);
%>
</body>
</html>
login.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%>">
<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">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<style type="text/css">
table {
margin: 0 auto;
}
.login {
margin: 0 auto;
}
</style>
</head>
<%
String msg = (String) request.getAttribute("msg");
if (msg == null)
msg = "";
%>
<body>
<form action="login" method="post">
<table>
<tr>
<td>
用户名:
</td>
<td>
<input type="text" name="userName">
</td>
</tr>
<tr>
<td>
密码:
</td>
<td>
<input type="password" name="pwd">
</td>
</tr>
<tr>
<td colspan="2">
<span style="color: red"><%=msg%></span>
</td>
</tr>
<tr>
<td colspan="2" class="login">
<input type="submit" value="登录">
</td>
</tr>
</table>
</form>
</body>
</html>
LoginServlet.java文件
package org.work.test;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.work.util.DBUtil;
public class LoginServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 1.设置编码
request.setCharacterEncoding("utf-8");
// 2.获取参数
String userName = request.getParameter("userName");
String pwd = request.getParameter("pwd");
HttpSession session = request.getSession();
System.out.println(session.getId());
// 连接数据库查询是否存在这样一条数据 如果存在跳到result.jsp
String sql = "SELECT * FROM users WHERE user_name=? and pwd=?";
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
boolean isTrue = false;
// 调用util包下的DBUtil.getConn();加载驱动并和数据库建立连接
conn = DBUtil.getConn();
try {
// 准备一个sql语句
// 使用prepareStatement访问数据库
ps = conn.prepareStatement(sql);
ps.setString(1, userName);
ps.setString(2, pwd);
// 使用ResultSet接收访问数据库的反馈结果
rs = ps.executeQuery();
if (rs.next()) {
isTrue = true;
} else {
isTrue = false;
}
} catch (SQLException e) {
e.printStackTrace();
}
// 调用util包下的getClose();方法关闭资源
DBUtil.getClose(ps, rs, conn);
// 3.输出
if (isTrue) {
session.setAttribute("userName", userName);
response.sendRedirect("result.jsp");
} else {
request.setAttribute("msg", "帐户名或密码错误!");
request.getRequestDispatcher("login.jsp")
.forward(request, response);
}
}
}
result.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>My JSP 'index.jsp' starting page</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">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<%
String userName = (String) session.getAttribute("userName");
if (userName == null || "".equals(userName)) {
response.sendRedirect("login.jsp");
} else {
System.out.println("jsp:" + session.getId());
HashMap<String, Integer> scoreMap = (HashMap<String, Integer>) application
.getAttribute("scoreMap");
//遍历map
if (scoreMap != null)
for (String s : scoreMap.keySet()) {
String key = s;
Integer score = scoreMap.get(key);
out.println(key + ":" + score + "分<br/>");
}
}
%>
</body>
</html>
最后页面的运行结果如下图:
抽奖页面
抽奖成功
登录页面
登陆失败页面
登陆成功页面