练习一:session 实现登录注销

package com.dzq.session.logout;

import java.util.*;

public class UserDao {
/**
* 存储用户信息,代替数据库
*/
private UserDao(){ }
private static Map<String, String> map=new HashMap<String, String>();
static{
map.put("张三丰", "111");
map.put("Adele", "111");
map.put("小杜", "111");
}
public static boolean valiNP(String username,String password){
return map.containsKey(username)&&map.get(username).equals(password);
}
}

我。。。。。。。。。。。。。。。。。。是。。。。。。。。。。。。分。。。。。。。。。。割。。。。。。。。。。线。。。。。。。。。。。。。。


package com.dzq.session.logout;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; @WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
/**
* 实现登录的servlet,登陆后重定向到主页index。jsp
*/
private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
String username=request.getParameter("username");
String password=request.getParameter("password");
if(UserDao.valiNP(username, password)){
request.getSession().setAttribute("user", username);
response.sendRedirect(request.getContextPath()+"/logout/index.jsp");
}else{ response.getWriter().write("用户名或者密码错误");
} } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
} }

我。。。。。。。。。。。。。。。。是。。。。。。。。。。。。分。。。。。。。。。。割。。。。。。。。。。线。。。。。。。。。。。。


package com.dzq.session.logout;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; @WebServlet("/LogoutServlet")
public class LogoutServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* 注销登录的servlet ,销毁session ,重定向到index。jsp
*/ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
if(request.getSession(false)!=null&&request.getSession().getAttribute("user")!=null){
request.getSession().invalidate();
}
response.sendRedirect(request.getContextPath()+"/logout/index.jsp");
} protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
} }

  

我。。。。。。。。。。。。。。是。。。。。。。。。。。。分。。。。。。。。。。割。。。。。。。。。。线。。。。。。。。。。。

我               的               下                        面                        是                 jsp                         页                                     面


  

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<!-- 主页index.jsp -->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>我的网站</h1><hr>
<%
String user=(String)session.getAttribute("user"); %>
<%
if(user==null||"".equals(user)){
%>
欢迎光临,游客....
<a href="login.jsp">登录</a>
<a href="#">注册</a>
<%
}else{
%>
欢迎回来,<%=user %>
<a href="${pageContext.request.contextPath }/LogoutServlet">注销</a>
<%
}
%>
</body>
</html>

  

我。。。。。。。。。。。。。。是。。。。。。。。。。。。分。。。。。。。。。。割。。。。。。。。。。线。。。。。。。。。。。。

我                     的                 下                   面              还        是                 jsp                   页                             面


  

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<!-- 登录页面 -->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>我的网站</h1><hr>
<form action="${pageContext.request.contextPath }/LoginServlet" method="post">
用户名:<input type="text" name="username" />
密码:<input type="password" name="password"/>
<input type="submit" value="登录"/>
</form>
</body>
</html>

  练习二:session 实现防止表单重复提交:

package com.dzq.session.resubmit;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; @WebServlet("/ResubServlet")
public class ResubServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* 获取session 防止重复提交
*/ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
try {
Thread.sleep(4*1000);
} catch (Exception e) {
e.printStackTrace();
}
String username=request.getParameter("username");
String valinum=request.getParameter("valinum");
String valinum2=(String) request.getSession().getAttribute("valinum"); if(valinum2!=null&& !"".equals(valinum2)&&valinum.equals(valinum2)){
request.getSession().removeAttribute("valinum");
System.out.println("向数据库中注册一次"+username);
}else{
response.getWriter().write("web不要重复提交");
} } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
} }

  

我。。。。。。。。。。。。。。是。。。。。。。。分。。。。。。。。割。。。。。。。。。。线。。。。。。。。。。。。

我                的                 下                  面                          是                    jsp                    页                面


  

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<!-- <script type="text/javascript">
var isNotSub=true;
function canSub() {
if(isNotSub){
isNotSub=false;
return true;
}else{
alert("请不要重复提交");
return false;
}
}
</script> -->
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
Random r=new Random();
int valinum=r.nextInt();
session.setAttribute("valinum", valinum+"");
%>
<form action="${pageContext.request.contextPath }/ResubServlet" method="post" onsubmit="return canSub()">
<input type="text" name="username"/>
<input type="hidden" name="valinum" value="<%=valinum%>"/>
<input type="submit" value="提交"/>
</form>
</body>
</html>

  

20160330javaweb之session 小练习的更多相关文章

  1. Session小案例------完成用户登录

    Session小案例------完成用户登录     在项目开发中,用户登陆功能再平常只是啦,当用户完毕username和password校验后.进入主界面,须要在主界面中显示用户的信息,此时用ses ...

  2. Session小案例-----简单购物车的使用

    Session小案例-----简单购物车的使用 同上篇一样,这里的处理请求和页面显示相同用的都是servlet. 功能实现例如以下: 1,显示站点的全部商品 2.用户点击购买后,可以记住用户选择的商品 ...

  3. ASP入门(十一)-Session小案例

    一般来说,在实际开发中,对于 Session 对象使用最多的就是用户登录部分了,这个案例将简单模拟一个用户登录表单.用户是否登录的判断以及用户退出的一系列功能,它一共分了以下几个页面. Login.a ...

  4. servlet(5) - Cookie和session - 小易Java笔记

    1.会话概述 (1)会话可简单理解为:用户开一个浏览器,点击多个超链接,访问服务器多个web资源,然后关闭浏览器,整个过程称之为一个会话. (2)会话过程中的数据不宜保存在request和servle ...

  5. ASP.NET保存信息总结(Application、Session、Cookie、ViewState和Cache等) ZT

    http://www.cnblogs.com/ranran/p/4065619.html http://www.cnblogs.com/jxlsomnus/p/4450911.html 以下是关于AS ...

  6. laravel框架session使用教程

    laravel是一款php框架了,在使用laravel时会碰到session使用问题了,在使用过程中碰到一些问题与一些应用的例子. 用Laravel开发应用,把原有的代码copy过来,以前的代码ses ...

  7. Asp.net 服务器Application,Session,Cookie,ViewState和Cache区别

    2.8 Context 的使用Context 对象包含与当前页面相关的信息,提供对整个上下文的访问,包括请求.响应.以及上文中的Session 和Application 等信息.可以使用此对象在网页之 ...

  8. [ASP.net教程]ASP.NET保存信息总结(Application、Session、Cookie、ViewState和Cache等)

    以下是关于ASP.NET中保存各种信息的对象的比较,理解这些对象的原理,对制作完善的程序来说是相当有必要的(摘至互联网,并非原创--xukunping)在ASP.NET中,有很多种保存信息的对象.例如 ...

  9. Application,Session,Cookie,ViewState和Cache区别

    在ASP.NET中,有很多种保存信息的内置对象,如:Application,Session,Cookie,ViewState和Cache等.下面分别介绍它们的用法和区别. 方法 信息量大小 作用域和保 ...

随机推荐

  1. 期待已久的2012年度最佳jQuery插件揭晓

    近日,国外著名博客WDL发布了2012年度最佳 jQuery 插件.jQuery 自2006年发布以来,经过6年的迅速发展,目前已是最流行和使用最广泛的 JavaScript 框架,这主要归功于众多围 ...

  2. 教您Photoshop中如何快速放大、缩小、移动图像

    教您Photoshop中如何快速放大.缩小.移动图像 http://jingyan.baidu.com/article/ae97a646aaeaaebbfc461d5e.html

  3. [转]ASP.NET MVC 入门10、Action Filter 与 内置的Filter实现(实例-防盗链)

    前一篇中我们已经了解了Action Filter 与 内置的Filter实现,现在我们就来写一个实例.就写一个防盗链的Filter吧. 首先继承自FilterAttribute类同时实现IAction ...

  4. 前景还是“钱景”——MM应用引擎的自我博弈

    纵观当前的移动互联网发展态势,巨大的商机已经展露无遗,各个领域的企业及个人对于APP的开发如火如荼,许多APP从诞生伊始,就面临着软件开发的专业性,商业模式的模糊性,以及市场推广的艰巨性三个巨大难题, ...

  5. [二]JFreeChart实践一

    生成一张简单的图片 java代码: package com.lxl.chart; import javax.servlet.http.HttpSession; import org.jfree.cha ...

  6. php数组相加 两个数组键名相同 后者不能覆盖前者

    array(“a”)+array(“b”)结果还是array(“a”) array(“a”)+array(“b”)的结果是 array(“a”) 因为,它们等效于array(“0″=>”a”)+ ...

  7. 递归小demo(1-100的和)

    public class demo1 { public static void main(String[] args) { //初始值为100 int n = 100; //调用number方法,返回 ...

  8. 远程使用Gpupdate(Hash,哈希)

    function Start-GPUpdate {     param     (         [String[]]         $ComputerName     )       $code ...

  9. scala编程笔记(三)类,字段和方法

    类,字段和方法 类是对象的蓝图,能够通过new来创建对象.在类的定义里能够有字段和方法.统称member val还是var都是指向对象的变量(var定义的字段可又一次赋值),def定义方法,包括可运行 ...

  10. 路冉的JavaScript学习笔记-2015年1月23日

    1.JavaScript的数据类型 A.原始类型:包含数值.字符串.布尔值.空值(null)和未定义值(undefined). Js原始类型均为不可改变类型.对不可变类型调用任何自带方法都不会改变原始 ...