JSP basic instructions and nine built-in objects and syntax

First, the concept: what is JSP✧

The JSP : J AVA S erver P AGEs script expression

JSP deployed on the server can respond to requests sent from the client and generates HTML XML or other format documents dynamically according to the request of the Web page content is then returned to the requestor

JSP technology to Java language as a scripting language for the user's HTTP request and to provide services to other Java programs on servers together to handle complex business requirements

Second, the instructions ✧

JSP page is used to configure and import resource file
format: <%@ 指令名称 属性名1=属性值1 属性名2=属性值2%>
usually to the head of the page

Type the name of the command:

1、page

Purpose: Configuration Properties JSP page

Common attributes:

✧、contentType

It is equivalent to the response.setContentType()
use of:

  • Mime type body is provided in response to the type and character set
  • Sets the current character set encoding jsp page
    (due to the nature of high-level IDE and read the contentType property is automatically set
    so that only senior IDEA Eclipse IDE, for example, in order to achieve
    low-level development tools, such as Notepad can not do)

IDE: Integrated development tool
if you need to set a lower pageEncoding property development tool to set the character set encoding of the current page

✧、language

Purpose: Sets the language used in the current page (only supports java)
in the future may support more languages

✧、buffer

Purpose: Sets the current page buffer size
defaults to 8kb

✧、import

Use "Import outer cladding when the page will need java package

Example:

<%@ page import="java.util.ArrayList" %>
<%@ page import="java.util.List" %>
✧, isErrorPage and errorPage

When a user accesses the page to see if the page is wrong, the user experience will be very friendly
Uses:
errorPage : After abnormal current page will automatically jump to the error page specified
cases:

errorPage="500.jsp"

When a page fault occurs after such a configuration is not an error page but the page jump to 500.jsp

isErrorPage : identifies the current page is an error page
defaults to false to true if you can use the built-in objects within a page Exception
Exception in the getMessage () method available to print an error message to the log

Example:

throw new RuntimeException("我发生错误啦!这里的文字会被以exception.getMessage()的方式获得");
✧、isELIgnored

Uses: whether to allow the expression is not resolved EL

Default: false ie resolved
if the EL expression is parsed page will be out to show exactly

✧、session

Purpose: To set whether to allow the creation of session
property values: true and false
is true by default


2、include

Purpose: to configure the resource file into a page by page contains information
such as the site at the top or bottom of the column is no need to repeat the writing part is the same with some pages contain can

Note: Here is a static include
is the label contains all the elements of the page are used to output

Common attributes:

file

Fill the path to the referenced page file: Usage

Example:
<%@ include file="top.jsp"%>such a statement is equivalent to the contents of the entire page top.jsp

top.jsp:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<h1>我是logo我是标题我是标题我是标题我是标题</h1>

home.jsp:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ include file="top.jsp"%>
<html>
<head>
    <title>主页</title>
</head>
<body>
    我是内容我是内容
</body>
</html>

The display is:

<h1>我是logo我是标题我是标题我是标题我是标题</h1>
<title>主页</title>
<body>
    我是内容我是内容
</body>

3、taglib

Uses: Import resources
commonly used to introduce the label library
for an introduction to jar package before use: javax.servlet.jsp.jstl.jar and jstl-impl.jar

Common properties

prefix

Purpose: definition of the prefix from the tag
, but preferably with convention "c" to define the prefix

hate

Purpose: To specify the position of the specific tag library


Third, the comments ✧

JSP page supports two types of comments: namely, html and jsp Notes Notes

1, html comments

<!-- 内容 -->

The annotation mode can only comment html snippet
can not be annotated script code content

Note the data is transmitted in response to the body but in vivo response code is annotated

2, jsp Notes (recommended)

<%-- 内容 --%>

Note that all the code can
use jsp html code annotation data is not transmitted in response to the body


Fourth, the built-in objects ✧

Built-in objects that in jsp page without having to create objects that can be used directly

JSP nine built-in objects

Four of which belong to the scope object: pageContext session and application and request and
their purpose is to share data
Let's take a look one by one:

1, a built-in objects: pageContext

Real Type: PageContext

Scope: Sharing data is limited to the current page
data is stored in the current page in the current page for a
long jump to other pages then this value is stored in the domain, there is no

Examples of general classes PageContext for access value , and obtain other objects built eight

Access values:
setAttribute()and getAttribute()
it acquires eight other built-in objects:

pageContext.getRequest()
pageContext.getSession()
pageContext.getServletContext()
pageContext.getResponse()
pageContext.getPage()
pageContext.getOut()
pageContext.getException()
2, the second built-in objects: request

Real Type: HttpServletRequest

Scope: Limited to a multiple resource requests for access
to forward requests by jumping inside the server
whenever a server that stored value responsive to the request in this domain, there is no

Examples of general classes HttpServletRequest for accessing values and jumps

Access values:
setAttribute()and getAttribute()
Jump:

request.getRequestDispatcher("").forward(request,response)
3, the third built-in objects: session

Real Type: HttpSession

Scope: Limited to multiple requests and responses of a session
as long as the browser is closed then the value of this domain, there is no deposit

Examples of general classes HttpSession for accessing values

Access values:
setAttribute()and getAttribute()
there is a "switch" to control whether the built-in objects may be used: session attributes page instruction (true / false)

4, the fourth built-in objects: application

Real Type: ServletContext

Scope: The whole project can be accessed by all users to share data between
servers open class is closed to create server class is destroyed
as long as the server is shut down then the value stored in the domain, there is no
instance of ServletContext class is generally used to access value

Access values:
setAttribute()andgetAttribute()


5, the fifth built-in objects: response

Real Type: HttpServletResponse
instance of the class generally used HttpServletResponse perform a jump course also be performed page content output

Jump:

response.sendRedirect("");
6, the sixth built-in objects: page

Real Type: Object
That translated into an instance of this jsp java class
represents an object of the current page is equivalent to this
the equivalent of Servlet current page

7, the seventh built-in objects: out

Real Type: JspWriter output stream object
instance of class JspWriter can output data to the page

Output order:
content put out an object placed in the output buffer response to
the first response output content itself is then output to the contents inside out

8, the eighth built-in objects: config

Real Type: ServletConfig
instance of the class ServletConfig
Servlet configuration object

Purpose is to obtain some of the configuration parameters Servlet

9, ninth built-in objects: exception

Only page declares isErrorPage="true"only the object
's real type: throwable exception object
instance of the class Throwable
exception object

You can be on the wrong page information output of the error


Fifth, grammar ✧

1、<%= %>

For output An expression that outputs the result to the page

2、<% %>

Java code segment for defining a 0-n statement is only a general statement

Example:

<%
    int a=10;
这里定义局部变量
    System.out.println(a);

    out.print(a);
这里输出的是局部变量(10)

    out.print(this.a);
这里输出的是成员变量(100)
%>

3、<%! %>

For global variables and member methods to create a class of
less use this syntax

<%!
    int a=100;
这里定义全局变量

    public static void aaa()
    {

    }
这里定义方法
%>

Characterized by two Java code segment may connect automatically:

<table border="1" align="center" width="40%">
    <tr>
        <td>姓名</td>
        <td>年龄</td>
    </tr>

    <%
        for (int i=0;i<10;i++) //这里定义Java遍历语句
        { //前半部分括号
    %>

    <tr>
        <td>张三</td>//被循环10次的部分
        <td>18</td>
    </tr>

    <%
        } //后半部分括号
    %>

</table>

Here Insert Picture Description


Published 174 original articles · won praise 5 · Views 240,000 +

Guess you like

Origin blog.csdn.net/Piconjo/article/details/104915754