当前位置:网站首页>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
![]()
边栏推荐
- clickhouse 21. X cluster four piece one copy deployment
- Mqtt of NLog custom target
- 短视频带货源码,保存图片到相册/图库
- MySQL master-slave connection prompt of docker: communications link failure
- Thoughts on joint primary key
- 【招聘】[北京中关村/远程][TensorBase][开源数据仓库]等一群人,做一件事
- AD20/Altium designer——过孔盖油
- ##Kibana+ELK集群日志处理
- Quickly master asp Net authentication framework identity - user registration
- 使用 POST 向 ASP.NET Core 传递数据时的长度限制与解决方案
猜你喜欢

Come to Xiamen! Online communication quota free registration

Qt Notes - qmap Custom key

NLog自定义Target之MQTT

0基础如何入门软件测试,能转行成功吗?

ABP Framework 5.3.0 版本新增功能和变更说明
![[fpga+pwm] design and implementation of phase shift trigger circuit for three-phase PWM rectifier based on FPGA](/img/ad/c039932abe409d696380e8427173c0.png)
[fpga+pwm] design and implementation of phase shift trigger circuit for three-phase PWM rectifier based on FPGA

Application description of DAP fact table processing summary function

以小见大:一个领域建模的简单示例,理解“领域驱动”。

Hello playwright: (7) simulate keyboard and mouse

UI自动化定位利器-xpath实战
随机推荐
Database mysql master-slave scheme
UI自动化定位利器-xpath实战
DAP事实表加工汇总功能应用说明
clickhouse 21. X cluster four piece one copy deployment
Blazor University (30) form - derived from inputbase
Scala equality
Blazor University (30)表单 —— 从 InputBase 派生
Cross platform brake browser
client-go gin的简单整合十-Update
【FPGA+PWM】基于FPGA的三相PWM整流器移相触发电路的设计与实现
数据库mysql 主从方案
试用了多款报表工具,终于找到了基于.Net 6开发的一个了
WPF效果第一百九十篇之再耍ListBox
面试突击58:truncate、delete和drop的6大区别!
Parallel通过XCM与Moonbeam集成,将PARA以及DeFi用例带入Moonbeam生态
STM32系列(HAL库)——F103C8T6硬件SPI点亮带字库OLED屏
mysql-5.6.21-centos6.5源码安装配置
诺亚财富拟登陆港交所:第一季度业绩大幅下滑,曾踩雷“承兴案”
CMB model 23 ukey is not recognized on win7
Partage de l'architecture du système de paiement du Groupe letv pour traiter 100 000 commandes simultanées élevées par seconde