JSP脚本的9个内置对象

JSP的内置对象都是Servlet API接口的实例,在JSP脚本生成的Servlet中的_jspService方法中创建实例(为什么没有exception对象?因为当页面中page指令的isErrorPage属性为true时,才可以使用exception对象。),所以我们可以在JSP的输出表达式中使用这些内置对象。
  public void _jspService(final javax.servlet.http.HttpServletRequest request, final javax.servlet.http.HttpServletResponse response)
        throws java.io.IOException, javax.servlet.ServletException {

    final javax.servlet.jsp.PageContext pageContext;
    javax.servlet.http.HttpSession session = null;
    final javax.servlet.ServletContext application;
    final javax.servlet.ServletConfig config;
    javax.servlet.jsp.JspWriter out = null;
    final java.lang.Object page = this;
    javax.servlet.jsp.JspWriter _jspx_out = null;
    javax.servlet.jsp.PageContext _jspx_page_context = null;

  }
 
1:application对象
  1:在整个Web系统中多个JSP,Servlet之间共享数据。‘
  2:访问Web应用的参数配置。
 
<%!
int i; 
%>
<%
application.setAttribute("counter",String.valueOf(++i));
%>
<%=application.getAttribute("counter") %>

Servlet中访问application变量:

    /**
     * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
     */
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        
         response.setContentType("text/html;charset=GBK");

         ServletContext servletContext = getServletConfig().getServletContext();
         PrintWriter out = response.getWriter();
         out.println(servletContext.getAttribute("counter"));
    }

获得Web应用配置参数:

<%
	application.getInitParameter("driver");
	application.getInitParameter("url");
	application.getInitParameter("username");
	application.getInitParameter("password");
%>

 web.xml:

    <context-param>
        <param-name>driver</param-name>
        <param-value>com.mysql.jdbc.Driver</param-value>
    </context-param>

 

 
 
2:out对象
 页面输出流
  /**
     * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
     */
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        
         response.setContentType("text/html;charset=GBK");

         PrintWriter out = response.getWriter();
         out.println("相见恨晚");
    }

3:config对象

  代表当前JSP页面的配置信息,在JSP中使用较少,Servlet中使用较多。

4:exception对象

  Throwable的实例,JSP页面异常机制的一部分。

  JSP生成Servlet,所有的JSP脚本,静态的HTML都会放在try块中,出现异常,如果指定了errorPage,就会转发到指定的error页面,如果没有就在当前页面输出异常。

5:pageContext对象

  代表上下文,访问JSP之间的共享数据。使用pageContext对象可以访问application,request,session,page范围内的对象。

  getAttribute(String name) 取得page的name属性

  getAttribute(String name,int scope)

        <%
            pageContext.getAttribute("test");
            
            pageContext.getAttribute("test", pageContext.PAGE_SCOPE);
            pageContext.getAttribute("test", pageContext.APPLICATION_SCOPE);
            pageContext.getAttribute("test", pageContext.SESSION_SCOPE);
            pageContext.getAttribute("test", pageContext.REQUEST_SCOPE);
            
        %>

6:request对象

  request对象封装着一次用户请求,request对象是HttpServletRequest接口的实例

  getParameter()

  getHeader()

        <%
            Enumeration<String> headerNames = request.getHeaderNames();
        
            while(headerNames.hasMoreElements()){
                String headerName = headerNames.nextElement();
            }
            
            request.setCharacterEncoding("gb2312");
            String name = request.getParameter("name");
            String[] colors = request.getParameterValues("color");
        %>
GET请求中含有中文字符
        <%
            
            //GET请求中含有中文字符
            //获取原始查询字符串
            String queryString = request.getQueryString();
            
            //使用URLDecoder解码字符串
            String decoderString = java.net.URLDecoder.decode(queryString, "gbk");
            
            String[] params = decoderString.split("&");
            
        %>

7:response对象

  1:response对象生成非字符的响应。

  2:重定向。

        <%
            response.sendRedirect("target.jsp");
        %>

  3:增加Cookie

        <%
            String name = request.getParameter("username");
        
            Cookie cookie = new Cookie("username",name);
            cookie.setMaxAge(6*24*60*60);
            response.addCookie(cookie);
        %>
        <%
            Cookie[] cookies = request.getCookies();
        
            for(Cookie cookie : cookies){
                
                if("username".equals(cookie.getName())){
                    out.print(cookie.getValue());
                }
            }
        %>

  默认情况下Cookie的值是不允许出现中文的,但可以借助URLEncoder先对中文进行编码,编码后的值存为Cookie的值。读取Cookie的时候应该先读取,再用URLDecoder进行解码。

8:session对象

  代表一次会话(从浏览器连接服务器开始到与服务器断开连接),可以在多个页面之间共享数据,一旦浏览器关闭,session的数据就会全部丢失。

   session对象用于跟踪用户的会话信息,判断用户是否登录等。

  只有访问JSP、Servlet等程序时才会创建Session,只访问HTML、IMAGE等静态资源并不会创建Session,可调用request.getSession(true)强制生成Session。

  服务器会把长时间没有活动的session清除掉,session就会失效,tomcat默认是20分钟,

  调用session对象的invalidate方法也会让session失效。

  虽然Session保存在服务器,对客户端是透明的,它的正常运行仍然需要客户端浏览器的支持。这是因为Session需要使用Cookie作为识别标志。HTTP协议是无状态的,Session不能依据HTTP连接来判断是否为同一客户,因此服务器向客户端浏览器发送一个名为JSESSIONID的Cookie,它的值为该Session的id(也就是HttpSession.getId()的返回值)。Session依据该Cookie来识别是否为同一用户。

  session的属性值是是可序列化的java对象。

        <%
            session.setAttribute("username", "harry");
        %>

  9:page对象

原文地址:https://www.cnblogs.com/harryV/p/3672162.html