jsp的内置对象有哪些?(JSP中内置对象及作用)
Java萌新小伙伴们大家好啊~~今天浙江优就业为大家带来了JSP内置对象的知识哟。
JSP有9个内置对象:
– request:封装客户端的请求,其中包含来自GET或POST请求的参数;
– response:封装服务器对客户端的响应;
– pageContext:通过该对象可以获取其他对象;
– session:封装用户会话的对象;
– application:封装服务器运行环境的对象;
– out:输出服务器响应的输出流对象;
– config:Web应用的配置对象;
– page:JSP页面本身(相当于Java程序中的this);
– exception:封装页面抛出异常的对象。
补充:如果用Servlet来生成网页中的动态内容无疑是非常繁琐的工作,另一方面,所有的文本和HTML标签都是硬编码,即使做出微小的修改,都需要进行重新编译。JSP解决了Servlet的这些问题,它是Servlet很好的补充,可以专门用作为用户呈现视图(View),而Servlet作为控制器(Controller)专门负责处理用户请求并转发或重定向到某个页面。基于Java的Web开发很多都同时使用了Servlet和JSP。JSP页面其实是一个Servlet,能够运行Servlet的服务器(Servlet容器)通常也是JSP容器,可以提供JSP页面的运行环境,Tomcat就是一个Servlet/JSP容器。第一次请求一个JSP页面时,Servlet/JSP容器首先将JSP页面转换成一个JSP页面的实现类,这是一个实现了JspPage接口或其子接口HttpJspPage的Java类。JspPage接口是Servlet的子接口,因此每个JSP页面都是一个Servlet。转换成功后,容器会编译Servlet类,之后容器加载和实例化Java字节码,并执行它通常对Servlet所做的生命周期操作。对同一个JSP页面的后续请求,容器会查看这个JSP页面是否被修改过,如果修改过就会重新转换并重新编译并执行。如果没有则执行内存中已经存在的Servlet实例。我们可以看一段JSP代码对应的Java程序就知道一切了,而且9个内置对象的神秘面纱也会被揭开。
JSP页面:
“>* { font-family:”Arial”; }
Hello, World!
Current time is:
对应的Java代码:
/* * Generated by the Jasper component of Apache Tomcat * Version: Apache Tomcat/7.0.52 * Generated at: 2014-10-13 13:28:38 UTC * Note: The last modified time of this file was set to * the last modified time of the source file after * generation to assist with modification tracking. */packageorg.apache.jsp; importjavax.servlet.*; importjavax.servlet.http.*; importjavax.servlet.jsp.*; publicfinalclassindex_jspextendsorg.apache.jasper.runtime.HttpJspBaseimplementsorg.apache.jasper.runtime.JspSourceDependent{privatestaticfinaljavax.servlet.jsp.JspFactory _jspxFactory = javax.servlet.jsp.JspFactory .getDefaultFactory(); privatestaticjava.util.Map _jspx_dependants; privatejavax.el.ExpressionFactory _el_expressionfactory; privateorg.apache.tomcat.InstanceManager _jsp_instancemanager; publicjava.util.Map getDependants() { return_jspx_dependants; } publicvoid_jspInit() { _el_expressionfactory = _jspxFactory.getJspApplicationContext( getServletConfig().getServletContext()).getExpressionFactory(); _jsp_instancemanager = org.apache.jasper.runtime.InstanceManagerFactory .getInstanceManager(getServletConfig()); } publicvoid_jspDestroy() { } publicvoid_jspService( finaljavax.servlet.http.HttpServletRequest request, finaljavax.servlet.http.HttpServletResponse response) throwsjava.io.IOException, javax.servlet.ServletException { // 内置对象就是在这里定义的finaljavax.servlet.jsp.PageContext pageContext; javax.servlet.http.HttpSession session = null; finaljavax.servlet.ServletContext application; finaljavax.servlet.ServletConfig config; javax.servlet.jsp.JspWriter out = null; finaljava.lang.Object page = this; javax.servlet.jsp.JspWriter _jspx_out = null; javax.servlet.jsp.PageContext _jspx_page_context = null; try{ response.setContentType( “text/html;charset=UTF-8”); 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 =原文seO-6.com out; out.write( ‘\r’); out.write( ‘\n’); String path = request.getContextPath(); String basePath = request.getScheme() + “://”+ request.getServerName() + “:”+ request.getServerPort() + path + “/”; // 以下代码通过输出流将HTML标签输出到浏览器中out.write( “\r\n”); out.write( “\r\n”); out.write( “\r\n”); out.write( “\r\n”); out.write( ” \r\n”); out.write( ” \r\n”); out.write( ” \r\n”); out.write( ” \r\n”); out.write( ” \r\n”); out.write( ” \r\n”); out.write( ” \r\n”); out.write( “
Hello, World!
\r\n”); out.write( “
\r\n”); out.write( “
Current time is: “); out.print( newjava.util.Date().toString()); out.write( “
\r\n”); out.write( ” \r\n”); out.write( “\r\n”); } catch(java.lang.Throwable t) { if(!(t instanceofjavax.servlet.jsp.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); elsethrownewServletException(t); } } finally{ _jspxFactory.releasePageContext(_jspx_page_context); } } }