In Java, there are a lot of common Context,
Like: ServletContext, Actioncontext, Servletactioncontext, ApplicationContext, PageContext, SessionContext ...
So, what is the Context? Literal translation is the meaning of context and environment. For example: "Today I received a bouquet of flowers, my boyfriend sent!" or "Today I received a bouquet of flowers, the flowers sent the wrong!"
Also received a bouquet of flowers, in different contexts to express the meaning is not the same.
Similarly, the context is actually the same, it can not be separated from the environment, otherwise it is taken out.
In addition, on the network Some people regard the Context as a public information or as a container, the individual feel that this explanation is slightly better.
Next talk about ServletContext, Actioncontext, Servletactioncontext.
1> ServletContext
A Web application has only one ServletContext instance, which is created before the container (including JBoss, TOMCAT, etc.) starts the Web project completely, and the lifecycle accompanies the entire Web application.
When writing a Servlet class, the first thing to do is to inherit an abstract class HttpServlet, and then you can get the ServletContext object directly through the Getservletcontext () method.
This is because the ServletConfig interface is implemented in the HttpServlet class, and a reference to the ServletContext object is maintained in the ServletConfig interface.
Use ServletContext to get configuration information for WEB usage, to share data among multiple servlets, and so on.
eg
<?xml version= "1.0" encoding= "UTF-8"?>
<context-param>
<param-name>url</param-name>
<param-value>jdbc:oracle:thin: @localhost:1521:orc</param-value>
</context-param>
<context-param>
<param-name>username</param-name>
<param-value>scott</param-value>
</context-param>
<context-param>
<param-name>password</param-name>
<param-value>tigger</param-value>
</context-param>
<servlet>
<servlet-name>ConnectionServlet</servlet-name>
<servlet-class>net.yeah.fancydeepin.servlet.ConnectionServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ConnectionServlet</servlet-name>
<url-pattern>/ConnectionServlet.action</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>PrepareConnectionServlet</servlet-name>
<servlet-class>net.yeah.fancydeepin.servlet.PrepareConnectionServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>PrepareConnectionServlet</servlet-name>
<url-pattern>/PrepareConnectionServlet.action</url-pattern>
</servlet-mapping>
</web-app>
Package net.yeah.fancydeepin.servlet;
Import java.io.IOException;
Import Javax.servlet.ServletContext;
Import javax.servlet.ServletException;
Import Javax.servlet.http.HttpServlet;
Import Javax.servlet.http.HttpServletRequest;
Import Javax.servlet.http.HttpServletResponse;
public class Prepareconnectionservlet extends HttpServlet {
Private static final long serialversionuid = 1L;
public void Init () throws Servletexception {
ServletContext context = Getservletcontext ();
String url = context.getinitparameter ("url");
String username = context.getinitparameter ("username");
String Password = context.getinitparameter ("password");
Context.setattribute ("url", url);
Context.setattribute ("username", username);
Context.setattribute ("password", password);
}
protected void doget (HttpServletRequest request,httpservletresponse response) throws servletexception,ioexception{
DoPost (request, response);
}
protected void DoPost (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException {
Response.sendredirect ("Connectionservlet.action");
}
}
Package net.yeah.fancydeepin.servlet;
Import java.io.IOException;
Import Javax.servlet.ServletContext;
Import javax.servlet.ServletException;
Import Javax.servlet.ServletRequest;
Import Javax.servlet.ServletResponse;
Import Javax.servlet.http.HttpServlet;
public class Connectionservlet extends HttpServlet {
Private static final long serialversionuid = 1L;
public void Service (ServletRequest request, servletresponse response) throws Servletexception, IOException {
ServletContext context = Getservletcontext ();
System.out.println ("***************************************");
System.out.println ("URL:" + context.getattribute ("url"));
System.out.println ("Username:" + context.getattribute ("Username"));
System.out.println ("Password:" + context.getattribute ("Password"));
System.out.println ("***************************************");
Super.service (request, response);
}
}
When accessing prepareconnectionservlet.action, the spool output:
***********************************************
URL:jdbc:oracle:thin: @localhost: 1521:orc
Username:scott
Password:tigger
***********************************************
2> Actioncontext
Actioncontext is the context of the current action execution, and some references to objects related to the current action are maintained in Actioncontext.
such as: Parameters (Parameters), session (sessions), Valuestack (value stack), Locale (localization information), etc.
During the Struts1 period, Struts1 's Action was tightly coupled with the Servlet API and JSP technology, and belonged to an intrusive framework:
Public Actionforward Execute (actionmapping mapping,actionform form,httpservletrequest request,httpservletresponse Response) {
TODO auto-generated Method Stub
return null;
}
By the time of Struts2, there was a big difference between the architecture of Struts2 and Struts1. Struts2 was integrated with webwork on the basis of Struts1 and became a new framework.
In Struts2, the data information associated with the servlet is transformed into an object unrelated to the servlet API, the Actioncontext object, through WebWork.
This allows the business logic controller to be detached from the Servlet API. Also, since Struts2 's Action is a new instance of every user request,
Actioncontext is not a thread-safe issue and can be used with confidence.
Package net.yeah.fancydeepin.action;
Import Java.util.Map;
Import Com.opensymphony.xwork2.ActionContext;
Import Com.opensymphony.xwork2.ActionSupport;
Import Com.opensymphony.xwork2.util.ValueStack;
public class Contextaction extends Actionsupport {
Private static final long serialversionuid = 1L;
Private String username;
private String password;
Public String execute () {
Actioncontext context = Actioncontext.getcontext ();
Valuestack value = Context.getvaluestack ();
Value.set ("username", username);
Value.set ("password", password);
Map<string, object> session = Context.getsession ();
Session.put ("url", "Http://www.blogjava.net/fancydeepin");
return SUCCESS;
}
public void Setusername (String username) {
This.username = Username;
}
public void SetPassword (String password) {
This.password = password;
}
}
<s:property value= "username"/><br>
<s:property value= "Password"/><br>
<s:property value= "#session. url"/><br>
When the context.action is accessed and passed to the corresponding parameters, the corresponding information is output in the browser.
Notice that the Action above Struts2 has no attribute added to the Getting method, but instead manually put the parameter value in the value stack (valuestack), otherwise the page will not be output parameters.
3> Servletactioncontext
First, Servletactioncontext is a subclass of Actioncontext. Servletactioncontext, in its name, means that it is tightly coupled to the Servlet API.
The constructors of the
servletactioncontext are private, mainly providing some static methods that can be used to obtain: Actioncontext, actionmapping, PageContext,
httpservletrequest, HttpServletResponse, ServletContext, Valuestack, HttpSession object references.
Public String execute () {
or implements Servletrequestaware
HttpServletRequest request = Servletactioncontext.getrequest ();
or implements Servletresponseaware
HttpServletResponse response = Servletactioncontext.getresponse ();
or implements Sessionaware
HttpSession session = Request.getsession ();
or implements Servletcontextaware
ServletContext context = Servletactioncontext.getservletcontext ();
return SUCCESS;
}
Spring Context Object