会话session是客户和服务器之间的不断地请求相应序列。 状态是有记忆能力的。 web容器用httpsession代表会话对象。 案例1:session的初识: 这里就是创建了一个会话对象,用一个图表来获得会话对象的属性,这里访问次数设置了accessCount局部变量,注意这里设置的是Integer类型的,通过会话存储accessCount到accessCount1属性里,accessCount就有了状态,记忆功能,以至于之后访问都会+1
package com.rc.hui01;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.time.LocalTime;
import javax.servlet.annotation.WebServlet;
@WebServlet("/ShowSessionServlet")
public class ShowSessionServlet extends HttpServlet{
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
HttpSession session = request.getSession(true);
String heading = null;
Integer accessCount = (Integer)session.getAttribute("accessCount1");
if(accessCount==null){
accessCount = new Integer(1);
heading = "欢迎您,首次登录该页面!";
}else{
heading = "欢迎您,再次访问该页面!";
accessCount = accessCount+1;
}
session.setAttribute("accessCount1",accessCount);
PrintWriter out = response.getWriter();
out.println("<html><head>");
out.println("<title>会话跟踪示例</title></head>");
out.println("<body><center>");
out.println("<h4>"+heading
+"<a href='ShowSessionServlet'>再次访问</a>"+"</h4>");
out.println("<table border='0'>");
out.println("<tr bgcolor=\"ffad00\"><td>信息</td><td>值</td>\n");
String state = session.isNew()?"新会话":"旧会话";
out.println("<tr><td>会话状态:<td>"+state+"\n");
out.println("<tr><td>会话ID:<td>"+session.getId()+"\n");
out.println("<tr><td>创建时间:<td>");
out.println(""+session.getCreationTime()+"\n");
out.println("<tr><td>最近访问时间:<td>");
out.println(""+session.getLastAccessedTime()+"\n");
out.println("<tr><td>最大不活动时间:<td>"+
session.getMaxInactiveInterval()+"\n");
out.println("<tr><td>Cookie:<td>"+request.getHeader("Cookie")+"\n");
out.println("<tr><td>已被访问次数:<td>"+accessCount+"\n");
out.println("</table>");
out.println("</center></body></html>");
}
}
案例2:猜数字游戏: 这个是生成了一个随机数,把这个随机数保存在会话对象里面,然后和你输的值比较来显示不同的画面
package com.rc.hui01;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.WebServlet;
@WebServlet("/GuessNumberServlet")
public class GuessNumberServlet extends HttpServlet{
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
int magic = (int)(Math.random()*101);
HttpSession session = request.getSession();
session.setAttribute("num",new Integer(magic));
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
out.println("<html><body>");
out.println("我想出一个0到100之间的数,请你猜!");
out.println("<form action='GuessNumberServlet' method='post'>");
out.println("<input type='text' name='guess' />");
out.println("<input type='submit' value='确定'/>");
out.println("</form>");
out.println("</body></html>");
}
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
int guess = Integer.parseInt(request.getParameter("guess"));
HttpSession session = request.getSession();
int magic = (Integer)session.getAttribute("num");
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
out.println("<html><body>");
if(guess==magic){
session.invalidate();
out.println("祝贺你,答对了!");
out.println("<a href ='GuessNumberServlet'>再猜一次.</a>");
}else if(guess>magic){
out.println("太大了! 请重猜!");
}else{
out.println("太小了! 请重猜!");
}
out.println("<form action='GuessNumberServlet' method='post'>");
out.println("<input type='text' name='guess' />");
out.println("<input type='submit' value='确定'/>");
out.println("</form>");
out.println("</body></html>");
}
}
|