3--page instruction in jsp

Page instruction is more important in jsp development.
Insert picture description here
The above operation instructions, marked in red, must be remembered, and only the import instruction can appear multiple times.
The syntax of the page instruction:
<%@ page attribute=”content”%>
Example 1: Character encoding set setting

<%@page language="java" contentType="text/html; charset=GBK" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
	<center>
		<h2>欢迎大家来到此播客学习</h2>
	</center>
</body>
</html>

or

<%@page language="java" contentType="text/html" pageEncoding="GBK"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
	<center>
	<h2>欢迎大家来到此播客学习</h2>
	</center>
</body>
</html>

The difference between using contentType and pageEncoding to set encoding:
pageEncoding refers to the encoding of the JSP file itself, and charset in contentType refers to the content encoding sent by the server to the client. If pageEncoding exists in JSP, then the encoding of JSP will be determined by pageEncoding, otherwise it will be determined by the charSet attribute of contentType. If neither exists, ISO-8859-1 encoding will be used. All content in jsp has been encoded twice. PageEncoding is used in the first stage, utf-8 is used in the second stage, and the third stage is the web page generated by Tomcat. At this time, contentType is used. Generally, development only needs to specify the pageEncoding encoding.
Example 2: Import external classes or packages

<%@page language="java" contentType="text/html" pageEncoding="GBK"%>
<%@page import="java.util.*" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
	<%
	   List<String> names= new ArrayList<String>();
	%>
</body>
</html>

<%@page import="java.util.*" %>You can import multiple classes or packages, separated by commas, or write multiple imports.
Example 3: Setting of the error page The error page
may be generated:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" errorPage="error.jsp"%>
    <!--errorPage:指定错误的处理页面  -->
<!DOCTYPE html >
<html>
<head>
<title>Insert title here</title>
</head>
<body>
	<%
	   //这里有一个错误
	   int num = 4/0;
	%>
</body>
</html>

Error handling page:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" isErrorPage="true"%>
    <!-- 
    isErrorPage:表示当前页面为错误处理的页面
     -->
<!DOCTYPE html >
<html>
<head>
<title>Insert title here</title>
</head>
<body>
	<%
		response.setStatus(200);
	%>
	<h1>系统出现问题,请联系管理员</h1>
</body>
</html>

Sometimes it may not be able to jump to the error page. In the operation of the error page, if there is a situation where the error.jsp page cannot be displayed, it may be that Tomcat also considers the error.jsp to be an error, and thus cannot jump. You can write it directly in error.jsp: response.setStatus(200);
This sentence sets a 200 HTTP status code, indicating that there is no error on this page.

Guess you like

Origin blog.csdn.net/qwy715229258163/article/details/113973930