JSP
1. JSP
JSP(javaServerPage)would be supplementary for Servlet, rather than be substituted. Because the JSP sloves tow problems of Servlet.Such as,first,the HTML tags in Servlet all must be enclosed by Java String,thus,make send HTTP Response become a process complicatedly;Second,all text&HTML tags must hard-compiling,so even if revise merely for Presentation Layer,we must re-compile the application.
2. JSP Concept
As we know that, JSP is essential for Servlet,cause it is a Servlet.But using JSP is easier than Servlet,they are reasons that JSP don't compile and its Extension Name is jsp,could edit it use any editor.
JSP Page also running in JSP Container. Usually, Servlet Container also is a JSP Container.eg:Tomcat is a Servlet/JSP Container.
JSP's comment(注释),implicit object(隐式对象)&three grammar elements.They are directive(指令)、scripting element(脚本元素)、action(动作).
3.JSP Request Process
Servlet/JSP Container must do two things at firstly client requires a JSP Page:
(1)javax.servlet.jsp.JspPage Interface or its sub-interface javax.servelt.jsp.HttpjspPage to transform JSP into a JSP page implementation class.It would bo best,we could know JspPage is a sub-interface of javax.servlet.Servlet,hence,JspPage can every JSP Page become a Servlet.The Servlet Class Name is decided on Servlet/JSP Container.
(2)if JSP is succeedly transformed into Servlet Class,then Servlet/JSP Container would compile Servlet Class.And Container load&instace Java byte code,excute it as if treat a common Servlet to operate in a life cycle.
At firstly do call JSP takes time is longer than late,so it needs to transform and compile.For sloving this problem,can take any ways as follows:
①Configuring application,so that calling all JSP page at program beginning rather than at initialing request.
②Previously compile JSP page,and configure them by Servlet style.
4.JSP API
JSP has a API,four packages in the API:
①javax.servlet.jsp. ---Including core class and interface,Servlet/JSP Container uses them transform JSP page into Servlet.JspPage&HttpJspPage interfaces are the package's importance members.All JSP both implement JspPage or HttpJspPage.In HTTP enviroment/circumstance,it would be best that we could choose HttpJspPage.
②javax.servlet.jsp.tagext. ---Including those types that develope self-define tags.
③javax.el. ---Provide API for Unified Expression Language.
④javax.servlet.jsp.el. ---Provide Servlet/JSP Container must class supported,so that to support JSP's Expression Language.
Actually, we hardly directly use JSP API except javax.servlet.jsp.tagext,wemore absorbedly sink into Servlet API.
5.Coding Java in JSP
Two things need to be careful.First,Java code must be neclosed with <% and %>,therefore,the java code can be putted anywhere in JSP;Second,we can make use of "import" attribute of page instructor to import any java class we want.
<%……%> is named scriptlet.
6.JSP Grammar
①Comment
JSP Comment starts with <%--,and end with --%>,for example:
<%-- retrieve products to display --%>
②Implicit Objects
We could use implicit objects as if a Reference Variables the Specific Calsses.For example,Implict Object request represents HttpServletRequest that is send service() of Servlet by Servlet/JSP Container.This line code gets userName of form uses HttpServletRequest Object.
<%
<span style="white-space:pre"> </span>//get form data use getParameter function;
String userName = request.getParameter("userName");
//get setAttribute() adds value,must use forward(url) of RequestDispatcher to redirect.
String [] data = request.getAttribute("keyValue");
%>
JSP contains only nine implicit objects described in the following table:
内置对象 | 类型 | 作用域 |
request | javax.servlet.http.HttpServletRequest | request |
response | javax.servlet.http.HttpServletResponse | response |
pageContext | javax.servlet.jsp.PageContext | page |
session | javax.servlet.http.HtpSession | session |
application | javax.servlet.jsp.ServletContext | application |
out | javax.servlet.jsp.JspWriter | page |
config | javax.servlet.ServletConfig | page |
page | java.lang.Object | page |
exception | java.lang.Throwable | page |
More details,please refrence three urls:
Roy's Blog_ http://www.blogjava.net/RoyPayne/archive/2012/01/05/367930.html
菜菜鸟_黑马的专栏:http://blog.csdn.net/lenhan12345/article/details/1566594
kelin1314:http://www.cnblogs.com/kelin1314/archive/2011/03/03/1969578.html
③Directive
Directive is first JSP grammar element,it is a order that directs how JSP Convertor should transform a JSP into a Servlet. JSP2.2 defines some directives,but the more important elements are page and include. It has other directives,such as taglib,tag,attribute and variable.
The examples for page and include:
page:
<%@ page attribute="value1" attribute2="value2"... %>
<%@ page import="java.util.ArrayList,java.util.Date"%>
<%@ page session="false" buffer="16kb"%>
include:
<%@ include file="url"%>
<%@ include file = "copyright.jspf"%>
Extension Name si jspf,it is JSP fragment,that also named JSP segment,we continue to use jspf is in accordance with JSP fragment.
Script Element
④Scriptlet
Scriptlet is a code segment,it makes Java code combine one JSP page. Scriptlet starts with <%, and ends with %>.
⑤Expression
Expression's result will be fill into print() of implicit object out.Express starts with <%=,ends with %>.
eg:
Tody is
<%=java.util.Calendar.getInstance().getTime()%>
⑥Declaration
Declaration can declarate variavbles and methods used in JSP.Declaration starts with <%! and ends with %>.
eg:
<%!
//Declarating a getTodayDate() method
public String getTodayDate(){
return new java.util.Date();
}
%>
The declaration can be put anywhere in JSP, and can have many declarations in one JSP.
⑥Closing Script Elements
As Expression Language develops in JSP 2.0,we advice to use EL to access Server Objects to instead java codes in JSP. Therefore,we can close it by define a scripting-invalid in deployment descriptor <jsp-property-group>.
eg:
<jsp-property-group>
<url-pattern>*.jsp</url-pattern>
<scripting-invalid>true</scripting-invalid>
</jsp-property-group>
Action
Action is third JSP grammar.
To be contiune....................