练习一: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. C# 操作 Word 修改word的高级属性中的自定义属性2

    word的类库使用的是word2007版本的类库,类库信息见下面图片,折腾了半天,终于找到入口,网上 很多说的添加或者修改word的高级属性中的自定义属性都是错误的,感觉都是在copy网上的代码,自己 ...

  2. iphone4s丢失如何找回

    iphone4s丢失如何找回 iphone4s手机丢了怎么办,其实苹果手机自带找回功能,但是前提你得打开了icloud这款软件. 方法/步骤 1 在手机的设置里找到icloud设置,如图. 2 点击进 ...

  3. HDU 2682

    思路:由于题目对能相连的点有限制,必须将这些点处理,能相连的点合并到一个集合中,最后查看是否所有点都在一个集合里,若都在说明是一个连通图,存在最小生成树,否则图不连通,不存在最小花费. #includ ...

  4. [Irving]DateTime格式处理大全

    DateTime dt = DateTime.Now;//    Label1.Text = dt.ToString();//2005-11-5 13:21:25//    Label2.Text = ...

  5. nyoj 找球号三(除了一个数个数为基数,其他为偶数,编程之美上的)

    #include<iostream> #include<stdio.h> using namespace std; int main() { int len; while(ci ...

  6. Storm系列(十七)DRPC介绍

    Storm版本0.9.5 在storm中DRPC服务应用于远程分布式计算,根据客户端提交的请求参数,而返回Storm计算的结果. DRPC服务启动流程(远程模式) 启动DRPC服务,启动命令:stor ...

  7. 新浪微博、腾讯微博、QQ空间、人人网、豆瓣 一键分享API

    新浪微博: http://service.weibo.com/share/share.php?url= count=表示是否显示当前页面被分享数量(1显示)(可选,允许为空) & url=将页 ...

  8. (Step by Step)How to setup IP Phone Server(VoIP Server) for free.

    You must have heard about IP Phone and SIP (Software IP Phone).Nowadays standard PSTN phone are bein ...

  9. epoll原理解释(转)

    转自:http://yaocoder.blog.51cto.com/2668309/888374   首先我们来定义流的概念,一个流可以是文件,socket,pipe等等可以进行I/O操作的内核对象. ...

  10. 问题-Delphi为什么不能连接oracle

    问题现象:delphi 为什么不能连接oracle 问题处理:加一句OraSession1.Options.Net := True;