1.原理图(简图)
2.代码
设置session
import javax.servlet.http.*;import javax.servlet.*;import java.io.*;public class setSession extends HttpServlet{
public void doGet(HttpServletRequest req,
HttpServletResponse resp) throws ServletException, IOException{ HttpSession session=req.getSession();//返回与当前request相关联的session,如果没有则在服务器端创建一个; session.setAttribute("username","zhangjie"); resp.getWriter().println("------setSession() is ok--------"); }}获取session:
import javax.servlet.http.*;import javax.servlet.*;import java.io.*;public class getSession extends HttpServlet{
public void doGet(HttpServletRequest req,
HttpServletResponse resp) throws ServletException, IOException{ HttpSession session=req.getSession();//返回与当前request相关联的session,如果没有则在服务器端创建一个; resp.getWriter().println(session.getAttribute("username")); resp.getWriter().println("------getSession() is ok--------"); }}