当前位置:网站首页>JSP learning part
JSP learning part
2022-06-29 07:52:00 【Joy's sister is a rookie】
JSP Introduction to
jsp The full name is java server pages.
Java The server page of .
jsp Its main function is to replace Servlet Program returns html Page data .
because Servlet Program returns html Page data is a very complicated thing . Development costs and maintenance costs are very high .(Servlet The code is jsp It's very complicated )
eg:
public class PringHtml extends HttpServlet {
@Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// Through the return flow of the response html The page data
resp.setContentType("text/html; charset=UTF-8");
PrintWriter writer = resp.getWriter();
writer.write("<!DOCTYPE html>\r\n");
writer.write(" <html lang=\"en\">\r\n");
writer.write(" <head>\r\n");
writer.write(" <meta charset=\"UTF-8\">\r\n");
writer.write(" <title>Title</title>\r\n");
writer.write(" </head>\r\n");
writer.write(" <body>\r\n");
writer.write(" This is a html The page data \r\n");
writer.write(" </body>\r\n");
writer.write("</html>\r\n");
writer.write("\r\n");
}
}
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
This is a html The page data
</body>
</html>
JSP The essence of
jsp A page is essentially a Servlet Program .
When we first visited jsp On the page .Tomcat The server will help us jsp Page translation becomes a java Source file . And compile it into .class Bytecode program .
From the source code, you can find ,HttpJspBase class . It directly inherits HttpServlet class . in other words .jsp Translated java class , It indirectly inherited HttpServlet class . in other words , The translation is a Servlet Program
Summary :
From the generated files, it is not difficult to find a rule .
a.jsp Translate into java The full name after the file is :a_jsp.java file
b.jsp Translate into java The full name after the file is :b_jsp.java file
that When we visit One xxx.jsp After the document
Translate into java The full name of the file is :xxx_jsp.java file
xxx_jsp.java The file is a Servlet Program . original jsp Medium html The content is translated into Servlet Class service Output as is in the method .
JSP The grammar of
JSP The head of the page Instructions
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
jsp The header of the file declares :
| attribute | explain |
|---|---|
| language | Express jsp What language files are translated . Only for now java. |
| contentType | Express jsp What is the data type returned . Also in the source code response.setContentType() Parameter values |
| pageEncoding | At present jsp The character set of the page file itself |
| import | To current jsp The class package that needs to be used for page import |
| autoFlush | Set when out When the output stream buffer is full , Whether to refresh the impulse area automatically . The default value is true |
| buffer | Set up out Size of buffer ( The default is 8kb) |
| errorPage | Set when jsp An error occurred while the page was running , Automatically jump to the wrong page path |
| isErrorPage | Set up current jsp Whether the page is an error message page . The default is false. If it is true have access to exception Exception object |
| session | Set up current jsp Whether the page gets session object , The default is true |
| extends | Set up jsp Translated java Class inherits by default |
JSP Frequently used scripts
Declaration script
Format :
<%! Statement java Code %>
effect :
1. We can define global variables .
2. Definition static Static code block
3. Define methods
4. Define inner classes
Code that can be written almost inside a class , Can be implemented through declaration scripts
<%--1、 Declare class properties --%>
<%!
private Integer id;
private String name;
private static Map<String,Object> map; %>
<%--2、 Statement static Static code block --%>
<%!
static {
map = new HashMap<String,Object>();
map.put("key1", "value1");
map.put("key2", "value2");
map.put("key3", "value3");
}
%>
<%--3、 Declare class methods --%>
<%!
public int abc(){
return 12;
}
%>
<%--4、 Declare inner classes --%>
<%!
public static class A {
private Integer id = 12;
private String abc = "abc";
}
%>
Expression script
Format :
<%= expression %>
effect :
Expression script Used to output content to the page .
Expression script Translate to Servlet programmatic service In the method With out.print() Printout
out yes jsp A built-in object of , Used to generate html Source code
Be careful : Don't end the expression with a semicolon , Otherwise, an error will be reported
The expression script can output any type .
<%=12 %> <br>
<%=12.12 %> <br>
<%=" I'm a string " %> <br>
<%=map%> <br>
<%=request.getParameter("username")%>
Code script
Format :
<% java sentence %>
effect :
Can be in jsp On the page , Write the functions we need ( Is written java sentence )
The characteristics of code scripts are :
1、 Code scripts are translated in _jspService In the method
2、 Code script due to translation to _jspService() In the method , So in _jspService() All existing objects in the method can be used directly .
3、 You can also combine multiple code script blocks to complete a complete java sentence .
4、 Code scripts can also be combined with expression scripts , stay jsp Output data on page
JSP Three notes
java notes
// A single java notes
/* Multiple lines java code annotation */
Single line comments and multi line comments can be used in the translated java See in the source code
html notes
<!-- html notes -->
html Your comments will be translated into java Output the code to html Page view
jsp notes
<%-- This is a jsp notes --%>
jsp Comments will be directly ignored in translation
JSP Nine built-in objects
Nine built-in objects , It's all we can do in 【 Code script 】 Medium or 【 Expression script 】 Objects that are used directly in
| object | effect |
|---|---|
| request object | Request object , Request information can be obtained |
| response object | The response object , Response information can be set |
| pageContext object | The current page context object . You can save attribute information in the current context |
| session object | Conversation object . You can get session information |
| exception object | Exception object . Only in jsp Page page Set in command isErrorPage=“true” Will exist when |
| application object | ServletContext Object instances , You can get some information about the whole project |
| config object | ServletConfig Object instances , Can get Servlet Configuration information |
| out object | jsp Output stream object |
| page object | Point to the present jsp The object of ( It's useless , It's better to use this object ) |
JSP Four domain objects
Four domain objects are often used to store data information .
Domain objects can be like Map The same object that accesses data . The four domain objects function the same . What's different is their access range to the data .
Although all four domain objects can access data . They have priority in use .
When the four domains are in use , The order of priority is , The order of their range from small to large .
pageContext ——> request ——> session ——> application
| Domain object | effect | Scope of action |
|---|---|---|
| pageContext | You can save data in the same jsp Page usage | At present jsp Valid within page range |
| request | You can save data in the same jsp Page usage | Valid within one request |
| session | It can be saved in a session and used | Valid within a session scope ( Open the browser to access the server , Until the browser is closed ) |
| application(ServletContext) | ServletContext object | Whole web Valid within the scope of work ( as long as web The project doesn't stop , The data are all there ) |
JSP Of out Output and respon.getWriter Similarities and differences of output
response In response to , We often use it to set the content returned to the client ( Output )
out It's also used for user output .
![[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-XKLnzpI4-1654616042020)(C:\Users\pon18\AppData\Roaming\Typora\typora-user-images\image-20220607231441018.png)]](/img/e4/1b0e27d7b72f81ac43102ed4096e15.png)
Example :
![[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-gXcUimBJ-1654616042022)(C:\Users\pon18\AppData\Roaming\Typora\typora-user-images\image-20220607231530605.png)]](/img/c8/f7a696999a86ae0708907599a8bc2b.png)
![[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-Xyqgnqqc-1654616042023)(C:\Users\pon18\AppData\Roaming\Typora\typora-user-images\image-20220607231602344.png)]](https://img-blog.csdnimg.cn/f0ced8f48e15408d9337fcf0f2bcb25a.png)
JSP Commonly used labels
In the work , Almost all use static inclusion .
Static inclusion
Format :
<%@ include file=""%>
file Property specifies what you want to include jsp The path of the page
First slash in address / Expressed as http://ip:port/ Project path / Mapping to code web Catalog
eg:
<%@ include file="/include/footer.jsp"%>
characteristic :
1、 Static inclusion does not translate the included jsp page .
2、 Static inclusion is actually the inclusion of jsp Copy the code of the page to the included location to execute the output .
Dynamic inclusion
Format :
<jsp:include page=""></jsp:include>
page Property specifies what you want to include jsp The path of the page Dynamic inclusion can also be like static inclusion . Execute and output the included content to the included location
eg:
<jsp:include page="/include/footer.jsp">
<jsp:param name="username" value="bbj"/>
<jsp:param name="password" value="root"/>
</jsp:include>
characteristic :
1、 Dynamic inclusion will include jsp The page is also translated into java Code
2、 The underlying code of dynamic inclusion uses the following code to call the included jsp Page execution output .
JspRuntimeLibrary.include(request, response, "/include/footer.jsp", out, false);
3、 Dynamic inclusion , You can also pass parameters
Request forwarding
Format :
<jsp:forward page=""></jsp:forward> Is the request forwarding tag , Its function is request forwarding page Property to set the path for request forwarding
eg:
<jsp:forward page="/scope2.jsp"></jsp:forward>
<jsp:forward The forwarding function is equivalent to request.getRequestDispatcher(“/xxxx.jsp”).forward(request, response); The function of .
边栏推荐
- js异或混淆代码
- 【域渗透提权】CVE-2020-1472 NetLogon 权限提升漏洞
- 719. find the distance of the number pair with the smallest K (two points)
- The table cannot be vacuumed because the cursor is open for a long time
- Postman pre request
- SQL Server 2008 publish and subscribe to SQL Server 2017 pit avoidance Guide
- [FreeRTOS] interrupt mechanism
- 程序调试 - Debug/Release 版本
- 数组知识点小结
- jsp学习部分
猜你喜欢

搭建jenkins环境并自动关联打包好的工程jar进行自动发布

从Nacos客户端谈Nacos配置中心
![Detailed explanation of communication principle between [industrial control old horse] single chip microcomputer and Siemens S7-200](/img/56/b300c0c3606dbc328e301092615bff.jpg)
Detailed explanation of communication principle between [industrial control old horse] single chip microcomputer and Siemens S7-200

Wechat applet learning notes (summer vacation)

Explanation of swing transformer theory

Software testing

How to share the virtual environment of pycharm to jupyter Lab
![[FreeRTOS] interrupt mechanism](/img/ab/9b1d07048b4631d7cc95db99ed529a.png)
[FreeRTOS] interrupt mechanism

面试官:为什么数据库连接很消耗资源,资源都消耗在哪里?

Protobuf 二进制文件学习及解析
随机推荐
Select distinct on statement in kingbasees
js:Array.reduce累加计算、合并数组
About the problem that the kingbasees temporary file is too large
【工控老马】西门子PLC s7-300SCL编程详解
【工控老马】PLC六路抢答器系统设计详解
from xx import*等价于from xx import *,不一定要加空格
719. find the distance of the number pair with the smallest K (two points)
[FreeRTOS] interrupt mechanism
【深度之眼吴恩达第四期作业班】多元线性回归Linear Regression with multiple variables总结
C#Mqtt订阅消息
Schnuka: 3D visual recognition system 3D visual inspection principle
Problem solving -- > online OJ (13)
From XX import* is equivalent to from XX import *, and no space is required
498. 对角线遍历(模拟)
Interviewer: why does database connection consume resources? Where are the resources consumed?
【工控老马】洗衣机PLC程序控制系统设计详解
呕心沥血总结出来的MySQL常见错误以及解决方法(二)
How to solve the cross domain problem of mobile phone accessing the web in the web development scenario
jsp学习部分
Viewing application and installation of Hana database license