cookie的入门使用
package com. hspedu. cookie ;
import javax. servlet. ServletException ;
import javax. servlet. http. Cookie ;
import javax. servlet. http. HttpServlet ;
import javax. servlet. http. HttpServletRequest ;
import javax. servlet. http. HttpServletResponse ;
import java. io. IOException ;
import java. io. PrintWriter ;
public class CreateCookie extends HttpServlet {
protected void doPost ( HttpServletRequest request, HttpServletResponse response) throws ServletException , IOException {
System . out. println ( "CreateCookie 被调用..." ) ;
Cookie cookie = new Cookie ( "username" , "tom" ) ;
Cookie cookie2 = new Cookie ( "email" , "[email protected] " ) ;
response. setContentType ( "text/html;charset=utf-8" ) ;
response. addCookie ( cookie) ;
response. addCookie ( cookie2) ;
PrintWriter writer = response. getWriter ( ) ;
writer. println ( "创建cookie成功~ " ) ;
writer. flush ( ) ;
writer. close ( ) ;
}
protected void doGet ( HttpServletRequest request, HttpServletResponse response) throws ServletException , IOException {
doPost ( request, response) ;
}
}
修改cookie
package com. jxb. cooki ;
import javax. servlet. ServletException ;
import javax. servlet. http. Cookie ;
import javax. servlet. http. HttpServlet ;
import javax. servlet. http. HttpServletRequest ;
import javax. servlet. http. HttpServletResponse ;
import java. io. IOException ;
import java. io. PrintWriter ;
public class UpdateCookie extends HttpServlet {
protected void doPost ( HttpServletRequest request, HttpServletResponse response) throws ServletException , IOException {
System . out. println ( "UpdateCookie 被调用..." ) ;
String cookieName = "emailx" ;
Cookie [ ] cookies = request. getCookies ( ) ;
Cookie userNameCookie = new Cookie ( "username" , "hahaha" ) ;
Cookie cookie = CookieUtils . readCookieByName ( cookieName, cookies) ;
if ( null == cookie) {
System . out. println ( "当前访问 服务端的 浏览器没有 该cookie" ) ;
} else {
cookie. setValue ( "hsp-hi" ) ;
}
System . out. println ( "=====修改后的cookies信息=======" ) ;
for ( Cookie cookie1 : cookies) {
System . out. println ( "cookie name= " + cookie1. getName ( )
+ " value= " + cookie1. getValue ( ) ) ;
}
response. setContentType ( "text/html;charset=utf-8" ) ;
if ( cookie != null ) {
response. addCookie ( cookie) ;
}
if ( userNameCookie != null ) {
response. addCookie ( userNameCookie) ;
}
PrintWriter writer = response. getWriter ( ) ;
writer. println ( "完成修改cookie的任务.. " ) ;
writer. flush ( ) ;
writer. close ( ) ;
}
protected void doGet ( HttpServletRequest request, HttpServletResponse response) throws ServletException , IOException {
doPost ( request, response) ;
}
}
读取cookie
package com. jxb. cooki ;
import javax. servlet. http. Cookie ;
public class CookieUtils {
public static Cookie readCookieByName ( String name, Cookie [ ] cookies) {
if ( name == null || "" . equals ( name) || cookies == null || cookies. length == 0 ) {
return null ;
}
for ( Cookie cookie : cookies) {
if ( name. equals ( cookie. getName ( ) ) ) {
return cookie;
}
}
return null ;
}
}
package com. jxb. cooki ;
import javax. servlet. ServletException ;
import javax. servlet. http. Cookie ;
import javax. servlet. http. HttpServlet ;
import javax. servlet. http. HttpServletRequest ;
import javax. servlet. http. HttpServletResponse ;
import java. io. IOException ;
import java. io. PrintWriter ;
public class ReadCookieByNameServlet extends HttpServlet {
protected void doPost ( HttpServletRequest request, HttpServletResponse response) throws ServletException , IOException {
Cookie [ ] cookies = request. getCookies ( ) ;
Cookie emailCookie = CookieUtils . readCookieByName ( "username" , cookies) ;
if ( null != emailCookie) {
System . out. println ( "得到cookie name=" + emailCookie. getName ( )
+ " value= " + emailCookie. getValue ( ) ) ;
} else {
System . out. println ( "sorry, 没有这个cookie" ) ;
}
response. setContentType ( "text/html;charset=utf-8" ) ;
PrintWriter writer = response. getWriter ( ) ;
writer. println ( "完成读取cookie的任务.. " ) ;
writer. flush ( ) ;
writer. close ( ) ;
}
protected void doGet ( HttpServletRequest request, HttpServletResponse response) throws ServletException , IOException {
doPost ( request, response) ;
}
}
package com. jxb. cooki ;
import javax. servlet. ServletException ;
import javax. servlet. http. Cookie ;
import javax. servlet. http. HttpServlet ;
import javax. servlet. http. HttpServletRequest ;
import javax. servlet. http. HttpServletResponse ;
import java. io. IOException ;
import java. io. PrintWriter ;
public class ReadCookies extends HttpServlet {
protected void doPost ( HttpServletRequest request, HttpServletResponse response) throws ServletException , IOException {
System . out. println ( "ReadCookies 被调用.." ) ;
Cookie [ ] cookies = request. getCookies ( ) ;
if ( cookies != null && cookies. length != 0 ) {
for ( Cookie cookie : cookies) {
System . out. println ( "cookie name= " + cookie. getName ( )
+ " value= " + cookie. getValue ( ) ) ;
}
}
response. setContentType ( "text/html;charset=utf-8" ) ;
PrintWriter writer = response. getWriter ( ) ;
writer. println ( "读取cookie信息成功~ " ) ;
writer. flush ( ) ;
writer. close ( ) ;
}
protected void doGet ( HttpServletRequest request, HttpServletResponse response) throws ServletException , IOException {
doPost ( request, response) ;
}
}
删除cookie
package com. jxb. cooki ;
import javax. servlet. ServletException ;
import javax. servlet. http. Cookie ;
import javax. servlet. http. HttpServlet ;
import javax. servlet. http. HttpServletRequest ;
import javax. servlet. http. HttpServletResponse ;
import java. io. IOException ;
import java. io. PrintWriter ;
public class CookieLive extends HttpServlet {
protected void doPost ( HttpServletRequest request, HttpServletResponse response) throws ServletException , IOException {
System . out. println ( "CookieLive 被调用..." ) ;
Cookie cookie = new Cookie ( "job" , "java" ) ;
cookie. setMaxAge ( 60 ) ;
response. addCookie ( cookie) ;
Cookie [ ] cookies = request. getCookies ( ) ;
Cookie usernameCookie =
CookieUtils . readCookieByName ( "username" , cookies) ;
if ( usernameCookie != null ) {
usernameCookie. setMaxAge ( 0 ) ;
response. addCookie ( usernameCookie) ;
} else {
System . out. println ( "没有找到该cookie, 无法删除..." ) ;
}
Cookie cookie3 = new Cookie ( "dkey" , "dkey_value" ) ;
response. addCookie ( cookie3) ;
response. setContentType ( "text/html;charset=utf-8" ) ;
PrintWriter writer = response. getWriter ( ) ;
writer. println ( "设置cookie生命周期 " ) ;
writer. flush ( ) ;
writer. close ( ) ;
}
protected void doGet ( HttpServletRequest request, HttpServletResponse response) throws ServletException , IOException {
doPost ( request, response) ;
}
}
cookie路径
package com. jxb. cooki ;
import javax. servlet. ServletException ;
import javax. servlet. http. Cookie ;
import javax. servlet. http. HttpServlet ;
import javax. servlet. http. HttpServletRequest ;
import javax. servlet. http. HttpServletResponse ;
import java. io. IOException ;
import java. io. PrintWriter ;
public class CookiePath extends HttpServlet {
protected void doPost ( HttpServletRequest request, HttpServletResponse response) throws ServletException , IOException {
System . out. println ( "CookiePath 被调用..." ) ;
Cookie cookie = new Cookie ( "address" , "bj" ) ;
Cookie cookie2 = new Cookie ( "salary" , "20000" ) ;
cookie. setPath ( request. getContextPath ( ) ) ;
cookie2. setPath ( request. getContextPath ( ) + "/aaa" ) ;
response. addCookie ( cookie) ;
response. addCookie ( cookie2) ;
response. setContentType ( "text/html;charset=utf-8" ) ;
PrintWriter writer = response. getWriter ( ) ;
writer. println ( "设置cookie有效路径成功 " ) ;
writer. flush ( ) ;
writer. close ( ) ;
}
protected void doGet ( HttpServletRequest request, HttpServletResponse response) throws ServletException , IOException {
doPost ( request, response) ;
}
}
自动保存登陆名
package com. jxb. cooki ;
import javax. servlet. ServletException ;
import javax. servlet. http. Cookie ;
import javax. servlet. http. HttpServlet ;
import javax. servlet. http. HttpServletRequest ;
import javax. servlet. http. HttpServletResponse ;
import java. io. IOException ;
import java. io. PrintWriter ;
public class UserUIServlet extends HttpServlet {
protected void doPost ( HttpServletRequest request, HttpServletResponse response) throws ServletException , IOException {
Cookie [ ] cookies = request. getCookies ( ) ;
Cookie loginuserCookie = CookieUtils . readCookieByName ( "loginuser" , cookies) ;
String username = "" ;
if ( loginuserCookie != null ) {
username = loginuserCookie. getValue ( ) ;
}
response. setContentType ( "text/html;charset=utf-8" ) ;
PrintWriter writer = response. getWriter ( ) ;
writer. println ( "\n" +
"\n" +
"\n" +
" \n" +
" 登录页面 \n" +
"\n" +
"\n" +
"用户登录界面 \n" +
" +
" u: + username + "\" name=\"username\"> \n" +
" p: \n" +
" \n" +
"\n" +
"\n" +
"" ) ;
writer. flush ( ) ;
writer. close ( ) ;
}
protected void doGet ( HttpServletRequest request, HttpServletResponse response) throws ServletException , IOException {
doPost ( request, response) ;
}
}
package com. jxb. cooki ;
import javax. servlet. ServletException ;
import javax. servlet. http. Cookie ;
import javax. servlet. http. HttpServlet ;
import javax. servlet. http. HttpServletRequest ;
import javax. servlet. http. HttpServletResponse ;
import java. io. IOException ;
import java. io. PrintWriter ;
public class LoginServlet extends HttpServlet {
protected void doPost ( HttpServletRequest request, HttpServletResponse response) throws ServletException , IOException {
String username = request. getParameter ( "username" ) ;
String pwd = request. getParameter ( "pwd" ) ;
response. setContentType ( "text/html;charset=utf-8" ) ;
PrintWriter writer = response. getWriter ( ) ;
if ( "hspedu" . equals ( username) && "123456" . equals ( pwd) ) {
Cookie loginuserCookie = new Cookie ( "loginuser" , username) ;
loginuserCookie. setMaxAge ( 3600 * 24 * 3 ) ;
response. addCookie ( loginuserCookie) ;
writer. println ( "登录OK " ) ;
} else {
writer. println ( "登录失败 " ) ;
}
writer. flush ( ) ;
writer. close ( ) ;
}
protected void doGet ( HttpServletRequest request, HttpServletResponse response) throws ServletException , IOException {
doPost ( request, response) ;
}
}
cookie中文的解码与编码
package com. jxb. cooki ;
import javax. servlet. ServletException ;
import javax. servlet. http. Cookie ;
import javax. servlet. http. HttpServlet ;
import javax. servlet. http. HttpServletRequest ;
import javax. servlet. http. HttpServletResponse ;
import java. io. IOException ;
import java. io. PrintWriter ;
import java. net. URLEncoder ;
public class EncoderCookie extends HttpServlet {
protected void doPost ( HttpServletRequest request, HttpServletResponse response) throws ServletException , IOException {
String company = URLEncoder . encode ( "韩顺平教育" , "utf-8" ) ;
Cookie cookie = new Cookie ( "company" , company) ;
response. addCookie ( cookie) ;
response. setContentType ( "text/html;charset=utf-8" ) ;
PrintWriter writer = response. getWriter ( ) ;
writer. println ( "设置中文cookie成功 " ) ;
writer. flush ( ) ;
writer. close ( ) ;
}
protected void doGet ( HttpServletRequest request, HttpServletResponse response) throws ServletException , IOException {
doPost ( request, response) ;
}
}
package com. jxb. cooki ;
import javax. servlet. ServletException ;
import javax. servlet. http. Cookie ;
import javax. servlet. http. HttpServlet ;
import javax. servlet. http. HttpServletRequest ;
import javax. servlet. http. HttpServletResponse ;
import java. io. IOException ;
import java. io. PrintWriter ;
import java. net. URLDecoder ;
public class ReadCookie2 extends HttpServlet {
protected void doPost ( HttpServletRequest request, HttpServletResponse response) throws ServletException , IOException {
System . out. println ( "ReadCookie2 被调用.." ) ;
Cookie [ ] cookies = request. getCookies ( ) ;
Cookie companyCookie = CookieUtils . readCookieByName ( "company" , cookies) ;
String companyVal = companyCookie. getValue ( ) ;
System . out. println ( "companyVal= " + companyVal) ;
companyVal = URLDecoder . decode ( companyVal, "utf-8" ) ;
System . out. println ( "解码后 companyVal= " + companyVal) ;
response. setContentType ( "text/html;charset=utf-8" ) ;
PrintWriter writer = response. getWriter ( ) ;
writer. println ( "读取中文cookie解码成功~ " ) ;
writer. flush ( ) ;
writer. close ( ) ;
}
protected void doGet ( HttpServletRequest request, HttpServletResponse response) throws ServletException , IOException {
doPost ( request, response) ;
}
}
创建session
package com. jxb. cooki. session ;
import javax. servlet. ServletException ;
import javax. servlet. http. HttpServlet ;
import javax. servlet. http. HttpServletRequest ;
import javax. servlet. http. HttpServletResponse ;
import javax. servlet. http. HttpSession ;
import java. io. IOException ;
import java. io. PrintWriter ;
public class CreateSession extends HttpServlet {
protected void doPost ( HttpServletRequest request, HttpServletResponse response) throws ServletException , IOException {
HttpSession session = request. getSession ( ) ;
System . out. println ( "CreateSession 当前sessionid= " + session. getId ( ) ) ;
session. setAttribute ( "email" , "[email protected] " ) ;
response. setContentType ( "text/html;charset=utf-8" ) ;
PrintWriter writer = response. getWriter ( ) ;
writer. println ( "创建/操作session成功... " ) ;
writer. flush ( ) ;
writer. close ( ) ;
}
protected void doGet ( HttpServletRequest request, HttpServletResponse response) throws ServletException , IOException {
doPost ( request, response) ;
}
}
读取session
package com. jxb. cooki. session ;
import javax. servlet. ServletException ;
import javax. servlet. http. HttpServlet ;
import javax. servlet. http. HttpServletRequest ;
import javax. servlet. http. HttpServletResponse ;
import javax. servlet. http. HttpSession ;
import java. io. IOException ;
import java. io. PrintWriter ;
public class ReadSession extends HttpServlet {
protected void doPost ( HttpServletRequest request, HttpServletResponse response) throws ServletException , IOException {
HttpSession session = request. getSession ( ) ;
System . out. println ( "ReadSession sessionid= " + session. getId ( ) ) ;
Object email = session. getAttribute ( "email" ) ;
if ( email != null ) {
System . out. println ( "session属性 email= " + ( String ) email) ;
} else {
System . out. println ( "session中没有 email属性 " ) ;
}
response. setContentType ( "text/html;charset=utf-8" ) ;
PrintWriter writer = response. getWriter ( ) ;
writer. println ( "读取session成功... " ) ;
writer. flush ( ) ;
writer. close ( ) ;
}
protected void doGet ( HttpServletRequest request, HttpServletResponse response) throws ServletException , IOException {
doPost ( request, response) ;
}
}
删除session
package com. jxb. cooki. session ;
import javax. servlet. ServletException ;
import javax. servlet. http. HttpServlet ;
import javax. servlet. http. HttpServletRequest ;
import javax. servlet. http. HttpServletResponse ;
import javax. servlet. http. HttpSession ;
import java. io. IOException ;
import java. io. PrintWriter ;
public class DeleteSession extends HttpServlet {
protected void doPost ( HttpServletRequest request, HttpServletResponse response) throws ServletException , IOException {
System . out. println ( "DeleteSession 被调用..." ) ;
HttpSession session = request. getSession ( ) ;
session. invalidate ( ) ;
response. setContentType ( "text/html;charset=utf-8" ) ;
PrintWriter writer = response. getWriter ( ) ;
writer. println ( "删除session成功 " ) ;
writer. flush ( ) ;
writer. close ( ) ;
}
protected void doGet ( HttpServletRequest request, HttpServletResponse response) throws ServletException , IOException {
doPost ( request, response) ;
}
}
session生命周期
package com. jxb. cooki. session ;
import javax. servlet. ServletException ;
import javax. servlet. http. HttpServlet ;
import javax. servlet. http. HttpServletRequest ;
import javax. servlet. http. HttpServletResponse ;
import javax. servlet. http. HttpSession ;
import java. io. IOException ;
import java. io. PrintWriter ;
public class CreateSession2 extends HttpServlet {
protected void doPost ( HttpServletRequest request, HttpServletResponse response) throws ServletException , IOException {
System . out. println ( "CreateSession2 被调用" ) ;
HttpSession session = request. getSession ( ) ;
System . out. println ( "CreateSession2 sid= " + session. getId ( ) ) ;
session. setMaxInactiveInterval ( 60 ) ;
session. setAttribute ( "u" , "jack" ) ;
response. setContentType ( "text/html;charset=utf-8" ) ;
PrintWriter writer = response. getWriter ( ) ;
writer. println ( "创建session成功, 设置生命周期60s " ) ;
writer. flush ( ) ;
writer. close ( ) ;
}
protected void doGet ( HttpServletRequest request, HttpServletResponse response) throws ServletException , IOException {
doPost ( request, response) ;
}
}
package com. jxb. cooki. session ;
import javax. servlet. ServletException ;
import javax. servlet. http. HttpServlet ;
import javax. servlet. http. HttpServletRequest ;
import javax. servlet. http. HttpServletResponse ;
import javax. servlet. http. HttpSession ;
import java. io. IOException ;
public class ReadSession2 extends HttpServlet {
protected void doPost ( HttpServletRequest request, HttpServletResponse response) throws ServletException , IOException {
HttpSession session = request. getSession ( ) ;
System . out. println ( "ReadSession2 sid= " + session. getId ( ) ) ;
Object u = session. getAttribute ( "u" ) ;
if ( u != null ) {
System . out. println ( "读取到session属性 u= " + ( String ) u) ;
} else {
System . out. println ( "读取不到session属性 u 说明原来的session被销毁" ) ;
}
}
protected void doGet ( HttpServletRequest request, HttpServletResponse response) throws ServletException , IOException {
doPost ( request, response) ;
}
}
CreateSession 当前sessionid= AC78607B4FCC8DC93DE3B3960CE247E7
ReadSession sessionid= AC78607B4FCC8DC93DE3B3960CE247E7
session属性 email= zs@qq.com
DeleteSession 被调用. . .
ReadSession sessionid= 9465084646 BE295082D77AB68F44F7CE
session中没有 email属性
CreateCookie 被调用. . .
CreateSession2 被调用
CreateSession2 sid= 9465084646 BE295082D77AB68F44F7CE
ReadSession2 sid= 9465084646 BE295082D77AB68F44F7CE
读取到session属性 u= jack
session作业
< ! DOCTYPE html>
< html lang= "en" >
< head>
< meta charset= "UTF-8" >
< title> 登录失败< / title>
< / head>
< body>
< h1> 登录失败< / h1>
< ! --
老韩回顾 web工程路径专题
1. a 标签是 浏览器解析
2. 第一 / 被解析成 http: / / localhost: 8080 /
3. 如果没有 / 会以当前浏览器地址栏 的 http: / / localhost: 8080 / 工程路径. . /资源 去掉资源部分作为参考路径
4 其它的回顾请大家看 web工程路径专题~ ~ , 他会贯彻 整个java后端开发
-- >
< a href= "/cooki/userlogin.html" > 点击重新登录< / a>
< / body>
< / html>
package com. jxb. cooki. session. homework ;
import javax. servlet. ServletException ;
import javax. servlet. http. HttpServlet ;
import javax. servlet. http. HttpServletRequest ;
import javax. servlet. http. HttpServletResponse ;
import javax. servlet. http. HttpSession ;
import java. io. IOException ;
public class LoginCheckServlet extends HttpServlet {
protected void doPost ( HttpServletRequest request, HttpServletResponse response) throws ServletException , IOException {
System . out. println ( "LoginCheckServlet 被调用.." ) ;
String username = request. getParameter ( "username" ) ;
String password = request. getParameter ( "password" ) ;
if ( "666666" . equals ( password) ) {
HttpSession session = request. getSession ( ) ;
session. setAttribute ( "loginuser" , username) ;
request. getRequestDispatcher ( "/manage" ) . forward ( request, response) ;
} else {
request. getRequestDispatcher ( "/error.html" ) . forward ( request, response) ;
}
}
protected void doGet ( HttpServletRequest request, HttpServletResponse response) throws ServletException , IOException {
doPost ( request, response) ;
}
}
package com. jxb. cooki. session. homework ;
import javax. servlet. ServletException ;
import javax. servlet. http. HttpServlet ;
import javax. servlet. http. HttpServletRequest ;
import javax. servlet. http. HttpServletResponse ;
import javax. servlet. http. HttpSession ;
import java. io. IOException ;
import java. io. PrintWriter ;
public class ManageServlet extends HttpServlet {
protected void doPost ( HttpServletRequest request, HttpServletResponse response) throws ServletException , IOException {
HttpSession session = request. getSession ( ) ;
Object loginuser = session. getAttribute ( "loginuser" ) ;
if ( loginuser == null ) {
response. sendRedirect ( request. getContextPath ( ) + "/userlogin.html" ) ;
return ;
} else {
response. setContentType ( "text/html;charset=utf-8" ) ;
PrintWriter writer = response. getWriter ( ) ;
writer. println ( "用户管理页面 " ) ;
writer. println ( "欢迎你, 管理员:" + loginuser. toString ( ) ) ;
writer. flush ( ) ;
writer. close ( ) ;
}
}
protected void doGet ( HttpServletRequest request, HttpServletResponse response) throws ServletException , IOException {
doPost ( request, response) ;
}
}