当前位置:网站首页>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

  

    1. 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

 

  1. login.jsp User login interface

The implementation effect is shown in the figure below

 

  1. 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

  1. 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 .

 

  1. 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 .

      /img/92/da928fd87b8cdc8537159a8dad29bb.png

 

4.1 config object

   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 :

    /img/ec/c3570274e0345a342c94cc43d63915.png

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

    /img/dd/8505e9bf9095f5e5f363fc183a0f60.png

   visit : visit http://localhost:8080/Web_Jsp/exception.jsp

    /img/a6/625b7d2772e22d95a754c6f83ccdef.png

原网站

版权声明
本文为[xiao_ zhu_ ting_ feng]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/173/202206221522245655.html