Javax.Servlet.ServletContext interface

xiaoxiao2021-03-06  21

Javax.Servlet.ServletContext interface

The collection of all servlets, JSP, JavaBean and other web parts installed in a server (for example, / myapplication) constitute a web application, each web application (same jvm), there will be a container Background object, while Javax.Servlet.ServletContext interface provides a way to access this background object. GetServletContext method for servlet instance: get this background object to which the servlet runs. From this background object, you can access the following information or resources: (Note that the method is not a method of servletContext but the method for obtaining background objects Because the HTTPSERVLET inherits the Servlet's relationship GenericServlet class and HTTPSERVLET classes have this method):

· Initialization parameter servletContext.GetInitParameter (String name).

· Store objects in the back to CONTEXT.GETATTRIBUTE (String Name)

· Resources associated with this background servletContext.getResource (String Path)

· Log ServletContext.log (String MSG)

The methods shown above are provided for servletContext, it is worth mentioning that the object access method stored in the backbone is common:

Context.setttribute (String Name, Object Object); on any type of object that is bound to a particular name. The Object object will be bind to the name name, stored in the servlet background, which can be shared by other servlets in the same background. Other servlets can get objects in a background by context.getattribute (String Name), or remove an object in the background via context.removeattribute (String Name).

· GetRequestDispatcher method: Its role passes a String containing the path to other resources. This path is the root path relative to servletContext. Example:

RequestDispatcher Rd = Request.getRequestDispatcher ("SecondServlet");

Rd.Forward (Request, Response) ;.

This method is mainly used for server redirection techniques, and both the servlets are mainly two mainly different from the ServletContext interface and the servletRequest interface, but the same name method for both provided but the same name method GetRequestDispatcher is very different. In an ActionRouter class example //Req.getRequestDispatcher (RES.EncodeURL (ur1) ).Forward (REQ ,RES); indicated by the servletRequest interface providing method for redirection.

转载请注明原文地址:https://www.9cbs.com/read-40162.html

New Post(0)