<%@ 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. oracle简历自增序列(转)

    步骤:1.创建序列   2.创建触发器. 语法解析:create sequence TB_CODE_SEQUENCEminvalue 1maxvalue 99999999999999999999999 ...

  2. IIS7.0配置网站时,提示“ISAPI 和 CGI 限制”

    把网站配置到IIS上的时候,访问网站提示如下错误:  

  3. Office文件的Open Xml 格式

    获取Office文件的Open Xml格式文件的步骤如下: 1.修改想要得到OpenXml格式文件的文件后缀名为.rar. 2.解压即可得到. 此时可以对xml文件进行操作,要想再次把这已经解压后的文 ...

  4. ios 上下拉刷新

    UITableView:下拉刷新和上拉加载更多 - cDigger 时间 2013-11-24 02:00:00  博客园精华区 原文  http://www.cnblogs.com/lexingyu ...

  5. 在Linux中安装JDK的步骤

    相信不少学习Java的朋友都在Windows操作系统中安装过JDK,这里就不对JDK做详细的介绍了. 在Windows下安装JDK可参考:JDK的安装和配置 1.下载JDK 我们可以去官网(http: ...

  6. android的JNI 、 NDK 学习!

    转载的! Java Native Interface (JNI)标准是java平台的一部分,它允许Java代码和其他语言写的代码进行交互.JNI 是本地编程接口,它使得在 Java 虚拟机 (VM) ...

  7. 谈谈Delph中的类和对象2---类可以理解成一种特殊的数据结构、类型转换

    三.类可以理解成一种特殊的数据结构 我们知道数据类型可以进行强制类型转换,类既然可以理解成一种数据类型,那么它也应该可以进行类型转换.比如下面代码为一个按钮(Button1)的单击事件 procedu ...

  8. Pyqt 时时CPU使用情况

    借鉴代码来自:https://github.com/hgoldfish/quickpanel 实现代码: # -*- coding:utf-8 -*- from __future__ import p ...

  9. 【翻译二十二】java-并发之集合与原子变量

    Concurrent Collections The java.util.concurrent package includes a number of additions to the Java C ...

  10. 那些年,我们在Django web开发中踩过的坑(一)——神奇的‘/’与ajax+iframe上传

    一.上传图片并在前端展示 为了避免前端整体刷新,我们采用ajax+iframe(兼容所有浏览器)上传,这样用户上传之后就可以立即看到图片: 上传前: 上传后: 前端部分html: <form s ...