中午吃完饭,将上午完成的用jsp操作cookies的小test粘贴到这里。虽然是很陈旧的知识了,但是毕竟是一次学习。放到这里,希望给予需要的人以帮助。
基本目的为:选择‘保存’并填写input内容后提交,能够在cookie中保存信息,再次打开index页面时,可以达到不用再次输入input的目的。
index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ page import="javax.servlet.http.Cookie" %>
<%@ page language="java" import="java.net.*" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>cookie test</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<script type="text/javascript" src="<%=basePath%>/js/jquery-1.3.2.min.js"> </script>
<script type="text/javascript" src="<%=basePath%>/js/jquery-pack.js"></script>
<% //遍历所有cookies,判断所用到的cookies的取值 。
Cookie[] myCookie = request.getCookies();
int checked = 0;
String userName = "";
if(myCookie!=null && myCookie.length!=0){
for(int n=0; n<=(myCookie.length-1); n++){
Cookie newCookie = myCookie[n];
if(newCookie.getName().equals("save")){
if(null != newCookie.getValue()){
checked = 1;
//解码。
userName = URLDecoder.decode(newCookie.getValue(),"utf-8");
}else{
checked = 0;
}
}
}
}
%>
<script type="text/javascript">
$(function(){
//依据checked,判断是否选否选择保存cookie
if(<%=checked%>==1){
$('#saveCookie').attr('checked', 'checked');
$('#username').val("<%=userName%>");
}else{
$('#saveCookie').removeAttr('checked');
}
});
</script>
</head>
<body>
<br/>
<br/>
<div align="center">
<div>
<form id="form" action="doCookie.jsp" method="post">
<lable>用户名</lable>
<input id="username" name="username" type="text"></input>
<label>保存cookie</label>
<input id="saveCookie" name="saveCookie" type="checkbox"/>
<input id="button" type="submit" value="提交"/>
</form>
</div>
<br/>
</div>
</body>
</html>
doCookie.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ page import="javax.servlet.http.Cookie" %>
<%@ page language="java" import="java.net.*" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'saveCookie.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<%//处理cookies。
String saveCookie = request.getParameter("saveCookie");
//已经是中文了
String userName = new String(request.getParameter("username").getBytes("ISO8859-1"),"utf-8"); Cookie cookie = null;
if(saveCookie != null){
//为了让cookie中能够添加中文,在进行一次包装。
cookie = new Cookie("save",URLEncoder.encode(userName,"utf-8")); cookie.setMaxAge(365*24*60);
}else{
cookie =new Cookie("save",null);
cookie.setMaxAge(0);
}
cookie.setPath("/");
response.addCookie(cookie);
%>
</head>
<body>
<!-- 自动跳转 -->
<script type="text/javascript">
window.location = "index.jsp";
</script>
</body>
</html>
分享到:
相关推荐
可扩展标记语言(eXtensible Markup Language,XML)是目前应用开发领域中的热门技术。 本章主要讲述XML基本概念, XML的三种显示样式:CSS(Cascading Style Sheet,层叠式样式表单)、XSL...如何利用JSP操作XML文件。
cookies的编写,可以实现它和jsp的连接,使用户管理自己的cookies
jsp操作mysql数据库 jsp操作mysql数据库 jsp操作mysql数据库 jsp操作mysql数据库 jsp操作mysql数据库
利用Javabean+JSP 实现文件的上传、显示、下载 实现文件上传功能和下载功能,显示文件列表和读取文件内容功能。
jsp操作mysql表实现增删改查,简单注册登录。 驱动包和jsp都在里面,导入即可运行!
作业6 JDBC操作数据库 作业类型:验证性实验 实验目的:理解JDBC接口操作数据库的方法,熟练掌握数据的增删改查。 实验内容: 1. 在MySQL或其他DBS中创建一个数据库TestDB,其中创建表user,表至少3个字段(编号,...
JSP期末大作业:利用JSP动态网页技术与MySQL数据库技术,制作一个调查问卷动态网页,分别实现调查问卷信息的注册、登陆、增、删、查、改、注销等操作
武夷学院-JSP实验七:JSP数据库操作 (2).docx武夷学院-JSP实验七:JSP数据库操作 (2).docx武夷学院-JSP实验七:JSP数据库操作 (2).docx武夷学院-JSP实验七:JSP数据库操作 (2).docx武夷学院-JSP实验七:JSP数据库...
jsp操作cookie实例,jsp操作cookie实例,jsp操作cookie实例,jsp操作cookie实例
利用 jsp 、 javabean 、 servlet 、session等知识点,结合相关设计模式、 以及软件工程的相关知识, 设计一个网站购物车, 用于记录客户的购物订单, 并能对购物车中商品信息进行查询、修改、删除、清空等操作,...
利用jsp生成图片验证码,没有用到servlet
利用jsp实现文件上传下载,将文件上传到tomcat服务器之后下载。
JSP中的文件操作,也是jsp的的基础知识
jsp的页面传质以及操作数据库,对数据库增删查改。十分全面
jsp对数据库操作,代码非常的简单。
利用jsp的综合技术而开发的jsp游戏,而且有声音,全部动态生成,学完此技术,精悍的技术 利用jsp的综合技术而开发的jsp游戏,而且有声音,全部动态生成,学完此技术,精悍的技术
利用jspsmart下载,源代码仅供参考
利用jspsmart上传,源代码仅供参考
JSP文件操作与应用
利用JSP编写的BBS论坛,简易实用,包括删除帖子,回复帖子等功能