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>
20160330javaweb之session 小练习的更多相关文章
- Session小案例------完成用户登录
Session小案例------完成用户登录 在项目开发中,用户登陆功能再平常只是啦,当用户完毕username和password校验后.进入主界面,须要在主界面中显示用户的信息,此时用ses ...
- Session小案例-----简单购物车的使用
Session小案例-----简单购物车的使用 同上篇一样,这里的处理请求和页面显示相同用的都是servlet. 功能实现例如以下: 1,显示站点的全部商品 2.用户点击购买后,可以记住用户选择的商品 ...
- ASP入门(十一)-Session小案例
一般来说,在实际开发中,对于 Session 对象使用最多的就是用户登录部分了,这个案例将简单模拟一个用户登录表单.用户是否登录的判断以及用户退出的一系列功能,它一共分了以下几个页面. Login.a ...
- servlet(5) - Cookie和session - 小易Java笔记
1.会话概述 (1)会话可简单理解为:用户开一个浏览器,点击多个超链接,访问服务器多个web资源,然后关闭浏览器,整个过程称之为一个会话. (2)会话过程中的数据不宜保存在request和servle ...
- 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 ...
- laravel框架session使用教程
laravel是一款php框架了,在使用laravel时会碰到session使用问题了,在使用过程中碰到一些问题与一些应用的例子. 用Laravel开发应用,把原有的代码copy过来,以前的代码ses ...
- Asp.net 服务器Application,Session,Cookie,ViewState和Cache区别
2.8 Context 的使用Context 对象包含与当前页面相关的信息,提供对整个上下文的访问,包括请求.响应.以及上文中的Session 和Application 等信息.可以使用此对象在网页之 ...
- [ASP.net教程]ASP.NET保存信息总结(Application、Session、Cookie、ViewState和Cache等)
以下是关于ASP.NET中保存各种信息的对象的比较,理解这些对象的原理,对制作完善的程序来说是相当有必要的(摘至互联网,并非原创--xukunping)在ASP.NET中,有很多种保存信息的对象.例如 ...
- Application,Session,Cookie,ViewState和Cache区别
在ASP.NET中,有很多种保存信息的内置对象,如:Application,Session,Cookie,ViewState和Cache等.下面分别介绍它们的用法和区别. 方法 信息量大小 作用域和保 ...
随机推荐
- 谈谈分布式事务之三: System.Transactions事务详解[上篇]
在.NET 1.x中,我们基本是通过ADO.NET实现对不同数据库访问的事务..NET 2.0为了带来了全新的事务编程模式,由于所有事务组件或者类型均定义在System.Transactions程序集 ...
- bzoj 3172 [Tjoi2013]单词(fail树,DP)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=3172 [题意] 题目的意思是这样的,给若干个单词,求每个单词在这一堆单词中的出现次数. ...
- SharePoint 2010 修改默认列表样式
SharePoint 2010 修改默认列表样式 :可以通过修改 下面两个全局配置进行修改.(未完..更新中...) C:\Program Files\Common Files\Microsoft ...
- Codeforces 295C Greg and Friends
BFS+DP.dp[i][j][0]表示有i个50kg,j个100kg的人在左岸,dp[i][j][1]表示有i个50kg,j个100kg的人在右岸.用BFS求最短路的时候记录到达该状态的可能情况. ...
- 授权给adfs读取ad 在ad服务器上运行 - setspn 命令 -摘自网络
Because the application pool identity for the AD FS 2.0 AppPool is running as a domain user/service ...
- Win+R指令(2)
1. gpedit.msc-----组策略 2. sndrec32-------录音机 3. Nslookup-------IP地址侦测器 ,是一个 监测网络中 DNS 服务器是否能正确实现域名解析的 ...
- HDU P4578 Transformation
Problem Description Yuanfang is puzzled with the question below: There are n integers, a1, a2, …, an ...
- RSA加密解密操作
国内私募机构九鼎控股打造APP,来就送 20元现金领取地址:http://jdb.jiudingcapital.com/phone.html内部邀请码:C8E245J (不写邀请码,没有现金送)国内私 ...
- UVA Knight Moves
题目例如以下: Knight Moves A friend of you is doing research on the Traveling Knight Problem (TKP) where ...
- JSON未定义解决办法
json官网:www.json.org json2.js地址:https://github.com/douglascrockford/JSON-js/blob/master/json2.js 引入解决 ...