您的位置:首页 > Web前端 > JavaScript

jsp4 Cookie

2016-05-04 17:37 477 查看

(1)

(2)Session----Coolie(3)



(4)




cookie保存index.jsp
<%@pagelanguage="java"import="java.util.*,java.net.*"contentType="text/html;charset=UTF-8"%>

<%

Stringpath=request.getContextPath();

StringbasePath=request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%>


<!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.01Transitional//EN">

<html>

<head>

<basehref="<%=basePath%>">


<title>MyJSP'index.jsp'startingpage</title>

<metahttp-equiv="pragma"content="no-cache">

<metahttp-equiv="cache-control"content="no-cache">

	<metahttp-equiv="expires"content="0">

<metahttp-equiv="keywords"content="keyword1,keyword2,keyword3">

<metahttp-equiv="description"content="Thisismypage">


</head>


<body>


<h1>用户登录</h1>

<hr>

<%

	request.setCharacterEncoding("utf-8");

Stringusername="";

Stringpassword="";

Cookie[]cookies=request.getCookies();

if(cookies!=null&&cookies.length>0){

	for(Cookiec:cookies){

if(c.getName().equals("username")){//(名称)

username=URLDecoder.decode(c.getValue(),"utf-8");

}

if(c.getName().equals("password")){

password=URLDecoder.decode(c.getValue(),"utf-8");

}

}

}

%>

	<formaction="dologin.jsp"method="post"name="loginForm">

<table>

	<tr><td>用户名:</td><td><inputtype="text"name="username"value="<%=username%>"/></td></tr>

	<tr><td>密码:</td><td><input	type="password"name="password"value="<%=password%>"/></td></tr>

	<tr><tdcolspan="2"><inputtype="checkbox"name="isUseCookie"checked="checkd"/>十天内记住登陆状态</td></tr>

	<tr><tdcolspan="2"aligin="center"><inputtype="submit"value="登录"></td></tr>

</table>


</form>


</body>

</html>

[/code]dologin.jsp
<%@pagelanguage="java"import="java.util.*,java.net.*"contentType="text/html;charset=UTF-8"%>

<%

Stringpath=request.getContextPath();

StringbasePath=request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%>


<!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.01Transitional//EN">

<html>

<head>

<basehref="<%=basePath%>">


<title>MyJSP'dologin.jsp'startingpage</title>


<metahttp-equiv="pragma"content="no-cache">

<metahttp-equiv="cache-control"content="no-cache">

	<metahttp-equiv="expires"content="0">

<metahttp-equiv="keywords"content="keyword1,keyword2,keyword3">

<metahttp-equiv="description"content="Thisismypage">

</head>


<body>

<h1>登陆成功</h1><br><br><br><br>

<%

request.setCharacterEncoding("utf-8");//防止中文乱码

//首先判断用户是否选择了记录登陆状态

String[]isUseCookie=request.getParameterValues("isUseCookie");//返回包含参数name的所有值的数组

if(isUseCookie!=null&&isUseCookie.length>0){

	//吧用户名和密码保存在Cookie对象里面

	Stringusername=URLEncoder.encode(request.getParameter("username"),"utf-8");//返回name指定函数的参数值

	//URLEncoder.encode(s,enc)防止中文乱码。。短码

	Stringpassword=URLEncoder.encode(request.getParameter("password"),"utf-8");

	

	CookieusernameCookie=newCookie("username",username);//创建cookie对象

	CookiepasswordCookie=newCookie("password",password);

	usernameCookie.setMaxAge(8640000);//设置最大生存期限为10天(有效期)

	passwordCookie.setMaxAge(8640000);

	response.addCookie(usernameCookie);//加入cookie对象

	response.addCookie(passwordCookie);

}else{

	Cookie[]cookies=request.getCookies();//读取cookie对象

	if(cookies!=null&&cookies.length>0){

for(Cookiec:cookies){

	if(c.getName().equals("username")||c.getName().equals("password")){//(名称)

c.setMaxAge(0);//设置cookie失效

	response.addCookie(c);//重新保存

}

}

	}

}

%>

<ahref="users.jsp"target="_blank">查看用户信息</a>

</body>

</html>

[/code]users.jsp
<%@pagelanguage="java"import="java.util.*,java.net.*"

	contentType="text/html;charset=UTF-8"%>

<%

	Stringpath=request.getContextPath();

	StringbasePath=request.getScheme()+"://"

	+request.getServerName()+":"+request.getServerPort()

	+path+"/";

%>


<!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.01Transitional//EN">

<html>

<head>

<basehref="<%=basePath%>">


<title>MyJSP'users.jsp'startingpage</title>


<metahttp-equiv="pragma"content="no-cache">

<metahttp-equiv="cache-control"content="no-cache">

<metahttp-equiv="expires"content="0">

<metahttp-equiv="keywords"content="keyword1,keyword2,keyword3">

<metahttp-equiv="description"content="Thisismypage">

</head>


<body>

<h1>用户信息</h1>

<br>

<br>

<br>

<br>

<%

	request.setCharacterEncoding("utf-8");

Stringusername="";

Stringpassword="";

Cookie[]cookies=request.getCookies();

if(cookies!=null&&cookies.length>0){

	for(Cookiec:cookies){

if(c.getName().equals("username")){//(名称)

	//解码用URLDecoder.decode(s,enc)

username=URLDecoder.decode(c.getValue(),"utf-8");

}

if(c.getName().equals("password")){

password=URLDecoder.decode(c.getValue(),"utf-8");

}

}

}

%>

	用户名:<%=username%>

<br>密码:<%=password%>

<br>

</body>

</html>

[/code](5)Session-Cookie


来自为知笔记(Wiz)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: