Introduction to JSP simple function

Source: Internet
Author: User

Jsp

  [1] Introduction
> HTML
-HTML is good at displaying a static Web page, but cannot call Java programs.
> Servlet
-Servlet is good at calling Java programs to interact with the background, but it is not good at displaying a full HTML page.
> We want to create a new technology HTML + Servlet Powerful Alliance
> JSP full name Java Server Pages, as the name implies is running in Java servers in the page, that is, in our Javaweb dynamic page, its essence is a servlet.
> JSP cannot be run out of the server.
> JSP = HTML + Java code
> Normal development in the servlet is responsible for processing requests, JSP is responsible for displaying the page

Note: For the page encoding problem, can be solved by adding-dfile.encoding=utf-8 at the end of the Eclipse.ini file!

[2] Operating principle
The essence of > jsp is a servlet
The > JSP is translated into a. java file by the Tomcat server before it is run, and then the. Java text is compiled into a. class file
While we are accessing the JSP, the translated class is the one that handles the request.

JSP-------Java---------

> We need to go to the mirror server to find the results.
> The Java file actually translated into is a servlet.
1. Inherit the Httpjspbase class, and the class inherits the HttpServlet
2. In the overall web. xml file, a JSP-related configuration was also found
This proves that the JSP is indeed a servlet.

[3] Basic grammar
> The basic syntax for learning JSP is to learn how the server translates a JSP file into a Java file

> Template Elements
is the HTML code in the JSP
The content of the template element will eventually be used as a parameter to the Out.write () and eventually output in the page
How to write a page in an HTML page, how to write it in a JSP

> Script Fragments
<%%>
-You can write Java code directly in a script fragment
-The code in the script fragment is copied unchanged to the specified bit J in the service method of the Java file
-What can be written in the service method and what can be written in the script fragment
-There can be more than one script fragment on a JSP page, but a complete structure is ensured between multiple script fragments

> Expressions
<%=%>
-JSP expressions are used to output an object to a page
-The content in the JSP expression will eventually be output to the page as a parameter of Out.print ()
-Out.print () method can receive what kind of parameter, what can write in the expression
Out.print () can receive arbitrary types of arguments, so any object can be passed in an expression.

> Notes
<%----%>
-The contents of the JSP comments are not translated into the JSP corresponding Java file

-Three types of annotations can be used in JSP
-HTML Comment Java comment JSP comment
<!--Clips--//java notes! <%----%>

-Three types of annotations visible range

JSP Source code Java source files The source of the webpage
HTML annotations Visible Visible Visible
Java annotations Visible Visible Not visible
JSP annotations Visible Not visible Not visible


> statement
<%! %>
-The contents of the JSP declaration are written directly to the JSP corresponding Java class.
-What can be written in a class can be written in a declaration. You can define a property, and you can define a method:
-In general, we do not write in the JSP corresponding class, so the declaration is basically not used, know on the line.

-You can get the value of the attribute in the declaration by this

[4] JSP directive
> Format: <%@ directive Name Property Name 1 = "Property value 1" Property Name 2 = "Property value 2"%>

> Page Directives
-the page instruction mainly tells the JSP engine how to parse the JSP file.
-Properties that may be used
-Import to guide the package
-language specifies which language the JSP file is translated into
Only one value for this property is Java
-contentType Specifies the encoding of the current page
Response.setcontenttype ("Text/html;charset=utf-8");
-to the browser to see
-pageencoding Specifies the encoding of the current page, and the JSP engine parses the JSP file according to the encoding
-to the server to see
-errorpage Specifies the page to which the page was forwarded after the error, and the address of this property does not add the project name.
-Iserrorpage whether the current page is an error page, and if true, you can use the exception object in the page.

-Properties that are not used in basic
-AutoFlush whether the cache is automatically refreshed, the default value is true, it is not generally modified to modify this property
-Buffer cache Size default 8KB generally not modified
-extends specifies the parent class to which the JSP corresponding Java class is based. Not at all.
-Info to set the current JSP information, no use
-Iselignored Whether the El expression is automatically ignored, default is false, not ignored, generally not modified
-Session object can be used directly, the default value is true, not generally modified

> Include directives
-<%@ include file= ""%>
-Static include directives
-Include directives are used primarily to include other pages in the current JSP page
-He will copy the included pages to the specified location on the target page intact.
-it will only translate the target page, not the included page
-such as 1.jsp include 2.jsp
Equivalent to copying 2.jsp into a specified position at 1.jsp
Only translate 1.jsp, not translate 2.jsp

[5] JSP action tag
> JSP action tags will eventually be translated into a set of Java code, executed in the program.

&LT;JSP: Tag Name Property name = "attribute value" ></jsp: Tag name >

<jsp:forward page= "target.jsp" >
<jsp:param value= "paramvalue" name= "paramname"/>
</jsp:forward>
> Jsp:forward tag function is to forward the request!
> If there are no child tags in the label, then the label body can not appear any content
> can use the Jsp:param tag to pass request parameters to the target page.

<jsp:include page= "target.jsp" ></jsp:include>
> jsp:include Dynamic Inclusion
> Dynamic inclusion generates a Java file for all pages
> Dynamic inclusion does not directly copy the included page directly into the target page, but instead uses the following code to include the page in the current page
Org.apache.jasper.runtime.JspRuntimeLibrary.include (Request, Response, "/include/2.jsp", out, false);

[6] implied objects
> There are 9 hidden objects in the JSP, 9 objects that I can use directly in the JSP.
> Because the nine hidden objects have been declared and assigned in the service method, they can be used directly in the JSP.
-PageContext
Type: PageContext
Represents: The context of the current page
Action: You can get to other hidden objects in the page, and it is also a domain object.

-Request
Type: HttpServletRequest
Representative: Request
Function: can get the request information sent by the user, it is also a domain object.

-session
Type: HttpSession
Delegate: Current session
Function: can be used as a domain object to share data.

-Application
Type: ServletContext
Rep: representing the entire Web application
Action: is the largest domain object in the Javaweb.

-Response
Type: HttpServletResponse
Representative: Response
Role: Send a response message to the browser

-Out
Type: JspWriter
Rep: output Stream
Role: Can output content to the page

-Config
Type: ServletConfig
Representative: Configuration information for the current JSP
Function: You can get the initialization parameters in the servlet tag

-page
Type: Object has the following code in the Service method Object page = this;
Represents: An object representing the current JSP
Role:

-exception
Type: Throwable
Representative: Exception information
Function: Gets the exception in the page

[6] JSP and servlet division of labor
> JSP essence is a servlet!!!
> Once in the Model1 era, servlet,jsp has completely replaced the servlet in our project.
> JSP excels at displaying a page and is not good at writing Java code
> servlet is good at writing Java code, not good at displaying pages
> We use a servlet handler and use a JSP to display the page.
> puts the results of the servlet processing into the domain and then gives it to the JSP display.





Introduction to JSP simple function

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.