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 -->
分享到:
相关推荐
JSP内置对象实验报告
JSP内置对象 JSP内置对象 JSP内置对象
JavaScript内置对象相关介绍及用法ppt JavaScript内置对象相关介绍及用法
帮助了解JSP九大内置对象,对jsp就打内置对象知识提高
JSP内置对象(9个常用的内置对象)JSP内置对象(9个常用的内置对象)
全面介绍了JSP的几种内置对象。熟练使用这些内置对象是开发JSP应用程序的基本要求,尤其是对于request、session和application对象更是必须要掌握的。 会使用对象所具有的方法,能熟练使用每一种对象的几种常用方法。...
asp.net内置对象 asp.net内置对象 asp.net内置对象
jsp九大内置对象学习
JSP内置对象大全及实例解析,前面部分讲的是JSP的9大内置对象的函数和方法,后面是各个对象的所有方法的实例。
ASP.NET的6个内置对象说明及简单的例子(PPT)
所有jsp内置对象的方法用法,方便开发人员查找
Jsp内置对象session总结:详细介绍,Session机制,原理,生命周期h和Session的主要方法等。
jsp内置对象 有关jsp的一些内置的对象的总结
jsp9大内置对象
JSP的内置对象及用法,jsp的内置对象有哪些及如何使用!
JSP实验内置对象、Cookie及JavaBean实验[参考].pdf
JSP内置对象的归纳与总结,内置对象概述 out对象 request对象 response对象 session对象 application对象 pageContext对象 exception对象
javascript的面向对象和内置对象讲解,包括了一些常用的方法和对象总结
javascript本地对象_内置对象和宿主对象