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

运行界面:

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

session应用----登录验证小案例的更多相关文章

  1. Express + Session 实现登录验证

    1. 写在前面 当我们登录了一个网站,在没有退出登录的情况下,我们关闭了这个网站 ,过一段时间,再次打开这个网站,依然还会是登录状态.这是因为,当我们登录了一个网站,服务器会保存我们的登录状态,直到我 ...

  2. cookie和session实现登录验证

    回话技术,比如在做登录功能的时候,需要配合是用存储在客户端的cookie信息,以及存储在服务端的session来实现登录功能.在cookie中保存了用户的信息,特别是一个特殊的令牌信息,当用户拿着这个 ...

  3. session的两个小案例

    学完了session,写两个小案例加深一下对session的巩固. 1. 用户登陆案例 登陆html页面提交后,将参数带给处理登陆的servlet,该servlet将获得登陆的用户名和密码,并将这些信 ...

  4. 基于form表单的极验滑动验证小案例

    01.目录展示 02.url.py urlpatterns = [ path('admin/', admin.site.urls), path('login/',views.login), path( ...

  5. 监听器应用【统计网站人数、自定义session扫描器、踢人小案例】

    从第一篇已经讲解过了监听器的基本概念,以及Servlet各种的监听器.这篇博文主要讲解的是监听器的应用. 统计网站在线人数 分析 我们在网站中一般使用Session来标识某用户是否登陆了,如果登陆了, ...

  6. 基于cookie和session的登录验证

    settings.py MIDDLEWARE = [ 'django.middleware.security.SecurityMiddleware', 'django.contrib.sessions ...

  7. Django基础之Session版登录验证

    from functools import wraps def check_login(func): @wraps(func) def inner(request, *args, **kwargs): ...

  8. python学习之最简单的用户注册及登录验证小程序

    文章都是从我的个人博客上粘贴过来的哦,更多内容请点击 http://www.iwangzheng.com 正如很多同学所知道的,楼主开始学习python了,前进的道路曲曲折折,有荆棘也有陷阱,从最简单 ...

  9. python3 登录验证小程序,同一用户输错三次密码,锁定账户

    ''' 让用户输入用户名密码 认证成功后显示欢迎信息用户3次认证失败后,退出程序,再次启动程序尝试登录时,还是锁定状态''' # !/usr/bin/env python # -*- coding:u ...

随机推荐

  1. PHP json_encode / json_decode

    2015年3月26日 14:14:16 PHP的json函数对几个特殊值的处理笔记 <?php //----------编码 $a = array(); $b = json_encode($a) ...

  2. FastReport调用Delphi中的自定义函数(人民币大写金额)mtm

    1. 在 FormCreate 中向FastReprot添加函数 (fPrint)窗口 procedure TfPrint.FormCreate(Sender: TObject); frxReport ...

  3. Delphi XE5 android 获取网络状态

    unit Androidapi.JNI.Network; interface function IsConnected: Boolean; function IsWiFiConnected: Bool ...

  4. 初识 MySQL 5.6 新特性、功能

    背景: 之前介绍过 MySQL 5.5 新功能.参数,现在要用MySQL5.6,所以就学习和了解下MySQL5.6新的特性和功能,尽量避免踩坑.在后续的学习过程中文章也会不定时更新. 一:参数默认值的 ...

  5. SaltStack之Master配置文件详解

    salt-master的配置文件位于/etc/salt/master,可用选项如下: #######################主配置 interface默认值:0.0.0.0(所有的网络地址接口 ...

  6. Effective C++ -----条款29:为“异常安全”而努力是值得的

    异常安全函数(Exception-safe functions)即使发生异常也不会泄露资源或允许任何数据结构败坏.这样的函数区分为三种可能的保证:基本型.强烈型.不抛异常型. “强烈保证”往往能够以c ...

  7. ZipArchive和SSZipArchive使用详解

    一.SSZipArchive 1.简介 SSZipArchive是iOS和Mac上一个简单实用的压缩和解压插件.用途包括:1.解压zip文件:2.解压密码保护的ZIP文件:3.创建新的zip文件:4. ...

  8. 【python】sqlite使用

    官方文档:https://docs.python.org/2/library/sqlite3.html sqlite教程:http://www.runoob.com/sqlite/sqlite-del ...

  9. 详解web.xml中元素的加载顺序

    一.背景 最近在项目中遇到了启动时出现加载service注解注入失败的问题,后来经过不懈努力发现了是因为web.xml配置文件中的元素加载顺序导致的,那么就抽空研究了以下tomcat在启动时web.x ...

  10. 在R语言中无法设置CRAN镜像问题

    很大的可能是因为使用的浏览器不是IE浏览器的问题,因为CRAN的镜像需要用IE浏览器来打开. 只需要按照下面设置即可: 1.打开IE-->设置-->Internet选项-->高级 2 ...