练习一: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. HtmlParser应用,使用Filter从爬取到的网页中获取需要的内容

    htmlparser是一个纯的java写的html解析的库,它不依赖于其它的java库文件,主要用于改造或提取html.它能超高速解析html,而且不会出错.现在htmlparser最新版本为2.0. ...

  2. 【HtmlParser】HtmlParser使用

    转载 http://www.cnblogs.com/549294286/archive/2012/09/04/2670601.html HTMLParser的核心模块是org.htmlparser.P ...

  3. Prism - WPF MVVM(Model-View-ViewModel)设计模式【学习】

    开发工具: VS2010 Blend Prism框架 基本概念: 数据绑定,依赖属性,依赖对象 WPF 委托式命令 Icommand接口 Lambda表达式 MVVM(Model-View-ViewM ...

  4. HTTP协议分析及攻防方法

    HTTP协议简介 应用层协议,无状态协议(可设定为维持TCP连接,但服务器端的资源会释放).默认HTTP的端口号为80,HTTPS的端口号为443. 基于HTTP协议的客户机访问包括4个过程,分别是建 ...

  5. PLS-00306:错误解决思路 - OracleHelper 执行Oracle函数的坑

    如果你是像我一样初次使用Net+Oracle的结合,我想你会跟我一样,有很大的概率碰到这个问题 ==================================================== ...

  6. debian下安装AMD驱动

    参考:http://blog.sciencenet.cn/blog-296919-464464.html 去AMD官网下载对应的驱动: amd-driver-installer-catalyst-13 ...

  7. Mathtype(对齐设置)

    选左对齐 可以编辑->插入符号->空格符

  8. oracle to_date函数(转载)

    TO_DATE格式(以时间:2007-11-02   13:45:25为例)           Year:              yy two digits 两位年                ...

  9. PHP字符串替换函数strtr()

    strtr函数比str_replace函数的效率要高很多,strtr()的两种定义方式: strtr(string, from, to)和strtr(string, array)1.strtr区分大小 ...

  10. 小波变换和motion信号处理(三)(转)

    这篇文章算太监了,去作者blog提问去吧:http://www.kunli.info/2012/04/08/fourier-wavelet-motion-signal-3/ 从前两篇发布到现在,过去一 ...