当前位置:网站首页>JSP technology: JSP overview, JSP basic syntax, JSP instructions, JSP implicit objects, JSP action elements

JSP technology: JSP overview, JSP basic syntax, JSP instructions, JSP implicit objects, JSP action elements

2022-06-11 07:46:00 Nagisa siku

1、JSP summary :

1.1 What is? jsp: Its full English name is java server pages ,java Server page , It is built on servlet Dynamic web page development technology based on standard .

  • java Server side page : stay jsp It can be written on java Code , Can also write html Mark , It runs on the server side .
  • jsp = java + html + jsp Something about yourself . Why does it produce jsp technology :
    Because in the early days there was no jsp when , need servlet Write data to the page , More trouble , So there is jsp technology ,
    jsp The biggest feature : It's very easy to write pages html.
    jsp In fact, its essence is servlet: You can see jsp Inherited HttpJspBase Inherit HttpServlet.

1.2 Write the first jsp file : jsp Generate java file , Translate it into class file , There is tomcat Server's work Catalog .
Be careful : modify jsp The default encoding format for :window > preferenes > Search for jsp > Select encoding Make changes

1.3 JSP Operation principle :

jsp–>java—>class

1. First visit through browser jsp when 
 2.JSP The container will generate the corresponding Servlet file , That is to say .java file 
 3.JSP Containers , Will be able to java Corresponding to file compilation class file 
 4.JSP Containers , I'm going to generate the corresponding servlet object 
 5. By generated servlet object , To handle requests sent by browsers , Response data to the browser .

2、JSP Basic grammar

2.1 JSP Script elements
stay jsp Page writing java Three forms of :
1. You can declare statements and methods
2. Can write java Code segment
3. You can write expressions :

  • 1.JSP Scriptlets: The corresponding position is generated in _jspService Methods the internal .
    stay jsp You can write on the page java code snippet , Basic grammar :<% java code snippet %>
  • 2.JSP Statement statement : The corresponding position is generated in the member position .
    stay jsp You can write variables and methods in it : Basic grammar :
    <%!
    Define variables or methods
    %>
  • 3.JSP expression : The corresponding position is generated in _jspService Methods the internal .
    stay jsp page , You can enter a result , Basic grammar :<%= expression %>, In fact, it's the same as out.print(); , Be careful : The semicolon cannot be written after the expression

2.2 JSP notes :
stay jsp Inside , Can write html, Can write java, Can write jsp Some of its own content .
stay jsp There are three forms of notes in it :

  1. html notes : : It exists everywhere .
  2. java notes : A single , Multiple lines , Documentation Comments : It exists jsp page , Also exist java In the document , Corresponding generated html It disappeared
  3. jsp notes : <%-- jsp notes –>: There is only jsp page , Generating the corresponding java When you file , It's gone .

3.JSP Instructions :page Instructions 、include Instructions 、taglib Instructions

3.1 page Instructions 
page The purpose of the command is to set and jsp Page related information , For example, setting up jsp Page code ,jsp The default language of the page, etc 
 Basic grammar :<%@ page  attribute =“ Property value ”  attribute =“ Property value ”  attribute =“ Property value ” %>
 such as :
<%@ page language=“java” contentType=“text/html; charset=UTF-8”
pageEncoding=“UTF-8”%>
 Common properties :
language:  default language java, It's just java A language. .
pageEncoding: Set up jsp The encoding format of , In general, you can omit  contentType This attribute .
session attribute : The type of worth is boolean, If the corresponding value is true, stay jsp The page can be used session object , On the contrary, you can't use .
import attribute : Guide pack , And in java It uses the same , If you want to use a class , The premise is to import the package name of a class .
----
3.2 include Instructions :
 stay jsp Pages usually need to display information from other pages , So you can go through this include Command to include other pages .
 Basic grammar :<%@ include file=“ Of contained files url” %>
 Create two jsp page : date.jsp page ,include.jsp page .
 Use jsp Of include The inclusion of an instruction implementation , Also known as static inclusion , The characteristics of static inclusion :
1. Put the content of multiple pages , Merge output , For example date.jsp  and  include.jsp Content merge output .
2. Multiple pages will generate one java file , After compiling , Corresponding to one class file .

4.JSP Implicit objects

4.1 Overview of implicit objects :
What are implicit objects : stay jsp Inside the page ,jsp Provides some objects , We don't have to create it ourselves , Objects that can be used directly .
jsp Nine common implicit objects :
request: HttpServletRequest, Request object , It's also a domain object , Scope a request
response:HttpServletResponse, The response object
session: HttpSession , Conversation object , It's also a domain object , Scope a conversation
application:ServletContext, Context object , It's also a domain object , The whole project
config:ServletConfig, Configuration object
out:JspWriter, Actually sum PrintWriter It's about the same .
page:Object , On behalf of the current jsp Page objects for
pageContext:PageContext ,jsp Containers
exception:Throwable, Exception object , Used to capture exception information .
Only current jsp Page has isErrorPage="true" When this property , Can be used exception object .

4.2 out object
out Object to output content to the web page , And finally through reponse Buffer for output .

4.3 pageContext object
The first function : Get the other eight implicit objects .

1. Get request object :getRequest();
2. Get the response object :getResponse();
3. Get the output object :getOut();

The second function : Manipulate the other three domain objects :request、session、application.
pageContext It's also a domain object : The scope of action is currently jsp page .

 Methods for manipulating the other three domain objects :
1. Store value :setAttribute(String key,Object obj,int scope);
APPLICATION_SCOPE : The corresponding is application
REQUEST_SCOPE:  The corresponding is request
SESSION_SCOPE: The corresponding is session
PAGE_SCOPE: The corresponding is oneself  pageContext
2. Value :getAttribute(String key,int scope);

 Conclusion : stay pageContext Domain objects use findAttribute Method gets value , A global search was performed ,
 The scope of the search :pageContext—>request----->session----->application
 First look for objects with small scope , If there is a value to get , Stop looking down , If no corresponding value exists , Just keep looking down .

4.4 exception object

 It's used to capture exception information .
 Using it usually uses two properties :
>>> errorPage: The value of the property , Pages that handle exception information , That is to say, an exception has occurred on the current page , Jump to the page dealing with exception information 
     such as :
    <%@ page language="java" contentType="text/html; charset=UTF-8"
     pageEncoding="UTF-8" errorPage="error.jsp"%>
 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 <html>
 <head>
 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 <title>Insert title here</title>
 </head>
 <body>
 	<%
 		int a=3;
 		int b=0;
 	%>
 	 Output :<%=(a/b) %>
----------------------

isErrorPage:  The value of the property is true perhaps false, If true, On the current page, you can use exception object   such as : <%@
page language=“java” contentType=“text/html; charset=UTF-8”
pageEncoding=“UTF-8” isErrorPage=“true”%>

----------------------
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 	<html>
 	<head>
 	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 	<title>Insert title here</title>
 	</head>
 	<body>
 		 Output exception information :  <%=exception.getMessage() %>
 	</body>
 	</html>
 	

5.JSP Action elements

5.1 jsp:include Action elements : Dynamic inclusion
Dynamic inclusion : Put the content of other resources page , Merge to the current page to be included , Last output .
Basic grammar :<jsp:include page=“ Of the included page url” flush=“true perhaps false”>jsp:include
Dynamic inclusion features : The content of multiple pages is merged and output , Multiple jsp Page generation multiple java file , Compile into multiple class file
Static inclusion features : The content of multiple pages is merged and output , Multiple jsp The page eventually generates a java file , Compile one class file .

5.2 jsp:forward Action elements : Request forwarding .
Its function is to forward requests , The effect is equivalent to RequestDispathcer Object's forward Method .
Basic grammar :<jsp:forward page=“ Forward page's url Address ”></jsp:forward>
Forward page's url Address : You don't need to carry the project name , Because forwarding is an internal behavior of the server .

原网站

版权声明
本文为[Nagisa siku]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/03/202203020518498723.html