`
jianchen
  • 浏览: 334791 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

内置对象

阅读更多

                                                            jsp的内置对象使用探究                     
内置对象是不需要声明(其实已经处理过被声明了,只是你自己编写程序是不用特地声明而已),直接可以在JSP中使用的对象,JSP有以下几种内置对象:
jsp九种内置对象:request, reponse, out, session, application, config, pagecontext, page, exception
观察自己所写的jsp文件被servlet容器器编译后生成的servlet源文件。发现所谓的jsp内置的对象,其实只是被预先定义好了而已,美其名曰内置对象。你才可以直接使用。

package org.apache.jsp;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.jsp.*;
import java.util.*;
import com.webstore.user.*;

public final class Login_jsp extends org.apache.jasper.runtime.HttpJspBase
    implements org.apache.jasper.runtime.JspSourceDependent {

  private static final JspFactory _jspxFactory = JspFactory.getDefaultFactory();

  private static java.util.List _jspx_dependants;

  private javax.el.ExpressionFactory _el_expressionfactory;
  private org.apache.AnnotationProcessor _jsp_annotationprocessor;

  public Object getDependants() {
    return _jspx_dependants;
  }

  public void _jspInit() {
    _el_expressionfactory = _jspxFactory.getJspApplicationContext(getServletConfig().getServletContext()).getExpressionFactory();
    _jsp_annotationprocessor = (org.apache.AnnotationProcessor) getServletConfig().getServletContext().getAttribute(org.apache.AnnotationProcessor.class.getName());
  }

  public void _jspDestroy() {
  }
  public void _jspService(HttpServletRequest request, HttpServletResponse response)
        throws java.io.IOException, ServletException {
                                         //编译成servlet后添加了很多代码,加进去了相应的内置对象。


    PageContext pageContext = null;      //以下就是在你用之前就已事先定义好的那些对象,才导致你可以在jsp里可以直接使用。   
    HttpSession session = null;          //而request和response对象是由服务器生成的。他将这两个对象传进来,然后回调          
    ServletContext application = null;   // _jspService()方法。
    ServletConfig config = null;
    JspWriter out = null;
    Object page = this;
    JspWriter _jspx_out = null;
    PageContext _jspx_page_context = null;
try {
      response.setContentType("text/html;charset=GBK");
      pageContext = _jspxFactory.getPageContext(this, request, response,
                              null, true, 8192, true);
      _jspx_page_context = pageContext;
      application = pageContext.getServletContext();
      config = pageContext.getServletConfig();
      session = pageContext.getSession();
      out = pageContext.getOut();
      _jspx_out = out;

      out.write("\r\n");
      out.write("\r\n");
      out.write("\r\n");
      out.write("\r\n");
      out.write("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">\r\n");
      out.write("<html>\r\n");
      out.write("\t<head>\r\n");
      out.write("\r\n");
      out.write("\r\n");
      out.write("\t\t<title>自服务系统</title>\r\n");
      out.write("\r\n");
      out.write("\t\t<meta http-equiv=\"pragma\" content=\"no-cache\">\r\n");
      out.write("\t\t<meta http-equiv=\"cache-control\" content=\"no-cache\">\r\n");
      out.write("\t\t<meta http-equiv=\"expires\" content=\"0\">\r\n");
      out.write("\t\t<meta http-equiv=\"keywords\" content=\"keyword1,keyword2,keyword3\">\r\n");
      out.write("\t\t<meta http-equiv=\"description\" content=\"This is my page\">\r\n");
      out.write("\t\t<!--\r\n");
      out.write("\t<link rel=\"stylesheet\" type=\"text/css\" href=\"styles.css\">\r\n");
      out.write("\t-->\r\n");
      out.write("\r\n");
      out.write("\t</head>\r\n");
      out.write("\r\n");
      out.write("\t<body>\r\n");
      out.write("\t\t<center>\r\n");
      out.write("\t\t\t欢迎您:\r\n");
      out.write("\t\t\t");
      out.print(username);
      out.write("\r\n");
      out.write("\t\t\t<br>\r\n");
      out.write("\t\t\t<a href=\"UserModify.jsp\">修改我的信息</a>\r\n");
      out.write("\t\t\t<br>\r\n");
      out.write("\t\t\t\r\n");
      out.write("\t\t\t<br>\r\n");
      out.write("\t\t\t<a href=\"MyOrders.jsp\">浏览以往订单</a>\r\n");
      out.write("\t\t</center>\r\n");
      out.write("\t</body>\r\n");
      out.write("</html>\r\n");
    } catch (Throwable t) {
      if (!(t instanceof SkipPageException)){
        out = _jspx_out;
        if (out != null && out.getBufferSize() != 0)
          try { out.clearBuffer(); } catch (java.io.IOException e) {}
        if (_jspx_page_context != null) _jspx_page_context.handlePageException(t);
      }
    } finally {
      _jspxFactory.releasePageContext(_jspx_page_context);
    }
  }
}
 


分析:HttpJspBase继承了javax.servlet.http.HttpServlet

config对象的使用
作用范围就当前页面,被包含到别的页面无效.jsp的config对象作用不大,在servlet中作用比较大.

<%@ page language="java" contentType="text/html; charset=utf-8"
  pageEncoding="utf-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>meg.jsp</title>
  </head>
  <body>
    <table align="center" border="0" cellPadding="0" cellSpacing="0"
      width="730">
      <tr>
        <td>
          <div align="center">
            <br>
            客户服务信箱:
            <%=config.getInitParameter("email")%>
            客户服务热线:
            <%=config.getInitParameter("phone")%>
            <br>
            客户服务 QQ:
            <%=config.getInitParameter("qq")%>
            <br>
          </div>
        </td>
      </tr>
    </table>
  </body>
</html>


在web.xml的</web-app>前插入
<!-- config -->
    <servlet>
     <servlet-name>admin</servlet-name>
     <jsp-file>/config.jsp</jsp-file>
     <init-param>
        <param-name>email</param-name>
        <param-value>service@abc.com</param-value>
     </init-param>
     <init-param>
        <param-name>phone</param-name>
        <param-value>0411-12345678</param-value>
     </init-param>
     <init-param>
        <param-name>qq</param-name>
        <param-value>88888中文呢</param-value>
     </init-param>
    </servlet>
    <servlet-mapping>
     <servlet-name>admin</servlet-name>
     <url-pattern>/config.jsp</url-pattern>
    </servlet-mapping>
<!-- end config -->
 
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics