当前位置:网站首页>JSP learning (3) -- JSP implicit object
JSP learning (3) -- JSP implicit object
2022-06-22 17:31:00 【xiao_ zhu_ ting_ feng】
1.0 summary
jsp Implicit objects are jsp Containers can be provided without declaration in jsp Page Java Objects used directly by program blocks and expression parts , Also known as jsp Built-in objects
1.1 brief introduction
We know JSP There are only two kinds of content in , Template data and elements , The element contains instructions , Script , label ( Behavior ), The script will slowly be replaced by all the tags , in other words JSP There is basically no embedding in Java Code , But we also know JSP Will be converted to servlet,
stay Servlet in , When outputting data , All need to pass response.getWrite(); But in JSP in , Use it directly out Object to output , Why? ? That's why out by JSP A hidden object of ,JSP Built in 9 Hidden objects , bring JSP Than Servlet It's easier to use , More convenient .
1.2 classification
List of interfaces to which the implicit object belongs

According to action

2.0 Input and output objects (out,request,response)
2.1 out object

- request object
Common methods are as follows

request Object application instance

The operation effect is as shown in the figure :

request.setCharacterEncodiong,request.setAttribute,request.getAttribute Application of methods

The operation effect is as follows :

2.3 response object
response The method provided by the

response Application example ( Refresh the page )

The effect is as follows

sendRedirect Realize page redirection


The effect is as follows

We can see that the page consists of responsDemo02 Jump to the responseDemo02_01
cookie Save client user information
- login.jsp User login interface


The implementation effect is shown in the figure below

- cookieSave.jsp ( Handle cookie value ,, And add a hyperlink to jump to another page to view the login page information )


The implementation effect is shown in the figure below

- cookieQuer.jsp( Show cookie Saved user name and password information )


The implementation effect is shown in the figure below
Select Save password

Choose not to save the password

3.0 Scope object (session object ,application object ,pageContext object )
3.1 session object
session Objects are objects that are automatically created by the server and related to user requests . The server will create one for each user session Object is used to store user information , Track user actions . This object is used internally Map Class to save data , So its data type is key-value form . Corresponding javax.servlet.http.HttpSession.class object .
The server creates objects in memory for different browsers to store data, which is called seesion. And authenticate the unique identity of this object SessionId( Store in cookie in ) Back to the browser , The browser will this SessionId preservation , When the browser sends a request to the server again , Together with this cookie Send it together , Find the corresponding session.
session The method provided by the
Method name | Function is introduced |
long getCreateTime() | obtain session Object creation time ; The return is from 1970 year 1 month 1 The number of milliseconds from the day to the establishment time |
String getId() | Get current session Object's ID Number , This ID Is the only one. , Used to indicate that each login to IE Browser users ; When the browser is refreshed , This value is constant ; But when you close the current browser and reopen a browser , This value will change |
Enumeration getAttributeNames() | obtain session The names of all values stored by the object , Back to a Enumeration Example |
void removeAttribute() | Delete session The name of the object is name Stored value of |
void Invalidate() | Interrupt the current session object |
Boolean isNew() | Judge the present session Whether the object is a newly created session object |
void setMaxInactiveInterval(int interval) | Used to specify the time , In seconds ,servlet The container will keep the session valid for this period of time |
int getMaxInactiveInterval() | Returns the maximum time interval , In seconds ,serlet The container will save the session open during this time |
Session Object instances
login.jsp


loginSuccess.jsp


Refresh the interface in five seconds ( The session is valid for 5s,5s Then the page information is automatically destroyed )

3.2 application object
This object is mainly used for multiple jsp Page or servlet Sharing variables between , Responsible for providing some global information about the application running in the server

Application Object method application

The operation results are as follows , The number will increase after the refresh


3.3 pageContext object ( This object is important )
PageContext The object is jsp The context of the page itself , Its scope is that all implicit objects defined in the page scope can be accessed by using it on the same page ,pageContext It is also a domain object , Can be used to save data , adopt pageContext Domain objects can also manipulate three other scopes (Request,Session,ServletContext)
pageContext Common methods
This function is more powerful , Basically, he has everything , Because it is JSP The manager of the page ( Context ), therefore JSP Built in objects in , It can all get , Here's how it works api:
1) Get the other eight built-in objects getXxx()
stay In common classes, you can use PageContext Get something else JSP Implicit objects . Use... When customizing labels .
pageContext.getOut(); // get out object
pageContext.getApplication(); // get application object
wait ....
2) Operate on the properties of the scope ( Four scopes )
Operate on the properties of the default scope .page
Object getAttribute(String name); // get page Scope data
void setAttribute(String name,Object o); // to page Scope settings
void removeAttribute(String name); // to page Scope remove content
3) Operate on the properties of the specified scope
Object getAttribute(String name,int Scope); // get Specify the data in the scope
void setAttribute(String name,Object o,int Scope); // Set the content for the specified scope
void removeAttribute(String name,int Scope); // Remove the contents of the specified scope (page/request/session/application)
4) Provide scope constants
PageContext.PAGE_SCOPE page
PageContext.REQUEST_SCOPE request
PageContext.SESSION_SCOPE response
PageContext.APPLICATION_SCOPE application
page The most powerful method in :
findAttribute(String name); // Automatically from page request session application Search for , Find it and take the value , Find the end .

stay 2.jsp in

pageContext Object method application
1. And request Object scope comparison
pageContextDame01.jsp

pageContext01_01.jsp

The operation effect is as shown in the figure

Result analysis :pageContext The properties saved by the object are limited only in the current page , The page is empty after jump ,request Object in the current request Valid within the scope of the request .
- And session,application Object scope comparison

Result display



Result analysis :session object ( The scope is the time that the user continues to connect to the server ),application object ( From server start to shutdown ) Than pageContext( Only applicable to the current page ) The life cycle should be long , and application The longest life cycle .

type :ServletConfig
Can get servlet Is the initialization parameter of , obtain servletContext object , obtain servletName.
I am here. servlet It explains in detail , You can check it out !
4.2、exception Exception object
Contains information about exceptions
Use it , must combination page Directive isErrorPage Properties and errorPage attribute .
exception.jsp Throw an exception NullPointException, And jump to error.jsp Error display page errorPage Property means that if an uncapped exception occurs , Will jump to error.jsp page
give an example :

error.jsp isErrorPage Property indicates that the page is an error display page , You can use exception object

visit : visit http://localhost:8080/Web_Jsp/exception.jsp
![]()
边栏推荐
- Principle of synchronized implementation
- Processing source code of spark executor execution results
- UI automation positioning edge -xpath actual combat
- Application description of DAP fact table processing summary function
- WPF 实现星空效果
- Analysis of the writer source code of spark shuffle
- Mybaits:接口代理方式实现Dao
- The way to optimize spark performance -- solving N poses of spark data skew
- mysql账号增删改、数据导入导出命令举例
- Recommend 7 super easy-to-use terminal tools - ssh+ftp
猜你喜欢
![[step 1 of advanced automated testing] 1 minute to introduce you to automated testing](/img/00/9647d552749092954a91bd84307773.png)
[step 1 of advanced automated testing] 1 minute to introduce you to automated testing

0 basic how to get started software testing, can you succeed in changing careers?

每秒處理10萬高並發訂單的樂視集團支付系統架構分享

You call this crap high availability?

Content recommendation process

Mybaits:接口代理方式实现Dao

Apache ShardingSphere 一文读懂

Xshell 7(SSH远程终端工具) v7.0.0109 官方中文正式版(附文件+安装教程)

快速掌握 ASP.NET 身份认证框架 Identity - 用户注册

Application description of DAP fact table processing summary function
随机推荐
系统吞吐量、TPS(QPS)、用户并发量、性能测试概念和公式
STM32 series (HAL Library) - f103c8t6 hardware SPI illuminates OLED screen with word library
数据库mysql 主从方案
mysql服务器启动后自动停止
CMB model 23 ukey is not recognized on win7
Blazor University (31)表单 —— 验证
Analysis of the writer source code of spark shuffle
测试组的任务职责和测试的基本概念
WPF effect chapter 190: playing listbox again
Examples of MySQL account addition, deletion, modification, data import and export commands
hydra安装及使用
client-go gin的简单整合十-Update
股票在哪家的平台买比较安全呢?
Docker 之MySQL 重启,提示Error response from daemon: driver failed programming external connectivity on **
同花顺是什么?在线开户安全么?
System throughput, TPS (QPS), user concurrency, performance test concepts and formulas
Basic application of scala for
The way to optimize spark performance -- solving N poses of spark data skew
Xftp 7(FTP/SFTP客户端) V7.0.0107 官方中文免费正式版(附文件+安装教程)
Hydra installation and use