JSP实现后台登录
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="content-type" content="text/html; charset=utf-8">
<script type="text/javascript">
function check_admin(form){
if(form.user.value==""){
alert("用户名不能为空");
form.user.focus();
return false;
}
if(form.pass.value==""){
alert("密码不能为空");
form.pass.focus();
return false;
}
if(form.verify.value==""){
alert("验证码不能为空");
form.verify.focus();
return false;
}
}
</script>
</head>
<body>
<center>
<form action="verify.jsp" method="post" onSubmit="return check_admin(this);">
<table width="400" border="1" bordercolor="#99ccff" cellpadding="0" cellspacing="0" style="border-collapse: collapse;">
<tr>
<td height="40" colspan="2" align="center">
<font>后台登录</font>
</td>
</tr>
<tr>
<td height="40" align="right"> 用户名 </td>
<td width="200" align="left" valign="middle">
<input type="text" name="user" size="21" />
</td>
</tr>
<tr>
<td height="40" align="right"> 密码 </td>
<td width="40" align="left" valign="middle">
<input type="password" name="pass" size="21" />
</td>
</tr>
<tr>
<td height="40" align="right"> 验证码 </td>
<td >
<img border="0" src="verifyCode.jsp">
</td>
</tr>
<tr>
<td height="40" align="right"> 输入验证码 </td>
<td>
<input type="text" name="verify" maxlength="4" size="21" value="" />
</td>
</tr>
<tr>
<td height="40" colspan="2" align="center">
<input type="submit" />
<input type="reset" />
</td>
</tr>
</table>
</form>
</center>
</body>
</html>
login_ok.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_ok.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 strLogin=(String)session.getAttribute("login");
String strUser=(String)session.getAttribute("me");
if (strLogin==null)
{
out.println("<h2>请先登录,谢谢!</h2>");
out.println("<h2>5秒钟后,自动跳转到登录页面!</h2>");
response.setHeader("Refresh","5;URL=login.jsp");
}
else
{
if (strLogin.equals("ok"))
{
out.println(strUser+"欢迎进入我们的网站!");
}
else
{
out.println("<h2>用户名或密码错误,请重新登录!</h2>");
out.println("<h2>5秒钟后,自动跳转到登录页面!</h2>");
response.setHeader("Refresh","5;URL=login.jsp");
}
}
%>
</body>
</html>
login_error.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_error.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>
*{
margin:0px;
padding: 0px;
}
#t1{
width:500px;
height:300px;
background: #FFE4C4;
position:absolute;
left:50%;
top:50%;
margin:-150px 0 0 -250px;
text-align: center;
line-height: 300px;
font-size:22px;
}
#myProgress {
width: 100%;
height: 30px;
position: relative;
background-color: #ddd;
}
#myBar {
background-color: #4CAF50;
width: 10px;
height: 30px;
position: absolute;
}
</style>
</head>
<body>
<div id="t1">
<!-- <p>密码或验证码输入错误</p> -->
<p>密码或验证码输入错误,0分3秒后可跳转至login页</p>
</div>
<div id="myProgress">
<div id="myBar"></div>
</div>
<script type="text/javascript">
function move() {
var elem = document.getElementById("myBar");
var width = 0;
var id = setInterval(frame, 20);
function frame() {
if (width == 100) {
clearInterval(id);
} else {
width++;
elem.style.width = width + '%';
}
}
}
var maxtime = 2; //按秒计算,自己调整!
function CountDown() {
if (maxtime >= 0) {
minutes = Math.floor(maxtime / 60);
seconds = Math.floor(maxtime % 60);
msg = "密码或验证码输入错误," + minutes + "分" + seconds + "秒后可跳转至login页";
document.getElementById("t1").innerHTML=msg;
//document.all["t1"].innerHTML = msg;
//if (maxtime == 5 * 60)alert("还剩5分钟");
--maxtime;
} else{
clearInterval(timer);
//alert("时间到,结束!");
window.location.href='./login.jsp';
}
}
timer = setInterval("CountDown()", 1000);
move();
</script>
<%-- <div style="font:20px;text-align:center;top:33%;left:33%;position:absolute;border:1px;border-color:red;">密码或验证码输入错误,3秒钟后跳转至login.jsp</div>
<script type="text/javascript">
window.navigator('./login.jsp');
</script>
<%
out.println( "<HTML><HEAD><META http-equiv='refresh' content='3; URL=/login/login.jsp' target=Main></HEAD><BODY bgcolor='#FFFFFF'></body></html>");
//out.println("<script language='javascript'>alert('账号或者密码输入不正确--您的操作有误!');window.location.href='/login/login.jsp';</script>");
%> --%>
</body>
</html>
verify.jsp
<%@ page language="java" import="java.util.*,com.JDBCUtil,java.sql.*" pageEncoding="utf-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'verify.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">
</head>
<body>
<%
String rand =(String)session.getAttribute("rand");
String input =request.getParameter("verify");
if(rand.equals(input))
{
String strUser=request.getParameter("user");
String strPass=request.getParameter("pass");
//打开连接
Connection conn = JDBCUtil.getConnection();
//设置sql语句和sql参数
String sql="select COUNT(*) from Admin where a_name= ? and a_pass= ?";
Object[] param =new Object[2];
param[0]=strUser;
param[1]=strPass;
//查询并返回结果集
ResultSet rs=JDBCUtil.doParamQuery(sql, param);
//将指针移动到第一行
rs.next();
//显示第一行第一列的值
System.out.println("数据库有"+rs.getInt(1)+"条记录与此相符合");
if(rs.getInt(1)==1){
System.out.println("登录成功");
//保存登录状态信息
session.setAttribute("login", "ok");
session.setAttribute("me", strUser);
response.sendRedirect("./login_ok.jsp");
}else{
System.out.println("登录失败");
response.sendRedirect("./login_error.jsp");
//直接在此页返回login.jsp,会使验证码不刷新,故使用跳转至错误处理页,再跳转至login.jsp
//out.println("<script>alert('登录失败')</script>");
//window.history.back(-1);验证码不刷新
//out.println("<script>window.history.back(-1);</script>");
//out.println("<script>window.location.href('./index.jsp')</script>");
}
}else{
out.println("<script>alert('验证码错误')</script>");
response.sendRedirect("./login_error.jsp");
//直接在此页返回login.jsp,会使验证码不刷新,故使用跳转至错误处理页,再跳转至login.jsp
//out.println("<script>alert('验证码错误')</script>");
//out.println("<script>window.history.back(-1);</script>");
//out.println("<script>window.location.href('./index.jsp')</script>");
}
/*if(strUser.equals("admin")&&strPass.equals("admin")&&rand.equals(input)){
out.println("<h3>登陆成功</h3>");
}else{
out.println("<h3>登陆失败</h3>");
}*/
/*if(strUser.equals("admin"))
{
if(strPass.equals("admin"))
{
if(rand.equals(input))
{
out.println("<script>alert('登录成功')</script>");
out.println("登录成功");
}
else{
out.println("<script>alert('验证码错误')</script>");
out.println("<script>window.location.href('./index.jsp')</script>");
}
}
else{
out.println("<script>alert('密码错误')</script>");
out.println("<script>window.location.href('./index.jsp')</script>");
}
}
else
{
out.println("<script>alert('用户名不存在')</script>");
out.println("<script>window.location.href('./index.jsp')</script>");
}
*/
%>
</body>
</html>
verifyCode.jsp
<!--设置页面类型和编码 -->
<%@ page contentType="image/jpeg" language="java" pageEncoding="utf-8"%>
<!--导入需要的包 -->
<%@ page import="java.util.*,java.awt.*,java.awt.image.*,javax.imageio.*"%>
<!--给定范围获得随机颜色 -->
<%!
Color getRandColor(int fc,int bc)
{
if(fc>255) fc=255;
if(bc>255) bc=255;
//随机生成rgb颜色
Random random = new Random();
int r=fc+random.nextInt(bc-fc);
int g=fc+random.nextInt(bc-fc);
int b=fc+random.nextInt(bc-fc);
return new Color(r,g,b);
}
%>
<%
//设置页面不缓存
response.setHeader("Pragma","No-cache");
response.setHeader("Cache-Control","no-cache");
response.setDateHeader("Expires", 0);
// 在内存中创建图象
int width=60, height=20;
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
// 获取图形上下文
Graphics g = image.getGraphics();
//生成随机类
Random random = new Random();
// 设定背景色
g.setColor(getRandColor(200,250));
g.fillRect(0, 0, width, height);
//设定字体
g.setFont(new Font("Times New Roman",Font.PLAIN,22));
//画边框
//g.setColor(new Color());
//g.drawRect(0,0,width-1,height-1);
// 随机产生155条干扰线,使图象中的认证码不易被其它程序探测到
g.setColor(getRandColor(160,200));
for (int i=0;i<155;i++)
{
int x = random.nextInt(width);
int y = random.nextInt(height);
int xl = random.nextInt(12);
int yl = random.nextInt(12);
g.drawLine(x,y,x+xl,y+yl);
}
// 取随机产生的认证码(4位数字)
String sRand="";
for (int i=0;i<4;i++){
String rand=String.valueOf(random.nextInt(10));
sRand+=rand;
// 将认证码显示到图象中
g.setColor(new Color(20+random.nextInt(110),40+random.nextInt(110),60+random.nextInt(110)));
//调用函数出来的颜色相同,可能是因为种子太接近,所以只能直接生成
g.drawString(rand,13*i+6,16);
}
// 将认证码存入SESSION
session.setAttribute("rand",sRand);
// 图象生效
g.dispose();
// 输出图象到页面
ImageIO.write(image, "JPEG", response.getOutputStream());
%>
还没有评论,来说两句吧...