0
点赞
收藏
分享

微信扫一扫

理解JSP底层

楠蛮鬼影 2024-06-16 阅读 25
import java.net.URLDecoder;

public class login_jsp{
    
    //JSP的9大内置对象
    private JSPWriter out;//当前JSP输出流对象
    private HttpServletRequest request;//请求对象
    private HttpServletResponse response;//响应对象
    private HttpSession session;//会话对象
    private ServletContext application;//全局域对象 -- 表示整个项目
    private ServletConfig config;//当前JSP配置文件对象
    private Object page = this;//当前JSP对象
    private PageContext pageContext;//当前JSP上下文关系对象:可以获取其他8大内置对象
    //Exception exception -- 当前JSP异常对象
    
    /**
    	9大内置对象中的四大域:
    		理解:这四大域都可以存储数据
    	
        请求域:
        	将数据存储到请求对象中,返回响应后,这个请求对象就会被销毁
    		request.setAttribute(key,value);
    		Object obj = request.getAttribute(key);
    		
    	会话域:
    		将数据存储到会话对象中,默认30分钟有效,多个客户端的Session不能共享数据的
    		session.setAttribute(key,value);
    		Object obj = session.getAttribute(key);
    		
    	全局域:
    		将数据存储到全局域对象中,全局域表示整个项目,项目关闭该对象才会被销毁,
    		全局域中的数据是整个项目共享的
    		application.setAttribute(key,value);
    		Object obj = application.getAttribute(key);
    	
    	页面域:
    		将数据存储到页面域对象中,数据只能在当前页使用
    		pageContext.setAttribute(key,value);
    		Object obj = pageContext.getAttribute(key);
    */
    
    
    
    public void jsp_service(){
        //设置响应编码格式
        response.setContentType("text/html;charset=UTF-8");
        
        //jsp文件中遇到Html代码,就用输出流传输给客户端
        out.writer("<html>\n\r");
        out.writer("<head>\n\r");
        out.writer("    <title>Title</title>");
        out.writer("</head>");
        out.writer("<body>");
	
        //jsp文件中遇到Java代码,就执行即可
    	Cookie[] cookies = request.getCookies();
        if(cookies != null){
          int count = 0;
          for (Cookie cookie : cookies) {
            String key = cookie.getName();
            String value = URLDecoder.decode(cookie.getValue(),"UTF-8");

            if(key.equals("username")){
              session.setAttribute("username",value);
              count++;
            }
            if(key.equals("name")){
              session.setAttribute("name",value);
              count++;
            }
            if(key.equals("role")){
              session.setAttribute("role",value);
              count++;
            }
          }
          if(count == 3){
            response.sendRedirect("index.jsp");
          }
        }   
    }
    
    .......
    
}


举报

相关推荐

0 条评论