admin管理员组

文章数量:1122855

Session。



例子:Welcome.jsp和loginservlet.java

先在servlet里面写入session代码

public void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

HttpSession session=null;
//通过request来得到session对象
session=request.getSession();


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

//12290890117-软开1班-陈允仁 
String username=request.getParameter("uname");
String userpass=request.getParameter("upass");
if(username.equals("jack")&&userpass.equals("abcd")){
//向session存入数据,这里是存放登录信息
session.setAttribute("LoginUser", username);

RequestDispatcher dis=request.getRequestDispatcher("welcome.jsp");
dis.forward(request,response);

}else{
response.sendRedirect("login.jsp");

}

再在jsp页面写session代码

  <%
  String uname=(String)session.getAttribute("Loginuser");
  if(uname!=null){
 
  response.sendRedirect("login.jsp");
  return;
 
  }


   %>

本文标签: session