利用servlet创建session,并设置权限

来源:互联网 发布:博客系统源码 编辑:程序博客网 时间:2024/06/05 04:52

Session其实指的就是访问者从到达某个特定主页到离开为止的那段时间。每一访问者都会单独获得一个Session。在Web应用程序中,当一个用户访问该应用时,Session类型的变量可以供这个用户在该Web应用的所有页面中共享数据;如果另一个用户也同时访问该Web应用,他也拥有自己的Session变量,但两个用户之间无法通过Session变量共享信息。

1。创建一个JAVA类,名称User.java,放在user包下

package user;

public class User {
    
private String username;
    
private String login;
    
public String getLogin() {
        
return login;
    }

    
public void setLogin(String login) {
        
this.login = login;
    }

    
public String getuserName() {
        
return username;
    }

    
public void setuserName(String name) {
        
this.username = name;
    }

}

2。创建一个servlet,名称:SessionId,放在test包下,主要功能:创建一个session,并且显示sessionid,在session中存入user信息。

package test;

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;

import user.User;

public class SessionId extends HttpServlet {

    
/**
     * The doGet method of the servlet. <br>
     *
     * This method is called when a form has its tag value method equals to get.
     * 
     * 
@param request the request send by the client to the server
     * 
@param response the response send by the server to the client
     * 
@throws ServletException if an error occurred
     * 
@throws IOException if an error occurred
     
*/

    
public void doGet(HttpServletRequest request, HttpServletResponse response)
            
throws ServletException, IOException {
        
        
//String sessionId = request.getRequestedSessionId();
        HttpSession session = request.getSession();
        String sessionId 
= session.getId();
        User user 
= new User();
        user.setuserName(
"tom");
        user.setLogin(
"true");
        session.setAttribute(
"user", user);
        

        response.setContentType(
"text/html");
        PrintWriter out 
= response.getWriter();
        out
                .println(
"<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">");
        out.println(
"<HTML>");
        out.println(
"  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
        
        out.println(
"  <BODY>");
        out.print(
"    This is ");
        out.print(
this.getClass());
        out.println(
", using the GET method");
        out.println(
"<br>");
        out.println(sessionId);
        out.println(
"<br>");
        out.println(
"<a href = "" + response.encodeURL("DeleteAll"+ "" target = _blank> new page </a>");
        out.println(
"<br>");
        out.println(
"  </BODY>");
        out.println(
"</HTML>");
        out.flush();
        out.close();
        System.out.println(
"*************************" + user.getuserName());
    }


    
/**
     * The doPost method of the servlet. <br>
     *
     * This method is called when a form has its tag value method equals to post.
     * 
     * 
@param request the request send by the client to the server
     * 
@param response the response send by the server to the client
     * 
@throws ServletException if an error occurred
     * 
@throws IOException if an error occurred
     
*/

    
public void doPost(HttpServletRequest request, HttpServletResponse response)
            
throws ServletException, IOException {

        
this.doGet(request, response);
    }


}

主要代码:
  HttpSession session = request.getSession();
  String sessionId = session.getId();
  User user = new User();
  user.setuserName("tom");
  user.setLogin("true");
  session.setAttribute("user", user);

3。创建一个servlet,名称DeleteAll,在test包下,主要功能:设置权限

package test;

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;
import javax.xml.registry.JAXRException;
import user.User;

public class DeleteAll extends HttpServlet {

    
/**
     * The doGet method of the servlet. <br>
     *
     * This method is called when a form has its tag value method equals to get.
     * 
     * 
@param request the request send by the client to the server
     * 
@param response the response send by the server to the client
     * 
@throws ServletException if an error occurred
     * 
@throws IOException if an error occurred
     * 
@throws JAXRException 
     
*/

    
public void doGet(HttpServletRequest request, HttpServletResponse response)
            
throws ServletException, IOException {
        
        HttpSession httpSession 
= request.getSession();
        String sessionid 
= httpSession.getId();
        
        User user 
= (User) httpSession.getAttribute("user");
        System.out.println(
"---------------------" + user.getuserName());

        response.setContentType(
"text/html");
        PrintWriter out 
= response.getWriter();
        out
                .println(
"<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">");
        out.println(
"<HTML>");
        out.println(
"  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
        out.println(
"  <BODY>");
        out.print(
"    This is ");
        out.print(
this.getClass());
        out.println(
", using the GET method");
        out.println(
"<br>");
        
if (user == null{
            out.println(
"sorry,u must login again.");
        }
else{
            out.println(user.getuserName() 
+ " u can delete all");
        }

        out.println(
", <br>"+  sessionid +  " <br>");
        out.println(
"  </BODY>");
        out.println(
"</HTML>");
        out.flush();
        out.close();
    }


    
/**
     * The doPost method of the servlet. <br>
     *
     * This method is called when a form has its tag value method equals to post.
     * 
     * 
@param request the request send by the client to the server
     * 
@param response the response send by the server to the client
     * 
@throws ServletException if an error occurred
     * 
@throws IOException if an error occurred
     
*/

    
public void doPost(HttpServletRequest request, HttpServletResponse response)
            
throws ServletException, IOException {

        
this.doGet(request, response);
    }


}

主要代码:
User user = (User) httpSession.getAttribute("user");

if (user == null) {
   out.println("sorry,u must login again.");
  }else{
   out.println(user.getuserName() + " u can delete all");
  }

5。浏览器中运行,如果从SessionId中转入DeleteAll中,则会显示:

tom u can delete all ,

如果直接打开DeleteAll,则会显示:

sorry,u must login again。

 

原创粉丝点击