目录
JSP抽奖小程序Java代码info.jsp代码
页面代码draw.jsp代码
浏览效果
Session通信效果Java代码LoginServlet代码
页面代码login.jsp代码result.jsp代码
浏览效果
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");
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页面,但是密码后面会显示账户名或密码错误 输入数据库中存储有的用户名和密码 点击登录之后 这样就完成了!