Java JSP(一) 基本原理和九个隐含对象
Hello.java:
package roadArchitectWeb.Test;
public class Hello {
private String name;
public Hello(String name) {
super();
this.name = name;
}
@Override
public String toString() {
return "Hello [name=" + name + "]";
}
}
test.jsp:
<%@page import="roadArchitectWeb.Test.Hello"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
Insert title here
<%
Hello hello = new Hello("zhangjie");
out.println(hello);
%>
然后再浏览器中运行的结果是:
Hello [name=zhangjie]
找到test.jsp对应的class文件,它在eclipse安装目录下,我的机器中是下面的目录:
D:\java\eclipseee\eclipse-jee-mars-R-win32-x86_64\eclipse\codes\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\work\Catalina\localhost\roadArchitectWeb\org\apache\jsp
然后发现两个文件:
test_jsp.class
test_jsp.java
打开test_jsp.java,如下:
/*
* Generated by the Jasper component of Apache Tomcat
* Version: Apache Tomcat/7.0.64
* Generated at: 2016-05-16 07:09:05 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.
*/
package org.apache.jsp;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.jsp.*;
import roadArchitectWeb.Test.Hello;
public final class test_jsp extends org.apache.jasper.runtime.HttpJspBase
implements org.apache.jasper.runtime.JspSourceDependent {
private static final javax.servlet.jsp.JspFactory _jspxFactory =
javax.servlet.jsp.JspFactory.getDefaultFactory();
private static java.util.Map _jspx_dependants;
private javax.el.ExpressionFactory _el_expressionfactory;
private org.apache.tomcat.InstanceManager _jsp_instancemanager;
public java.util.Map getDependants() {
return _jspx_dependants;
}
public void _jspInit() {
_el_expressionfactory = _jspxFactory.getJspApplicationContext(getServletConfig().getServletContext()).getExpressionFactory();
_jsp_instancemanager = org.apache.jasper.runtime.InstanceManagerFactory.getInstanceManager(getServletConfig());
}
public void _jspDestroy() {
}
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;
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 = out;
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("Insert title here \r\n");
out.write("\r\n");
out.write("\r\n");
Hello hello = new Hello("zhangjie");
out.println(hello);
out.write("\r\n");
out.write("\r\n");
out.write("");
} catch (java.lang.Throwable t) {
if (!(t instanceof javax.servlet.jsp.SkipPageException)){
out = _jspx_out;
if (out != null && out.getBufferSize() != 0)
try {
if (response.isCommitted()) {
out.flush();
} else {
out.clearBuffer();
}
} catch (java.io.IOException e) {}
if (_jspx_page_context != null) _jspx_page_context.handlePageException(t);
else throw new ServletException(t);
}
} finally {
_jspxFactory.releasePageContext(_jspx_page_context);
}
}
}
我们发现:
A:init,destroy和service方法都有,如上面代码所示,test_jsp这个方法继承自HttpJspBase;而如下代码说明,HttpJspBase是继承自HttpServlet:
public abstract class HttpJspBase extends HttpServlet implements HttpJspPage {
B:而且jsp的呈现都在service中的out.write中。如上面的out.write
1)pageContext
2)request
3)session
4)application
5)out
6)response
7)config
8)page
9)exception
注意到下面test_jsp.java中的代码:除了exception外,标明了每个隐含对象的类型;
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;
下面的代码说明了,各个隐含对象的出处:
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 = out;
下面就来使用下经常用到的隐含对象:
不常用的:response,config,exception,page; 剩下的经常使用的(out的用法已经展示了,下面是其他四个的用法):
A)pageContext
B)request
C)session
D)application
其中B,C,D都是由pageContext的get方式得到;上面四个对象的作用于由小到大。pageContext的作用域就是当前页面,request的作用域是一次请求应答,session的作用域是一次会话(一次会话指的是打开浏览器到关闭浏览器的整个过程),application指的是整个web应用,指整个应用从开启到关闭的整个过程,所以它的作用域是最大的。
以常见的方法为例来分析几个对象的作用域:经过setAttribute和getAttrbute为例,测试后发现上面的作用域是完全正确的。
1)Jsp模板元素,指的是静态HTML,它才是整个jsp的框架
2)Jsp的表达式,形如 <%=%>
<%Hello hello = new Hello("zhangjie");%>
<%out.println(hello);%>
<%=hello%>
后两个是完全一样的。
3)Jsp脚本片段 ,形如<%%>
4)Jsp声明,由于整个jsp实际上是在servlet的service方法中执行的,所以示无法在其中声明一个方法的,但可以通过形如<%! %>的方式声明一个函数,不过很少用
5)Jsp注释格式:
<%-- --%>
html注释格式:
具体区别如下:
1)请求的转发: 地址栏是初次发出请求的地址.
请求的重定向: 地址栏不再是初次发出的请求地址. 地址栏为最后响应的那个地址
2)请求的转发: 在最终的 Servlet 中, request 对象和中转的那个 request 是同一个对象.
请求的重定向: 在最终的 Servlet 中, request 对象和中转的那个 request 不是同一个对象.
3)请求的转发: 只能转发给当前 WEB 应用的的资源.
请求的重定向: 可以重定向到任何资源.
4)请求的转发: / 代表的是当前 WEB 应用的根目录
请求的重定向: / 代表的是当前 WEB 站点的根目录.