JAVAWeb-Servlet and JSP-JSP-JSP instruction

JAVAWeb-Servlet and JSP-JSP-JSP instruction

JSP directives role is to do some basic property to the current page, provides the basic environment for running the current page.
Three types of instructions contained in a JSP

  1. page instruction page directive
  2. include instructions comprising instructions
  3. taglib directive, instruction tag library

These instructions using the syntax, format are as follows:

<%@ 指令名称 属性名=属性值 ...%>

Here's what a few commonly used commands

page directive

1.import property

Guide package Well, right, in addition to the java.lang package without guide under class, the other had to guide package ah.

//导一个包
<%@ page import="java.util.*" %>

//导两个包,包名之间用逗号隔开
<%@ page import="java.util.*,java.sql.*"%>
2.errorPage property

errorPage property value, that can be jsp files can also be a html file. When the current JSP page fault occurs, it will jump to errorPage written page.
grammar:

<% page errorPage="error.jsp"%>

have to be aware of is:
In the error page, if you need to use the exception object, you need to join the page directive

<%@ page isErrorPage="true"%>

test.jsp

//test.jsp

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

//发生异常后,跳转到error.jsp页面
<%@ page errorPage="/error.jsp"%>
<html>
<head>
	<title>errorPage属性</title>
</head>
<body>
	<%
		//这里手动模拟一个被0除异常
		System.out.pringln(10/0)
	%>
</body>
</html>

error.jsp

//error.jsp

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

//这里要配置isErrorPage属性,否则JSP不会内建exception对象
<%@ page isErrorPage="true" %>
<html>
<head>
	<title>error</title>
</head>
<body>
	<%
		//这手打印出错误的堆栈信息
		exception.printStackTrace();
	%>
</body>
</html>

3.session property

If you want to use the session object in JSP, you need to write in a JSP page on page directive:

<%@ page session="true" %>  

This and similar objects exception.

There are a lot of page directive, but it seems even if the amount is not often used, it did not take long forgotten, and so therefore when needed, you can go to view the document, just to say a few commonly used.

inlclude instruction

Only one attribute comprising instructionsfile.
This attribute specifies the path to the JSP files JSP file to include.
JSP files can contain content pages, and other variables to be included in the file.
For example:
There left.jsp and main.jsp, let main.jsp contain left.jsp.

The included file left.jsp

//被包含文件left.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java"  %>
<html>
<head>
	<title>这是被包含文件left.jsp</title>
</head>
<body>
	left
	<%
		int num = 88;
	%>	
</body>
</html>
//包含文件的文件mian.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java"  %>

//include指令,包含left.jsp
<% include file="/left.jsp" %>
<html>
<head>
	<title>mian.jsp</title>
</head>
<body>
	<%=num %>		
</body>
</html>

The above operating results JSP as follows:
This is the result of the above jsp run
we can see, main.jsp not only contains the contents left.jsp the same, also included in the definition of left.jsp variable num, so you can directly access in main.jsp the num.
This method is called static includes.
So, include application scenarios generally what is it?
We all know that a site may contain many pages, but these pages may be in:

  1. Web page navigation section head
  2. Record information section at the bottom of the page
  3. The right part of the page links section

Are the same, these same parts, use can be contained, to reduce a code redundancy, also more convenient from modification. Modern web development, which often has the following modes:

Use a large main, jsp includes head, body, jsp on the right side, so easy code management
Such use main.jsp contain head.jsp, body.jsp, right.jsp way to web design

3.taglib instruction

This section is more, finished behind supplemented on the link.

Released two original articles · won praise 1 · views 150

Guess you like

Origin blog.csdn.net/The_Love_Juice/article/details/104456950