JavaWeb - ServletContext java class acquisition method ServletContext

https://www.jianshu.com/p/31d27181d542

 

  Cause I want to get a relative path, the need to use servletContext getRealPath () method, so the Internet search to find two ways to get ServletContext.

  Method 1: The first method is such that:

  ServletActionContext.getServletContext();

  Or ServletContext servletContext = req.getServletContext (); where req is a HttpServletRequest object.

  The first two methods described above using Struts limited, and the two methods is only in the event of a request to obtain the web ServletContext object.

  方法2:WebApplicationContext webApplicationContext = ContextLoader.getCurrentWebApplicationContext();    

  ServletContext servletContext = webApplicationContext.getServletContext();

  This approach to the object by obtaining servletContext spring containers, can be acquired. In fact, because the web project at startup, will WebApplicationContext injected into the ServletContext, but also retains a reference to the ServletContext as properties.

 

ServletContext, HttpSession, HttpServletRequest three range comparison attribute space domain

https://blog.csdn.net/w_linux/article/details/79770990

 

 

 

request < session < sevelet

So you can get to know the request session to which he belongs, the same session where he can get sevelet.

So, how to obtain the request is critical. Before an article that is of this.

(获取httpservletRequest对象)
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder
.getRequestAttributes()).getRequest();

Internet, said basically this method. This code is more important

 

 

  Cause I want to get a relative path, the need to use servletContext getRealPath () method, so the Internet search to find two ways to get ServletContext.

  Method 1: The first method is such that:

  ServletActionContext.getServletContext();

  Or ServletContext servletContext = req.getServletContext (); where req is a HttpServletRequest object.

  The first two methods described above using Struts limited, and the two methods is only in the event of a request to obtain the web ServletContext object.

  方法2:WebApplicationContext webApplicationContext = ContextLoader.getCurrentWebApplicationContext();    

  ServletContext servletContext = webApplicationContext.getServletContext();

  This approach to the object by obtaining servletContext spring containers, can be acquired. In fact, because the web project at startup, will WebApplicationContext injected into the ServletContext, but also retains a reference to the ServletContext as properties.

 

ServletContext, HttpSession, HttpServletRequest three range comparison attribute space domain

https://blog.csdn.net/w_linux/article/details/79770990

 

 

 

request < session < sevelet

So you can get to know the request session to which he belongs, the same session where he can get sevelet.

So, how to obtain the request is critical. Before an article that is of this.

(获取httpservletRequest对象)
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder
.getRequestAttributes()).getRequest();

Internet, said basically this method. This code is more important

 

 

Guess you like

Origin www.cnblogs.com/cuiqq/p/11906665.html