Java web--jsp (4)

Source: Internet
Author: User
Tags java web tomcat server

1. JSP directive: JSP Directive (Directive) is designed for the JSP engine,
They do not directly produce any visible output, but simply tell the engine how to handle the rest of the JSP page.

2. In the current JSP 2.0, the page, include, and taglib three directives are defined

3. Page directive:

1). Page directives are used to define the various properties of a JSP page, regardless of where the page directive appears in the JSP page.
It is the entire JSP page, in order to maintain the readability of the program and follow good programming habits, the page directive is best placed in the entire JSP page start position.

2). The properties commonly used by page directives:

①. Import Property: Specifies the class to import for the Servlet that corresponds to the current JSP page.
<% @page import= "Java.text.DateFormat"%>

②. Session property: Evaluates to True or FALSE, specifies whether the session hidden variable of the current page is available, or whether the current page must be generated when accessing the HttpSession
Object.
<%@ page session= "false"%>

③. ErrorPage and Iserrorpage:
> ErrorPage Specifies what to do if the current page has an incorrect actual response page. where/represents the root directory of the current WEB app.
<%@ page errorpage= "/error.jsp"%>

> How the JSP engine uses the request forwarding when responding to error.jsp.

> Iserrorpage Specifies whether the current page is an error handling page, which can be used to indicate whether the current page can use exception to hide variables. Note that if you specify
Iserrorpage= "true" and use the Exception method, it is generally not recommended to have direct access to the page.

> How do I make it impossible for a customer to access a page directly? For a Tomcat server, files under Web-inf cannot be accessed by
to enter the address directly in the browser. But forwarding by request is possible!

> can also configure the error page in the Web. xml file:

<error-page>
<!--Specify the code for the error: 404 No resource specified, 500 internal error.-->
<err Or-code>404</error-code>
<!--Specify the location of the response page-->
<location>/web-inf/error.jsp</location
</error-page>

<error-page>
<!--Specifies the type of exception-->
<exception-type> Java.lang.arithmeticexception</exception-type>
<location>/WEB-INF/error.jsp</location>
</error-page>

④. ContentType: Specifies the type of response for the current JSP page. The actual call is Response.setcontenttype ("text/html; Charset=utf-8 ");
Typically, the value for a JSP page is text/html; charset=utf-8. CharSet specifies what the character encoding of the returned page is. Typically the value is UTF-8

⑤. Pageencoding: Specifies the character encoding of the current JSP page. Typically, this value is consistent with CharSet in ContentType.

⑥. Iselignored: Specifies whether the current JSP page can use an EL expression. Usually the value is false.

3. include directive: <%@ include file= "b.jsp"%>

1). Include directives are used to notify the JSP engine to merge the contents of other files into the Servlet source file converted into the current JSP page when translating the current JSP page.
This approach, introduced at the source file level, is called static ingestion, and the current JSP page is tightly combined with a statically introduced page as a servlet.

2). The setting value of the file property must use a relative path

3). If you start with/begin with the root directory of the current Web application (note that it is not the site root), it is relative to the current file.

4. Jsp:incluce Tags:

1). <jsp:include page= "b.jsp" ></jsp:include>

2). Dynamic ingestion: Instead of generating a servlet source file like the include directive, it generates two servlet source files and then uses a method to include the target page
In.

Org.apache.jasper.runtime.JspRuntimeLibrary.include (Request, Response, "b.jsp", out, false);

5. Jsp:forward:

1).

<jsp:forward page= "/include/b.jsp" ></jsp:forward>

Equivalent.

<%
Request.getrequestdispatcher ("/include/b.jsp"). Forward (request, response);
%>

2). However, using Jsp:forward, you can use the Jsp:param sub-label to pass some parameters to b.jsp. Similarly jsp:include can also use Jsp:param sub-tags.

<jsp:forward page= "/include/b.jsp" >
<jsp:param value= "ABCD" name= "username"/>
</jsp:forward>

OR

<jsp:include page= "/include/b.jsp" >
<jsp:param value= "ABCD" name= "username"/>
</jsp:include>

On the b.jsp page, you can get the incoming request parameters through Request.getparameter ("username").

6. About Chinese characters:

1). On the JSP page input Chinese, the request page does not appear garbled: Guarantee Contenttype= "text/html; Charset=utf-8 ",
Pageencoding= "UTF-8" CharSet and pageencoding are encoded in the same language and are supported in Chinese. A value of UTF-8 is generally recommended

It is also necessary to ensure that the character encoding displayed by the browser is consistent with the encoding of the requested JSP page.

2). Gets the Chinese parameter value: The default parameter is encoded as iso-8859-1 used in the transfer process

①. For POST requests: As long as the request information is obtained (in the call to Request.getparameter or request.getreader, etc.),
Call Request.setcharacterencoding ("UTF-8").

②. For GET requests: The previous method is not valid for get. You can modify the way Tomcat server.xml files are.

Refer to the Usebodyencodingforuri property of the http://localhost:8989/docs/config/index.html document.
Add the Usebodyencodingforuri= "true" property to the Connector node.

<connector connectiontimeout= "20000" port= "8989" protocol= "http/1.1" redirectport= "8443" useBodyEncodingForURI= " True "/>

Java web--jsp (4)

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.