JSP and El expressions

Source: Internet
Author: User
Tags html tags in domain

First, JSP

1. JSP (Java Server Pages), like the Servlet technology, is a technology defined by SUN to develop dynamic WEB resources.

JSP technology allows you to nest Java code in a page, and allows developers to get Web-common objects such as request, response, and so on. Realize the interaction between the server and the browser, so JSP is also a dynamic WEB resource development technology.

2, the implementation of JSP process. When a user accesses a JSP page for the first time, the server translates the contents of the JSP into a Servlet and then stores the bytecode and source files in the host directory of the server work directory. Through the Servlet, the server outputs the contents of the JSP page through the Out object to the client's browser. When the JSP is embedded in the Java code, the server will leave the Java code to the Servlet to execute intact. If a compiled Servlet byte code already exists in the server, then the user accesses the JSP instead of the JSP, but accesses the servlet directly.

3, Jsp Nine large implicit objects:

(1) Request: Encapsulates the user submits the information---(User request period requests)

(2) PageContext: encapsulates all other objects. ---(Page execution period page)

(3) session: Obtained when the user first visits. is a built-in domain object for a Jsp. ---(Session sessions throughout)

(4) Application: The ServletContext object that is created when the server is started. Which is a WEB application. ---(whole application running period application)

(5) Out: A Stream object that outputs data to the client machine. For the output of various data. The interior is a jspwriter with a default cache size of 8KB. ---(Page execution period page)

(6) Config: Configure the servlet, which is used to access the initialization parameters of the servlet instance. ---(Page execution period page)

(7) Page: Refers to the current JSP page, equivalent to the This keyword in the servlet, representing a servlet instance generated from the page. ---(Page execution period page)

(8) Response: The customer's request to make dynamic response to the customer to send data. ---(Page execution period page)

(9) Exception: abnormal. ---(Page execution period page)

4, JSP Best practices

The Servlet is responsible only for responding to request-generation data and bringing the data to the jsp,jsp using the forwarding technique only as a display of data.

Second, JSP syntax

1, Jsp template elements. The HTML content in the JSP page is called the JSP template element. The JSP template element defines the basic skeleton of a Web page, which defines the structure and appearance of the page.

2, Jsp script expression. <%= expression%>. Current time: <%= new Date (). tolocalstring ()%>.

3, JSP script fragment. Used to add multiple lines of Java code to a page. <% Java Code%>

4, JSP declaration. The code written in the JSP page is translated to the service in the Servlet by default, and the Java code in the JSP declaration is translated outside the _jspservice method. This defines the global variables, methods, and static blocks of code for the Servlet. Because the implicit object of the JSP is life in the service method, the members defined in the JSP declaration cannot be accessed.

5, JSP instructions. JSP directives are designed for servers that do not directly produce any visible output, but rather tell the server how to handle the rest of the JSP page.

<%@ directive Attribute name = "Value"%> Example: <%@ contexttype= "text/html charset=gb2312"%>

(1) page instruction. The page directive is used to define the various properties of the JSP page, regardless of where the page instruction appears in the JSP page, it is a JSP page.

<%@ page

[language= "Java"]///To specify that the current page is embedded with Java code.

[extends= "Package.class"]//To specify which class the translated Servlet inherits.

[import= "java.io.*"]//the package needed to import Java code, separated by a "," number, or separately.

[session= True | false]//Specifies whether the translated Servlet automatically creates a Session object, and the default value is true. We often have to set it to false. Because the session is valid for a long time, we do not need to create a session object to reduce server pressure.

[buffer= none | 8kb | sizekb]//is used to specify the size of the flushing area of the out object, that is, the buffer size of the jspwriter, which defaults to 8KB.

[Autoflush= ' true | false ']//Specifies whether the upper buffer buffer is automatically refreshed, and defaults to true.

[Isthreadsage= ' true | false ']//default is True, implemented in a way that inherits the Singlethreadmodel markup interface.

[info= "text"]//Specify the current page information as text.

[errorpage= "Error.index"]//Specifies the error message page where the directory for the specified URL must use a relative path and, if it starts with "/", represents the root directory of the current WEB application, not the root of the site. Another way to set the error page: To add a child element under the,<web-app> element in the Web.xml configuration file:

<exception-type> exception type fully qualified name </excetion-type> or <error-code> error number </error-code>

<location> error Message Display page </location> Note that the error page must be larger than 1kb to display correctly. If the error message page is configured in the Web.xml file, the errorpage directive set in the JSP will not work. The exception implicit object is used to pass the error message.

[Iserrorpage= ' true | false ']//Specify whether this page is the wrong process page, and if so, better change to true.

[ContentType]//Specifies the MIME type of the page, what page the page is, and what encoding is used for the page. Where CharSet is used to describe the JSP source file character encoding set. Refers to what encoding is used when sending to a client.

[Pageencoding= "CharacterSet | Iso-8859-1 "]//is used to specify how the current JSP file is encoded, and how the file itself is encoded.

[Iselignored= ' true | false ']//Specifies whether the page ignores EL expressions, and the default value is False.

(2) Include directives: for the introduction of other JSP pages, if the use of the include directive to introduce other JSP pages, then the JSP server will translate the two JSP into a Servlet, so include instruction is often referred to as static introduction.

<%@ include file= "url"%> where the path to the URL begins with "/" to represent the current WEB application. The name of the page suffix recommended in the JSP specification is. JSPF. Since the static introduction is a two JSP compiled into a Servlet to call the client, so the two introduced JSP page instructions can not conflict. and introduced JSP pages do not appear JSP template elements, that is, HTML tags, otherwise the HTML tag content will be called to the client, resulting in code is not elegant.

Dynamic Introduction: Take advantage of the Getrequestdispatcher ("URL") of the Request object. Include (Request,response); Method. Principle: In the Servlet runtime dynamic to add other servlet to the page, to the server multiple requests, so inefficient. We should try to use the static introduction method in the development. Static introduction is the synthesis of multiple servlet sets into a servlet before calling the client.

Third, Jsp garbled problem analysis

JSP garbled reason: We edit JSP, because the editor is different, the stored Jsp source file specified encoding method is also different, if we use Notepad edit JSP, then the JSP source file encoding method for gb2312 saved to our hard drive. When the user accesses the server, the server through the hard disk edited Jsp file translation to the Servlet, because the server is not Chinese design, so often using iso-8859-1 encoding, because the ISO-8859-1 code table does not correspond to Chinese characters, So can not compile the data normally, and then compiled into the. class file is garbled, also caused the data sent to the client can not be normal display.

Solution: Now that we have found the reason for the garbled, then we should control the server to compile the JSP source file into a Servlet using a code table that can be compiled in Chinese. So editing the source file should be encoded in the same way that the server compiles the JSP. Then we can add pageencoding= "utf-8" to the page instruction and tell the server to encode the Utf-8 code table. Since the newest server will automatically write the coded way to the client in the form of a response header, there is no need to add the contenttype= "text/html;charset=utf-8" page directive, but to be compatible with the old server, It is also OK to join in caution.

Four, out implicit object

1. An out implicit object is used to send text data to the client. The Out object is returned by invoking the Getout () method of the PageContext object, and its function and usage are very similar to those PrintWriter objects returned by the Servletresponse.getwriter () method.

2, the type of out implicit object in the JSP page is jspwriter,jspwriter equivalent to a priterwriter with caching function, the buffer property of the page instruction of JSP pages can adjust its cache size and even turn off the cache.

3, when the data is written to out, then when the data will be addressed to the client's browser.

(1) Setting the Buffer property of the page instruction closes the cached function of the Out object, and once written to the out object, out is sent directly to the PrintWriter object's buffer.

(2) The buffer for the Out object is full.

(3) The entire JSP page ends.

4, out buffer working principle:

Writing data to out is not addressed directly to the client, but is first written in the out buffer. Similarly, writing data in the printwriter of a Servlet is written in a printwriter buffer, when out buffers are full, or when the JSP page executes, the server writes the data from the out buffer into the printwriter buffer, and then addressed to the client. So if you write the data in the PrintWriter, and write the data in out, then regardless of the sequence of writes, the last time you call the client, the PrintWriter data is sent to the client first. The Out object is a printwriter buffer. When we write Jsp pages, we try to use out objects to output data.

Five, JSP implementation file download[Java]  View plain copy print? <%@ page language= "java"  import= "java.util.*"  pageencoding= "Utf-8"%><% @page   Import= "Java.io.FileInputStream"%><% @page  import= "Java.io.OutputStream"%>   <% @page  import= "Java.net.URLEncoder"%><! doctype html public  "-//w3c//dtd html 4.01 transitional//en" ><%            string path = application.getrealpath (" download/picture. jpg ");           string filename =  path.substring (Path.lastindexof ("\")  + 1);            fileinputstream in = new fileinputstream (path);            int len = 0;            byte[] b = new byte[1024];           OutputStream os =  Response.getoutputstream ();           response.setheader (

Related Article

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.