20160330javaweb之session 小练习

练习一: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>

  

时间: 2024-08-27 05:25:40

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

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

Session小案例-----简单购物车的使用 同上篇一样,这里的处理请求和页面显示同样用的都是servlet. 功能实现如下: 1,显示网站的所有商品 2,用户点击购买后,能够记住用户选择的商品 3,实现了多个会话共享一个session 4, 实现了浏览器禁用cookie后数据共享问题的处理 首页: package cn.itcast.shopping; import java.io.IOException; import java.io.PrintWriter; import java.io

Session小案例------完成用户登陆

Session小案例------完成用户登陆 在项目开发中,用户登陆功能再寻常不过啦,当用户完成用户名和密码校验后,进入主界面,需要在主界面中显示用户的信息,此时用session来记住用户是最为合适不过了. 功能实现如下: 1,完成用户登陆功能 2,在主界面记住用户 3,完成用户注销功能 用户类: package cn.itcast.login; public class User{ private String username; private String password; public

Application,Session,Cookie,ViewState和Cache区别

在ASP.NET中,有很多种保存信息的内置对象,如:Application,Session,Cookie,ViewState和Cache等.下面分别介绍它们的用法和区别. 方法 信息量大小 作用域和保存时间 应用范围 保存位置 Application 任意大小 整个应用程序的生命期 整个应用程序/所有用户 服务器端 Cache 任意大小 可以根据需要设定 整个应用程序/所有用户 服务器端 Session 小量,简单的数据 用户活动时间+一段延迟时间(一般为20分钟) 单个用户 服务器端 Cook

ASP.NET:Application,Session,Cookie,ViewState和Cache之间的区别(转)

在ASP.NET中,有很多种保存信息的对象.例如:Application,Session,Cookie,ViewState和Cache等,那么它们有什么区别呢?每一种对象应用的环境是什么? 为了更清楚的了解,我们总结出每一种对象应用的具体环境,如下表所示: 方法 信息量大小 保存时间 应用范围 保存位置 Application 任意大小 整个应用程序的生命期 所有用户 服务器端 Session 小量.简单的数据 用户活动时间+一段延迟时间(一般为20分钟) 单个用户 服务器端 Cookie 小量

laravel session的使用

用Laravel开发应用,把原有的代码copy过来,以前的代码session使用了$_SESSION,本以为移植过来可以很好的运行的,因为没有依赖其他的组件,结果出现了这个 Undefined variable: _SESSION Laravel的session的配置文件配置在 app/config/session.php 中,使用时可以看看 session 配置文件中可用的选项设定及注释. Laravel 默认使用 file 的方式来实现 session的.她并不用php原生的$_SESSIO

APPlication,Session和Cookie的区别

方法 信息量大小 保存时间 应用范围 保存位置 Application 任意大小 整个应用程序的生命期 所有用户 服务器端 Session 小量,简单的数据 用户活动时间+一段延迟时间(一般为20分钟) 单个用户 服务器端 Cookie 小量,简单的数据 可以根据需要设定 单个用户 客户端 1.Application对象     Application用于保存所有用户的公共的数据信息,如果使用Application对象,一个需要考虑的问题是任何写操作都要在Application_OnStart事

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

以下是关于ASP.NET中保存各种信息的对象的比较,理解这些对象的原理,对制作完善的程序来说是相当有必要的(摘至互联网,并非原创--xukunping)在ASP.NET中,有很多种保存信息的对象.例如:APPlication,Session,Cookie,ViewState和Cache等,那么它们有什么区别呢?每一种对象应用的环境是什么?    为了更清楚的了解,我们总结出每一种对象应用的具体环境,如下表所示:方法 信息量大小 保存时间 应用范围 保存位置Application 任意大小 整个应

cookie 和session 的区别(转)

二者的定义: 当你在浏览网站的时候,WEB 服务器会先送一小小资料放在你的计算机上,Cookie 会帮你在网站上所打的文字或是一些选择, 都纪录下来.当下次你再光临同一个网站,WEB 服务器会先看看有没有它上次留下的 Cookie 资料,有的话,就会依据 Cookie 里的内容来判断使用者,送出特定的网页内容给你. Cookie 的使用很普遍,许多有提供个人化服务的网站,都是利用 Cookie 来辨认使用者,以方便送出使用者量身定做的内容,像是 Web 接口的免费 email 网站,都要用到 C

ASP.NET Application,Session,Cookie和ViewState等对象用法和区别 (转)

在ASP.NET中,有很多种保存信息的内置对象,如:Application,Session,Cookie,ViewState和Cache等.下面分别介绍它们的用法和区别. 方法 信息量大小 作用域和保存时间 应用范围 保存位置 Application 任意大小 整个应用程序的生命期 整个应用程序/所有用户 服务器端 Cache 任意大小 可以根据需要设定 整个应用程序/所有用户 服务器端 Session 小量,简单的数据 用户活动时间+一段延迟时间(一般为20分钟) 单个用户 服务器端 Cook