Jsp
Jsp stands for Java Server Pages, is a dynamic page in our JavaWeb in.
Jsp its essence is a Servlet. JSP Servlet can do can do.
Jsp must be running on the server, you can not directly use the browser to open.
Jsp is a dynamic page, html static pages
Dynamic page | Static pages | |
---|---|---|
Operating principle | Parsed by the server, the data is displayed in the browser | Analytical run directly in the browser |
Maintenance costs | Low, you can modify the background data, thereby affecting the data page | Higher, Modified Pages must be covered by the original page |
database | You can connect to the database | Can not connect to the database |
Access speed | Slower | Faster |
Writing Code | Java code can be written |
Precautions
1, jsp page is similar to a page of html. jsp stored directly to the WebContent directory, and the same time to visit the jsp html, and also access the same html
- Our JSP page is actually written when you run Tomcat JSP, not directly displayed, but the JSP page is converted into a Java class, it is actually a Servlet.
- Let us find the directory for the Eclipse is in: The next workspace, right-click the Tomcat service Browse Deployment Location ...
- In the work directory ... work \ Catalina \ localhost \ day07_jsp \ org \ apache \ jsp folder
- Open the file to view the contents inside index_jsp.java: Discovery, the generated class inherits from HttpJspBase class. This is a jsp Servlet program file generation to inherit a base class! And this HttpJspBase class inherits from HttpServlet class.
- So when we visit a xxx.jsp translation of documents into the full name of the file is xxx_jsp.java java file.
-
If the file has not changed and will not be translated and compiled
-
instruction
-
- Three instructions: <% @ page%>, <% @ include%>, | <% @ taglib%>
- html&css&js&jQuery等...
<% int i = 0;%>
<%=i%>
-
-
html:<!-- -->
-
JSP comments | Java annotations | HTML comments | |
---|---|---|---|
JSP page | visible | visible | visible |
Java code | Invisible | visible | visible |
Browser | Invisible | Invisible |
-
<% @ Directive attribute name = attribute value attribute attribute value = 2 ... 2%>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
-
language: the language, the value of java and only java.
-
contentType: consistent role and response.setContentType (), set the browser encoding.
-
pageEncoding: Set coded character set Jsp page.
-
import: guide package
-
isErrorPage: Set the current page is an error page, the default value is "false".
-
true: Set the current page as an error page, you can use the built-in object exception, catch the exception.
-
false: Set the current page is not an error page, not use exception built-in objects, catch the exception.
-
-
<% @Include File = " included file path " %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
-
-
prefix is used to specify the name of the prefix, we use the JSTL by name.
-
uri corresponds to the unique identification database, since a plurality of different library JSTL composition, using the property which library to be imported.
-
-
JSP action tags with different HTML tags, HTML tags parsed by the browser, and JSP action tags require server (Tomcat) to run.
Role: for forwarding operations in the page
<jsp:forward page="target.jsp"></jsp:forward>
Forwarding sub-tab; when forwarding the parameter setting request, () in the target page acquisition request parameter by request.getParameter.
<jsp:forward page="target.jsp"> <jsp:param value="paramValue" name="paramName"/> </jsp:forward>
Note: If the tag does not require the forwarding operation parameter setting request, the inside of the tag the start and end tags, are not allowed to write any content (including spaces)
Role: Dynamic contain, the other page contains the current page.
<jsp:include page="target.jsp"></jsp:include>
org.apache.jasper.runtime.JspRuntimeLibrary.include(request, response, "target.jsp", out, false);
@include instruction | jsp: include tag | |
---|---|---|
Feature | Static included | Dynamic contain |
The basic syntax of the form | <%@ include file=”…”%> | <jsp:include page=”…”/> |
It includes the timing of action occurred | During translation | During request |
Whether to generate java files | Does not generate | Form |
Merger | Copy the code | The combined operating results |
Content contained | The actual file contents | Page output |
Code Violation | possible | impossible |
Compile times | 1 | File included + 1 |
Scope | Applicable contains pure static content (CSS, HTML, JS), or no time-consuming operation. Or a large number of jsp java code |
Java objects, developers JSP container provided for each page you can use them directly without having to explicitly declare
-
-
Definition: representing the page domain objects used to represent the entire JSP page.
-
effect:
-
Page domain object
-
"Big Brother" nine implicit objects, you can call the eight other implicit objects directly.
-
-
-
-
Definition: the server on behalf of the browser sends a request message, the object created by the server, transmitted to the final doGet () and doPost () method in the form of parameters.
Whenever a client requests a JSP page, JSP engine will create a new request object to represent this request. The request object provides a range of methods to obtain HTTP headers, cookies, HTTP methods, and so on.
-
Action (request objects see a Servlet)
-
Get request parameters
-
Get url address parameters
-
Request forwarding
-
Request to save the data field (Data & removable data acquisition)
-
Acquisition request header
-
-
-
-
Definition: The session between the browser and the server on behalf of.
-
effect
-
Session domain objects,
session object for tracking sessions between the various client requests.
-
-
-
Definitions: Servlet context, on behalf of the current web application.
Web container when you start, it was all created for each Web application ServletContext object corresponds to a unique , meaning Servlet context, on behalf of the current Web application.
-
effect
-
Get the context path of the project (with / project name): getContextPath ()
-
Get Local real path virtual path is mapped: getRealPath (String path)
-
-
4. Obtain the Web application initialization parameters: application.getInitParameter ( "ParamName");
- Get Servlet manner: Use this.getServletContext () method to get.
-
Type: Object
-
Effect: this, the current class object.
-
类型:HttpServletResponse
-
Definition: The response message sent on behalf of the server to the browser, the object created by the server, transmitted to the final process doGet () and doPost () as a parameter.
-
effect:
-
Page response data (response thereof), the data including text, Html like.
-
Redirect
-
Header information setting response
-
-
In the embodiment Get Servlet: doGet () or doPost () directly using
-
Type: ServletConfig
-
Definition: represents the current configuration information of the Servlet, each has a unique corresponding Servlet ServletConfig object.
-
effect:
-
Gets Servlet Name: getServletName ()
-
Obtaining global context ServletContext object: getServletContext ()
-
Gets Servlet initialization parameters: getInitParameter (String) / getInitParameterNames ().
-
-
Get in the way Servlet: this.getServletConfig ()
-
Type: JspWriter
-
Definition: represents the output stream of the current page.
-
Action: Similar to PrintWriter Servlet functionality in the response data to the page, the data may be in response to a page, page segment, string and the like.
-
Get in the way Servlet: None
exception
-
Type: Throwable
-
Definition: represents the exception object of the current page.
-
Role: capture processing exception information page.
-
Nine built-in objects, all we can in the code [script] or [expression] script object directly.
Domain object program, is mainly responsible for data exchange between different web resources, (such as: data exchange between the servlet and jsp).
Within each domain object maintains a Map <String, Object>, a common approach to domain objects.
-
Field to set the properties: void setAttribute (String key, Object value);
-
Gets the specified property from the domain: Object getAttribute (String key);
-
Remove the domain specified attribute: void removeAttribute (String key);
-
-
Each page has its own unique a pageContext object.
-
Note servlet is not the object.
-
-
request: the shared data request is currently active.
-
The current request: forward, direct access to a page for the current request.
-
Not in the current request: redirection, open the page and then click on a hyperlink is not currently requested page.
-
-
session: a session shared data is valid range.
-
Current Session: Current browsers do not shut down & not replace is the current browser session.
-
Only care about whether the browser is closed, do not care about restarting the server shuts down.
-
Different browsers do not share the session.
-
-
application: share data efficiently in a single server process running.
-
Scope | Start Time | End Time | |
---|---|---|---|
pageContext | The current JSP page | Page load | Leaving the page |
request | The same request | Receipt of the request | response |
session | The same session | Start Session | End Session |
application | Current Web Application | Web Application Load |