Get the App
SLTechnology News&Howtos  ›  Servers  › 

Detailed introduction of ServletContext

Shulou Source: shulou.com Published: 2022-06-01 16:32:56 09月19日 Update

This article mainly explains "detailed introduction of ServletContext". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn the detailed introduction of ServletContext.

First, when the WEB container starts, it creates a corresponding ServletContext object for each WEB application, which represents the current WEB application.

Second, the ServletContext object can obtain the reference to the Servlet object through the ServletConfig.getServletContext method, and it can also obtain the reference to its object through the this.getServletContext90.

3. Because all Servlet in a WEB application share the same ServletContext object, Servlet objects can communicate with each other through ServletContext object. The ServletContext object is also commonly referred to as the context domain object. It is used in public chat rooms.

4. How to use ServletContext?

1. How do I get a ServletContext object?

ServletContext sc=this.getServletContext ()

two。 Think of ServletContext as a table, which is very similar to session.

Add attribute: setAttribute (String name,Object ob)

Value obtained: getAttribute (String name)

Delete attribute: removedAttribute (String name)

3. Life cycle

The life cycle of properties in ServletContext starts from creation to the end of server shutdown

5. ServletContext application

1. Can be shared by multiple servlet in the project

two。 Gets the initialization parameters of the wenb application. For example, we have a code like this in web.xml

Name

Scott

If it is a brotherly relationship with the element

String value=this.getServletContext () .getInitParameter (name)

The result value of value is scott

If it's the son of the element,

String value=this.getServletConfig () .getIn.getInitParameter (name)

The value of value is also scott.

3. Can request forwarding

(1) response.sendRedirect ("/ web application name / resource name")

(2) request.getRequestDispatcher ("/ resource name") .forward (request,response) = = this.getServletContext.getRequestDispatcher ("/ resource name") .forward (request,response)

4. Read the resource file of web application

(1) Resource files are written under WebRoot

1 > read the contents of the resource file, for example, there is a msql.properties file

The contents of the document are as follows:

Username=scott

Password=123

How to read:

InputStream is=this.getServletContext () .getresourceAsStream ("msql.properties"); / / read to a file

Propreties pp=new Propreties ()

Pp.load (is)

System.out.println ("name=" + pp.getProperty ("username")); / / the result is scott

2 > read the full path of the file, for example, there is a / imgs/1.jpg file under webRoot

String Path=this.getServletContext () .getRealPath ("/ imgs/1.jpg")

(2) use class loader for resource files under src

Randomly pick a class under the src file, such as: Servlet.java

InputStream is=Servlet.class.getClassloader.getResourceAsStream ("File name")

At this point, I believe that you have a deeper understanding of the "detailed introduction of ServletContext", you might as well come to the actual operation! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Tags: Files objects resources applications content can be passed attributes results elements cycles methods life learning practical deeper similar under between code representative Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia Microsoft Shulou Tech Info MySQL Shulou Information