面向对象先生

导航

统计

9大jsp隐式对象 与servlet之间的关系

  JSP隐私对象
    |
       --------------------------------------------------------------------------
       |                   |                          |                     |
       输入输出对象    作用域通信对象     Servlet对象         错误对象
       |                   |                           |                     |
         out                  session               page             exception
           |                   |                          |                     |               

  request           application           config      
       |                   |
        response        pageContext

实验环境搭配:
1.Tomcat\webapps\建立Web站点目录Test
2.Test\创建WEB-INF文件夹,即可以搭建起Web站点服务
3.tomcat\work\Catalina\localhost\Test\org\apache\jsp中找到对应的jsp生成的.java文件

<!-----------------------------1.out对象-------------------------------->

===JSP页面中的使用:===
...
<body>
   <%
   out.print("使用out隐式对象输入页面内容");
   %>
</body>
...

===Servlet中生成的代码:===

...
JspFactory _jspxFactory = null;
PageContext pageContext = null;
JspWriter out = null;

pageContext = _jspxFactory.getPageContext(this, request, response,
                       null, true, 8192, true);
   out = pageContext.getOut();
   out.print("使用out隐式对象输入页面内容");              
...

**************************index.jsp文件*************************

<%@page contentType="text/html;charset=gbk"%>
<html>
<head>
   <title>Test</title>
</head>
<body>
   <%
   out.print("使用out隐式对象输入页面内容");
   %>
</body>
</html>

 

********************生成的index_jsp.java文件********************

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.jsp.*;

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

   private static java.util.Vector _jspx_dependants;

   public java.util.List getDependants() {
     return _jspx_dependants;
   }

   public void _jspService(HttpServletRequest request, HttpServletResponse response)
         throws java.io.IOException, ServletException {

     JspFactory _jspxFactory = null;
   
     // _jspx_page_context = pageContext;
     PageContext pageContext = null;
     PageContext _jspx_page_context = null;
     HttpSession session = null;
     ServletContext application = null;
   
     Object page = this;
     ServletConfig config = null;
   
     // _jspx_out = out;
     JspWriter out = null; 
     JspWriter _jspx_out = null;
   
     try {
       _jspxFactory = JspFactory.getDefaultFactory();
     
       response.setContentType("text/html;charset=gbk");
     

       //创建pageContext对象     
       pageContext = _jspxFactory.getPageContext(this, request, response,
         null, true, 8192, true);
       _jspx_page_context = pageContext;
     
       //创建application对象
       application = pageContext.getServletContext();
     
       //创建session对象
       session = pageContext.getSession();
     
     
       //创建config对象
       config = pageContext.getServletConfig();
     
     
       //创建out对象
       out = pageContext.getOut();
       _jspx_out = out;

       out.write("\r\n");
       out.write("<html>\r\n");
       out.write("\t<head>\r\n");
       out.write("\t\t<title>Test</title>\r\n");
       out.write("\t</head>\r\n");
       out.write("\t<body>\r\n");
       out.write("\r\n");
       out.write("\t\t");

   out.print("使用out隐式对象输入页面内容");
  
       out.write("\r\n");
       out.write("\t</body>\r\n");
       out.write("</html>");
     } catch (Throwable t) {
       if (!(t instanceof SkipPageException)){
         out = _jspx_out;
         if (out != null && out.getBufferSize() != 0)
           out.clearBuffer();
         if (_jspx_page_context != null) _jspx_page_context.handlePageException(t);
       }
     } finally {
       if (_jspxFactory != null) _jspxFactory.releasePageContext(_jspx_page_context);
     }
   }
}


<!-----------------------------2.request对象-------------------------------->

===JSP页面中的使用:===
...
<body>
   <%
   request.setAttribute("name","value");
   request.getAttribute("name");
   注意:getAttribute()只能取到setAttribute()存入的Value
  
   String temp = request.getParameter("name");
   String[] temp = request.getParameterValues("name");
   %>
</body>
...

===Servlet中生成的代码:===

...
public void _jspService(HttpServletRequest request, HttpServletResponse response)
         throws java.io.IOException, ServletException {
           request.setAttribute("name","value");
         }             
...

<!-----------------------------3.response对象-------------------------------->

===JSP页面中的使用:===
...
<body>
   <%
   response.setContentType("text/html;charset=gbk");
   response.sendRedirect("another.jsp");
   %>
</body>
...

===Servlet中生成的代码:===
...
public void _jspService(HttpServletRequest request, HttpServletResponse response)
         throws java.io.IOException, ServletException {
             response.setContentType("text/html;charset=gbk");
         response.sendRedirect("second.jsp");
         }             
...

<!-----------------------------4.session对象-------------------------------->

===JSP页面中的使用:===
...
<body>
   <%
   session.setAttribute("name","value");
   String temp = (String)session.getAttribute("name");
   %>
</body>
...

===Servlet中生成的代码:===

...
JspFactory _jspxFactory = null;
PageContext pageContext = null;
HttpSession session = null;

pageContext = _jspxFactory.getPageContext(this, request, response,
                       null, true, 8192, true);
   session = pageContext.getSession();
   session.setAttribute("name","value");
   String temp = (String)session.getAttribute("name");              
...

<!-----------------------------5.application对象-------------------------------->
===JSP页面中的使用:===
...
<body>
   <%
   application.setAttribute("name","value");
   String tempApp = (String)application.getAttribute("name");
   %>
</body>
...

===Servlet中生成的代码:===

...
JspFactory _jspxFactory = null;
PageContext pageContext = null;
ServletContext application = null;

pageContext = _jspxFactory.getPageContext(this, request, response,
                       null, true, 8192, true);
   application = pageContext.getServletContext();
application.setAttribute("name","value");
String tempApp = (String)application.getAttribute("name");              
...

<!-----------------------------6.pageContext对象-------------------------------->

===JSP页面中的使用:===
...
<body>
   <%
   pageContext.setAttribute("name","value");
   String tempPage = (String)pageContext.getAttribute("name");
   %>
</body>
...

===Servlet中生成的代码:===

...
JspFactory _jspxFactory = null;
PageContext pageContext = null;

pageContext = _jspxFactory.getPageContext(this, request, response,
                       null, true, 8192, true);
                      
pageContext.setAttribute("name","value");
String tempPage = (String)pageContext.getAttribute("name");             
...

<!-----------------------------7.page对象-------------------------------->
===JSP页面中的使用:===
...
<body>
   <%=((javax.servlet.jsp.HttpJspPage)page).getServletInfo()%>
</body>
...

===Servlet中生成的代码:===

...
page对象表示页面本身,她是java.lang.Objec类的一个实例.为jsp页面创建Servlet类定义变量和方法.
这些变量和方法包含页面的信息.使用page对象可以访问所有这些变量和方法.
page对象很少使用?一般使用page指令即可?(为什么?)            
...

<!-----------------------------8.config对象-------------------------------->
===JSP页面中的使用:===
...
<%
   String tempConfig = (String)config.getInitParameter("test");
   out.println(tempConfig);
%>
...

===Servlet中生成的代码:===
... 
JspFactory _jspxFactory = null;
PageContext pageContext = null;

pageContext = _jspxFactory.getPageContext(this, request, response,
                       null, true, 8192, true);
config = pageContext.getServletConfig();                                
String tempConfig = (String)config.getInitParameter("test");
out.println(tempConfig);
...

<!-----------------------------9.exception对象-------------------------------->

<--------------------------exception.jsp----------------------------------->
<%@ page contentType="text/html; charset=gb2312" errorPage="/error.jsp" %>
<html>
<head>
<title>出错演示</title>
</head>
<body>
<%
String t="test";
int i=Integer.parseInt(t);
%>
</body>
</html>

<--------------------------error.jsp----------------------------------->
<%@ page contentType="text/html; charset=gb2312" language="java" isErrorPage="true" %>
<html>
<head>
<title>出错了!</title>
</head>
<body>
出错了!
<br>
发生了以下的错误:
<br>
<%=exception.getMessage()%>
<%
exception.printStackTrace(out);
%>
</body>
</html>

 

posted on 2009-12-07 17:55  面向对象先生  阅读(498)  评论(0)    收藏  举报

点击右上角即可分享
微信分享提示