Web Edition 06-Jsp based on knowledge

Jsp

 Jsp stands for Java Server Pages, is a dynamic page in our JavaWeb in.

Jsp data can be presented in a manner HTML page can be embedded in an HTML Java code.

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 technical standard Web pages, mainly syntax consists of: Instruction, html template elements, script fragment (small script), expressions, statements, comments, the suffix is ​​* .jsp.

Jsp responsible for data acquisition and display

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 Unable to write java code

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

2, jsp default encoding set is iso-8859-1, jsp modify default encoding is UTF-8

JSP operating principle

  • 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.
  • In the conf directory of tomcat in web.xm L configured the mapping information of JSP

Later in the same jsp file access

  • If the file has not changed and will not be translated and compiled

  • If the file is changed, it will translate and compile

Jsp basic grammar

instruction

  • Syntax: <% @%>

  • Three instructions: <% @ page%>, <% @ include%>, | <% @ taglib%>

Template elements

  • html&css&js&jQuery等...

Code script fragment

  • Format: <%>

  • Action: in the _jspService () method, the writing java code.

<% int i = 0;%>

expression

  • Format: <% =%>

  • Effect: the data to the page, and Out.print () the same function.

 <%=i%>

statement

  • Format: <!%%>

  • Role: In this post-translational class helloworld_jsp Servlet class, writing java code.

Note

  • java: single-line comments: //, multi-line comments: / ** /

  • html:<!-- -->

  • jsp:<%-- --%> 

  JSP comments Java annotations HTML comments
JSP page visible visible visible
Java code Invisible visible visible
Browser Invisible Invisible visible

Jsp common commands

Syntax

  • <% @ Directive attribute name = attribute value attribute attribute value = 2 ... 2%>

page directive

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>

Attributes

  • 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.

  • errorPage: Set the current jump when the target page page errors. Error need only be captured in _jspService () in.

include instruction: Static included

<% @Include File = " included file path "  %>     
  • Role: The object file that contains the current file.

  • Features: included files will not be translated & compiled. (Includes first, then translated)

taglib directive

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 
  • Attributes

    • 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.

  • Role: the introduction of tag libraries.

Jsp action tags

JSP action tags with different HTML tags, HTML tags parsed by the browser, and JSP action tags require server (Tomcat) to run.

Forward action tags

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)

Dynamic contain action tags

Role: Dynamic contain, the other page contains the current page.

<jsp:include page="target.jsp"></jsp:include>

Features: included files will be translated simultaneously & compiled. (Translated first, and then included)

  • Essence principle: when dynamic comprising, Tomcat Servlet add the following code will be generated:

org.apache.jasper.runtime.JspRuntimeLibrary.include(request, response, "target.jsp", out, false);

The difference between static and dynamic include contained

  @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 It includes the need to pass parameters. Java code contains a lot of computation, the operation takes a long time.

Jsp nine implicit objects

Java objects, developers JSP container provided for each page you can use them directly without having to explicitly declare

pageContext

  • Type: PageContext

  • Definition: representing the page domain objects used to represent the entire JSP page.

  • effect:

    1. Page domain object

    2. "Big Brother" nine implicit objects, you can call the eight other implicit objects directly.

  • Get in the Servlet: None.

  • Type: HttpServletRequest

  • 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)

    1. Get request parameters

    2. Get url address parameters

    3. Request forwarding

    4. Request to save the data field (Data & removable data acquisition)

    5. Acquisition request header

  • In the embodiment Get Servlet: doGet () or doPost () used directly.

session

  • Type: HttpSession

  • 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.

application

  • In the embodiment Get Servlet: request.getSession ();

  • Type: ServletContext

  • 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

    1. Get the context path of the project (with / project name): getContextPath ()

    2. Get Local real path virtual path is mapped: getRealPath (String path)

    3. Obtaining global initialization parameter WEB application (basic need)

      4. Obtain the Web application initialization parameters: application.getInitParameter ( "ParamName");

  • Get Servlet manner: Use this.getServletContext () method to get.

page

  • Type: Object

  • Effect: this, the current class object.

response

  • 类型: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:

    1. Page response data (response thereof), the data including text, Html like.

    2. Redirect

    3. Header information setting response

  • In the embodiment Get Servlet: doGet () or doPost () directly using

config

  • Type: ServletConfig

  • Definition: represents the current configuration information of the Servlet, each has a unique corresponding Servlet ServletConfig object.

  • effect:

    1. Gets Servlet Name: getServletName ()

    2. Obtaining global context ServletContext object: getServletContext ()

    3. Gets Servlet initialization parameters: getInitParameter (String) / getInitParameterNames ().

  • Get in the way Servlet: this.getServletConfig ()

out

  • 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.

  • Get in the way Servlet: new Throwable ()

Nine built-in objects, all we can in the code [script] or [expression] script object directly.

Jsp four domain object

Domain object program, is mainly responsible for data exchange between different web resources, (such as: data exchange between the servlet and jsp).

Domain object analysis

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);

Domain object validity

  • pageContext: the current page to share data effectively, fail to leave the current page.

    • 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.

    • The server closes the destruction

Domain Objects 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 Web Application uninstall

Guess you like

Origin www.cnblogs.com/Open-ing/p/12094239.html