Session技术

今天讲的总的可以归为三点:
第一个就是
   首先是request.getSession();方法,如果是第一次获取,原先没有Session技术;服务器会自动向浏览器返回一个id;
  HttpSession session=request.getSession();//可以带参数,如果是true代表没有自动创建,false代表没有也不创建;
  session.setAttribute(“name”,”nameValue”);//为session属性赋值;
  
  
  然后在创建一个Servlet类,来获取session的值;
  首先解决乱码问题;
  response.setContentType(“text/html;charset=utf-8”);
  PrintWriter out = response.getWriter();
  //获取session的id
  HttpSession session = resquest.getSession();
  String  value = session.getAttribute(“name”);
  out.print(“购买的商品是”+value);
  
  第二种就是
  把这两个弄成一个html文件,进行超连接,第一个叫购物。第二个叫结账
  有这个一种情况,你购物完之后,获取一个id但是不小心关了,再重新点开,打开结账发现值为空,是不是很纠结;所以解决一下这个问题,
  需要在购物内加入一个Cookie来保存这个id到本地;
   创建cookie应该都会了吧,在这里也就不详细介绍了,
  首先获取一个session的id,放到cookie里保存;
  String id  = session.getID();
  Cookie cookie = new Cookie(“JESSIONID”,id);//参数值与session一致
  cookie.setMaxAge(30*60);//与浏览器保存时间一致;
  response.addCookie(cookie);
  
  第三种就是用户把浏览器的Cookie设置关闭,导致cookie无法使用;
  这个需要使用URL重写;
IE禁用Cookie后的session处理
解决方案:URL重写
response. encodeRedirectURL(java.lang.String url)
用于对sendRedirect方法后的url地址进行重写。
response. encodeURL(java.lang.String url)
用于对表单action和超链接的url地址进行重写



package com.csdn.Session;

import java.io.IOException;
import java.io.PrintWriter;

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 javax.servlet.http.HttpSession;

public class SessionTest1 extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

response.setContentType("text/html");
PrintWriter out = response.getWriter();

HttpSession session= request.getSession();
String id = session.getId();
Cookie cookie=new Cookie("JSESSIONID",id);
cookie.setMaxAge(30*60);
response.addCookie(cookie);
session.setAttribute("name", "手机");

}

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);

}

}





package com.csdn.Session;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class SessionTest2 extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();

HttpSession session = request.getSession();


   String value=(String) session.getAttribute("name");
   
    out.print("你购买的商品是"+value);

}

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);

}

}



package com.csdn.Session;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class SessionTest3 extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
           
request.getSession();

String url1=response.encodeURL("/SessionTest/SessionTest1");
String url2=response.encodeURL("/SessionTest/SessionTest2");

out.print("<a href='"+url1+"'> 购买</a><br/>");
out.print("<a href='"+url2+"'>结账</a>");
}

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);

}

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值