JSP(Java Server Page) which display the page of server also a servlet in essence. The JSP could write java code in a html page.
JSP = Java + HTML
In the MVC:
Servlet: controller
Jsp: viwer
We can see the jsp file in work directory in tomcat's root path. This directory is used to restore the jsp java file an jsp class file. From the source code, we can see from
the jsp's java file that class extends from org.apache.jasper.runtime.HttpJspBase. The HttpJspBase also extends the HttpServlet. So the jsp is a servlet in essence.
How to write java code in jsp?
<%! %> define the member variables usually, we define method in it. If define the member field in it, it may lead to thread security problems.
<% %> define the local variables the variables are in local position, at the _jspservice()
<%= %> output variables call the out.print("content")
The comment of jsp:
<%-- comment --%>
We recommend the use of this style in jsp, it will improve the effeciency of jsp.
The instruction of jsp:
page taglib include
The format of instruction:
<%@ instruction attribute1="value1" attribute2="value2" ... %>
Page:
import: import the jar of class jsp needs. <%@page import="java.util.Date" %>
session: has the session object or not, the default value is true.
buffer: when write to the html page, the out stream is JspWriter, this attribute is to set the capacity of the buffer, the default value is 8kb.
errorPage: set the page location when there is a error in the jsp, we can define the error page in the web.xml:
<error-page>
<error-code>404</error-code>
<location>/404.jsp</location>
</error-page>
isErrorPage: has the exception inner-object in the jsp, the default value is false.
contentType: has the same funciont as the response.setContentType("text/html;charset=utf-8");
pageEncoding: set the encoding of the server when the server translate the jsp code.
Taglib: import external tag lib, when we use the JSTL.
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
Include: include some resource in the static way.
static: <%@include file="/8.jsp">
dynamic: <jsp:include page="/8.jsp"></jsp:include>
We should use the static way as most as possible.
The action of jsp:
1. <jsp:include page="/8.jsp"> include the resource of 8.jsp in the dynamic way.
2. <jsp:forward page="/8.jsp"> forward to the specific resource and the url location will not change in the browser.
3. <jsp:param value="value" name="name"> forward the parameter.
There are 9 inner objects in jsp:
request/response/config/application/out/exception/session/pageContext/page
The detail of pageContext inner object:
1. It's a domain object, and it can manipulate the data in other three domain object (ServletRequest, HttpSession, ServletContext):
void setAttribute(String name, Object value);
void removeAttribute(String name);
Object getAttribute(String name);
pageContext's scope is limited in the page, only works in the page.
How to handle the data of other three domain object?
void setAttribute(String name, Object value, int scope);
void getAttribute(String name, int scope);
void removeAttribute(String name, int scope);
the value of scope:
1. PageContext.PAGE_SCOPE
2. PageContext.REQUEST_SCOPE
3. PageContext.SESSION_SCOPE
4. PageContext.APPLICATION_SCOPE
the code:
pageContext.setAttribute("name","value",PageContext.APPLICATION_SCOPE);
equals:
application.setAttribute("name","value");
Object findAttribute(String key): find the object from page,request,session,application. Return the minimum range object.
2. Get other domain object:
ServletRequest requst = pageContext.getRequest();
From this feature, if we want to get other domain object in a method, we can :
public void method(){
ServletRequest request = pageContext.getRequest();
HttpSession session = pageContext.getSession();
JspWriter writer = pageContext.getOut();
}
The 4 important domain object:
PageContext, ServletRequest, HttpSession, ServletContext