session应用----登录验证小案例

时间:2024-06-29 08:36:50
 <%@ 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">
</head>
<script type="text/javascript">
function func() {
document.getElementById("ima1").src="VerifyCodeServlet?a="+new Date().getTime();
}
</script>
<body>
<%
String msg="";
String s=(String)request.getAttribute("msg");
if(s!=null) msg=s;
%>
<h1>登录</h1>
<font color="red"><%=msg %></font>
<form action="AServlet" method="post">
用户名:<input type="text" name="uname"><br>
验证码:<input type="text" name="verifyCode" size="4">
<img id="ima1" src="VerifyCodeServlet"/>
<a href="javascript:func();">看不清,换一张</a><br>
<input type="submit" value="提交">
</form>
</body>
</html>

index.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">
</head>
<%
String uname=(String)request.getSession().getAttribute("uname");
if(uname==null)
{
request.setAttribute("msg", "请先登录!");
response.sendRedirect("index.jsp");
}
%>
<body>
欢迎<%=uname %>
</body>
</html>

suc.jsp

 package cn.yzu;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; public class VerifyCodeServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
VerifyCode verifyCode = new VerifyCode();
BufferedImage bi = verifyCode.getImage();
request.getSession().setAttribute("vc", verifyCode.getText());
verifyCode.output(bi,response.getOutputStream());
} }

VerifyCodeServlet

 package cn.yzu;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class AServlet extends HttpServlet {
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String uname=request.getParameter("uname");
String verifyCode=request.getParameter("verifyCode");
if(uname.equals("fmy")&&verifyCode.equalsIgnoreCase((String) request.getSession().getAttribute("vc")))
{
request.getSession().setAttribute("uname", uname);
response.sendRedirect("suc.jsp");
}
else
{
request.setAttribute("msg", "用户名或者验证码错误!");
request.getRequestDispatcher("index.jsp").forward(request, response);
}
}
}

AServlet

运行界面:

session应用----登录验证小案例

注:verifyCode类见博客:http://www.cnblogs.com/fengmingyue/p/5987814.html