首页 > 代码库 > session 的工作原理以及使用细节和url编码

session 的工作原理以及使用细节和url编码

/**********************************************模拟页面************************************************************/

package 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 SessionDemo1 extends HttpServlet {
    //session底层封装了cookie所以,如果,浏览器禁用了cookie那么sessionID就没法带过去了
    //解决的办法就是,通过超链接的形式将sessionId带过去,此时超链接的url地址要经过编码
    //这样就保证,即使你把浏览器的cookie禁用掉,我也能将sessionId带过去
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //模拟这种场景
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;chatset=UTF-8");
        PrintWriter out = response.getWriter();
        
        request.getSession();
        //对url进行编码
        String url1 = response.encodeURL("/day07/SessionDemo2");
        String url2 = response.encodeURL("/day07/SessionDemo3");
        out.print("<a href=http://www.mamicode.com/‘"+url1+"‘>购物</a>     ");
        out.print("<a href=http://www.mamicode.com/‘"+url2+"‘>结帐</a>");
    }

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

}
/********************************************************购买页面***************************************************************/

package 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 SessionDemo2 extends HttpServlet {
    
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;chatset=UTF-8");
        
        HttpSession session = request.getSession();
        session.setAttribute("good", "电视机");
        
    }

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

}
/*******************************************************8结帐页面******************************************************************/

package 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 SessionDemo3 extends HttpServlet {
    
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;chatset=UTF-8");
        PrintWriter out = response.getWriter();
        
        HttpSession session = request.getSession(false);
        String value = http://www.mamicode.com/(String) session.getAttribute("good");
        out.print("您购买的商品是"+value);
        
    }

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

}