一、JSP元素
<% %>Java代码
<%@ %>Directive:共三种<%@ page %> <%@ include %> <%@ taglib %>
<%@ page %>共有13种参数:
<%= %>表达式:表达式中的内容相当于PrintWriter.out.print()中的参数,表达式中的内容不能返回void。
<%! %>变量或方法声明,与在<% %>声明不同之处在于前者声明的变量为JSP_Servlet的instance variable,而后者声明的变量为_jspServivr()方法中的local varible。
<@-- --@>jsp注释:当想要注释掉JSP元素时,需要用到。<!-- --!>html注释:无法注释掉jsp元素。
<jsp:>jsp standard action
<x:xxx>自定义Jsp action
${} Expression Language
<@-->声明instance variable<--@>
<%! int count = 0;%>
<@-->声明JSP_Servlet中的方法<--@>
<%! int counter(){return ++count;}>
<@-->调用方法<--@>
<%= counter()%>
二、JSP页面本质上是一个Servlet
Container将JSP翻译为Servlet class源文件(.java),然后再将其编译为Servlet class(.class),之后的过程与Servlet相同。
在翻译JSP之前,Container先查看<%@%>中的信息获得翻译所需信息,比如<%@page import = "String"%>会在JSP_Servlet的源文件生成import语句。接着生成<%!%>中的instance variable和方法。将<% %>和<%= %>中的全部内容和其他html元素放入_jspService(HttpServletRequest,HttpServletResposne)方法中,并且在该方法中声明和实例化一些隐含Object。隐含Object如下:注意JspWriter与PrintWriter无关,implicit Object在_jspService方法中声明,为local variable,因此JSP页面中只有<%%>和<%=%>可以使用这些Objects.
<% %>和<%= %>中的全部内容都在JSP_Servlet中的_jspService()方法中,因此<% %>中声明的变量为local varible。
当<%%>和<%!%>中声明了相同名字的变量时:
<!--相当于声明了同名的instance和local variable--!>
<%int x = 42%>
<%! int x = 22%>
<@--由于<%=%>在_jspService方法中,直接将x作为参数只能传入local variable--@>
<%=x%>
<!--调用instance x--!>
<%=this.x%>
三、JSP生命周期
JSP_Servlet implements HttpJspPage interface并且继承了一些其他类。
HttpJspPage interface中的jspInit()和jspDestroy()方法可以被override,_jspService()方法不可override。每个请求的线程中均有一个_jspService(HttpServletRequest,HttpServletResposne)。jspInit()、jspDestroy()和_jspService()与一般Servlet中的init(),destroy() service()类似,且分别由init(),destroy() service()调用。
生命周期:用户请求JSP,container翻译JSP为.java文件,然后编译为.class文件,之后加载类,调用jspInit()使之成为Servlet,之后为该请求新建一个线程并调用_jspService()。
注意翻译和编译的过程只在第一次请求该JSP时执行一次。
设置JSP的Servlet initParameter,与一般Servlet类似:注意<servlet-class>换为<jsp-file>
<web-app ...>
<servlet>
<servlet-name>MyTestInit</servlet-name>
<jsp-file>/TestInit.jsp</jsp-file>
<init-param>
<param-name>email</param-name>
<param-value>ikickedbutt@wickedlysmart.com</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>MyTestInit</servlet-name>
<url-pattern>/TestInif.jsp</url-pattern>
</servlet-mapping>
</web-app>
override jspInit()对ServletContext 和ServletConfig进行操作
<%!
public void jspInit() {
//getServletConfig()从HttpServlet中继承而来
ServletConfig sConfig = getServletConfig();
String emailAddr = sConfig.getInitParameter(“email”);
//getServletContext()从HttpServlet中继承而来,此处不可用application对象,因其为_jspService()中的local variable
ServletContext ctx = getServletContext();
ctx.setAttribute(“mail”, emailAddr);
}
%>
JSP Attribute与Servlet Attribute比较
Objects with page scope are accessible only within the page in which they're created. The data is valid only during the processing of the current response; once the response is sent back to the browser, the data is no longer valid. If the request is forwarded to another page or the browser makes another request as a result of a redirect, the data is also lost.
http://www.apekshit.com/t/425/JSP-Page-Scope-Example
PageContext中的常用方法:
getAttribute(String name)方法得到page scope内的attribute;getAttribute(String name, int scope)得到指定scope范围内的attribute,其中int scope由PageContext.APPLICATION_SCOPE,PageContext.SESSION_SCOPE,PageContext.REQUEST_SCOPE;PageContext.PAGE_SCOPE四个static final variable表示。setAttribute()与get类似。
getAttributeNamesInScope(int scope)
findAttribute(String name)用于搜索各scope内的attribute,搜索顺序为page request session application,搜索到后的效果与get方法相同。
getRequest()
getServletConfig()
getServletContext()
getSession()
以上四种方法用于得到相应的implicit Object。