JSP实现抽奖效果和Session通信

JSP抽奖小程序

Java代码

info.jsp代码

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@page import="java.security.AllPermission"%>
<%
	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 'info.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>
		<%
			HashMap<String, Integer> scoreMap = (HashMap<String, Integer>)application.getAttribute("scoreMap");
			if(scoreMap == null){
				scoreMap = new HashMap<String, Integer>();
			}
			String ip = request.getRemoteAddr();

			Integer score = scoreMap.get(ip);

			if (score == null) {
				score = new Random().nextInt(7) - 1;
				scoreMap.put(ip, score);
			}

			System.out.println(ip + ":" + score);
			application.setAttribute("scoreMap",scoreMap);

			request.setAttribute("score", score);
			request.getRequestDispatcher("draw.jsp").forward(request, response);		
		%>
	</body>
</html>

页面代码

draw.jsp代码

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@page import="java.util.Map.Entry"%>
<%
	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">
	-->
		<style type="text/css">
body {
	text-align: center;
}

.btn {
	width: 300px;
	height: 150px;
	font-size: 60px;
	background: red;
	color: white;
}

.head {
	font-size: 60px;
	color: red;
}

.bottom {
	font-size: 40px;
	color: blue;
}
</style>

	</head>

	<body>
		<%
			HashMap<String, Integer> scoreMap = (HashMap<String, Integer>) application
					.getAttribute("scoreMap");
			Integer score = (Integer) request.getAttribute("score");
			String ip = (String) request.getAttribute("ip");
			String msg = "";
			if (score != null) {
				switch (score) {
				case -1:
					msg = "恭喜你要请我吃六顿饭";
					break;
				case 0:
					msg = "恭喜你要请我吃五顿饭";
					break;
				case 1:
					msg = "恭喜你要请我吃四顿饭";
					break;
				case 2:
					msg = "恭喜你要请我吃三顿饭";
					break;
				case 3:
					msg = "恭喜你要请我吃两顿饭";
					break;
				case 4:
					msg = "恭喜你要请我吃一顿饭";
					break;
				case 5:
					msg = "恭喜你啊,运气真好,请我吃一个月饭";
					break;
				}
			}
		%>
		<h1 class="head">
			点击开始抽奖
		</h1>
		<form action="info.jsp" method="post">
			<input class="btn" type="submit" value="抽奖" />
		</form>
		<h1 class="bottom"><%=msg%><br />
		</h1>

	</body>
</html>

浏览效果

打开效果:
在这里插入图片描述
点击抽奖之后:
在这里插入图片描述
这样一个抽奖程序就完成了

Session通信效果

在抽奖程序的基础下在添加以下代码

Java代码

LoginServlet代码

package top.xinsir.test;

import java.io.IOException;
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 top.xinsir.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 {
		// 设置编码
		request.setCharacterEncoding("utf-8");

		// 获取参数
		String userName = request.getParameter("userName");
		String pwd = request.getParameter("pwd");

		HttpSession session = request.getSession();

		Connection conn = null;
		ResultSet rs = null;
		PreparedStatement ps = null;

		conn = DBUtil.getConn();
		String sql = "SELECT * FROM users WHERE user_name=? AND pwd=?";
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, userName);
			ps.setString(2, pwd);

			rs = ps.executeQuery();
			if (rs.next()) {
				session.setAttribute("userName", userName);
				response.sendRedirect("result.jsp");
			} else {
				request.setAttribute("msg", "账户名或密码错误");
				request.getRequestDispatcher("login.jsp").forward(request,
						response);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}

	}

}

页面代码

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%>">

<title>My JSP 'login.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 msg = (String) request.getAttribute("msg");
	    	if(msg==null)
	    	msg="";
	%>
	<form action="login" method="post">
		用户名:
		<input type="text" name="userName" /><br />
		密码:
		<input type="text" name="pwd" />
		<span style="color:red"><%=msg %></span><br />
		<input type="submit" value="登录" />
	</form>
</body>
</html>

result.jsp代码

查看用户抽奖的页面,先用session判断是否有用户名,然后从登录页面获取账号密码登陆之后,在info.jsp页面获取ip和抽奖的等级写到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 'result.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 {
			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>

浏览效果

先在draw.jsp页面点击一次抽奖
在这里插入图片描述
然后在地址栏输入result.jsp,会跳转到login.jsp页面
在这里插入图片描述
随便输入数据库没有的用户名和密码
在这里插入图片描述
点击登录之后还是在login.jsp页面,但是密码后面会显示账户名或密码错误
在这里插入图片描述
输入数据库中存储有的用户名和密码
在这里插入图片描述
点击登录之后
在这里插入图片描述
这样就完成了!

  • 4
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值