在Servlet中session一个值,如何在java类中使用session的值
展开全部
先得到session,再从session中获取到参数值.
追问
获取到了,但不知道怎么在普通类中使用,怎么传参啊?
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
1、可以通过HttpServletRequest的getSession()方法获得,此方法会返回一个布尔值来表示是否成功得到了Session。
2、尝试获得键名为“VisitCounter”的session值,将获得的值转换为Integer对象。
3、如果是空则说明session还没有设置,就往session中添加VisitCounter,否认则对VisitCounter+1并保存到session中。
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
/**
* Example Servlet
* @author outofmemory.cn
*/
public class ExampleServlet extends HttpServlet {
/** Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
* @param request servlet request
* @param response servlet response
*/
protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
printPageStart(out);
//Obtain the session object, create a new session if doesn't exist
HttpSession session = request.getSession(true);
//Check if our session variable is set, if so, get the session variable value
//which is an Integer object, and add one to the value.
//If the value is not set, create an Integer object with the default value 1.
//Add the variable to the session overwriting any possible present values.
Integer param = (Integer) session.getAttribute("MySessionVariable");
if (param != null) {
session.setAttribute("MySessionVariable", new Integer(param.intValue() + 1));
param = (Integer) session.getAttribute("MySessionVariable");
} else {
param = new Integer(1);
session.setAttribute("MySessionVariable", param);
}
out.println("You have displayed this page <b>" + param.intValue() + "</b> times this session.<br/><br/>");
out.println("Hit the browsers refresh button.");
printPageEnd(out);
}
/** Prints out the start of the html page
* @param out the PrintWriter object
*/
private void printPageStart(PrintWriter out)
{
out.println("<html>");
out.println("<head>");
out.println("<title>Example Servlet of how to store and retrieve session variables</title>");
out.println("</head>");
out.println("<body>");
}
/** Prints out the end of the html page
* @param out the PrintWriter object
*/
private void printPageEnd(PrintWriter out)
{
out.println("</body>");
out.println("</html>");
out.close();
}
}
2、尝试获得键名为“VisitCounter”的session值,将获得的值转换为Integer对象。
3、如果是空则说明session还没有设置,就往session中添加VisitCounter,否认则对VisitCounter+1并保存到session中。
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
/**
* Example Servlet
* @author outofmemory.cn
*/
public class ExampleServlet extends HttpServlet {
/** Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
* @param request servlet request
* @param response servlet response
*/
protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
printPageStart(out);
//Obtain the session object, create a new session if doesn't exist
HttpSession session = request.getSession(true);
//Check if our session variable is set, if so, get the session variable value
//which is an Integer object, and add one to the value.
//If the value is not set, create an Integer object with the default value 1.
//Add the variable to the session overwriting any possible present values.
Integer param = (Integer) session.getAttribute("MySessionVariable");
if (param != null) {
session.setAttribute("MySessionVariable", new Integer(param.intValue() + 1));
param = (Integer) session.getAttribute("MySessionVariable");
} else {
param = new Integer(1);
session.setAttribute("MySessionVariable", param);
}
out.println("You have displayed this page <b>" + param.intValue() + "</b> times this session.<br/><br/>");
out.println("Hit the browsers refresh button.");
printPageEnd(out);
}
/** Prints out the start of the html page
* @param out the PrintWriter object
*/
private void printPageStart(PrintWriter out)
{
out.println("<html>");
out.println("<head>");
out.println("<title>Example Servlet of how to store and retrieve session variables</title>");
out.println("</head>");
out.println("<body>");
}
/** Prints out the end of the html page
* @param out the PrintWriter object
*/
private void printPageEnd(PrintWriter out)
{
out.println("</body>");
out.println("</html>");
out.close();
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |