Action to access the SERVLETAPI.
The main thing is to receive form parameters and access values to the domain object.
There are three ways to Sevletapi in action:
1. Fully decoupled form:
* Use one of the classes: Actioncontext.
* Static Actioncontext getcontext (); ---get Actioncontext objects
* Map getparameters (); ---receives the arguments that are submitted to the action.
* Map getsession (); ---Get a map of the session.
* Map getapplication (); ---Get a map of application.
* void put (String key,object value); ---Save the value to the request.
* Object get (String key); ---Get the value from the request.
* Code Implementation :
Get Actioncontext Object
Actioncontext context = Actioncontext.getcontext ();
Get form submission Parameters:
map<string,object> map = Context.getparameters ();
String[] username = (string[]) map.get ("username");
System.out.println (Username[0]);
string[] Password = (string[]) map.get ("password");
System.out.println (Password[0]);
To store values in the request domain
Context.put ("Reqname", "Reqvalue");
System.out.println ("value in the request domain:" +context.get ("Reqname"));
To store values in the Session field
Context.getsession (). Put ("Sessname", "Sessvalue");
System.out.println ("Value in Session field:" +context.getsession (). Get ("Sessname"));
Save value to ServletContext
Context.getapplication (). Put ("AppName", "Appvalue");
System.out.println ("Value in the Application field:" +context.getapplication (). Get ("AppName"));
2. How to use interface injection:
* Implement Interface:
* Servletrequestaware
* Servletresponseaware
* Servletcontextaware
* Code implementation:
public class RequestAction2 extends Actionsupport implements servletrequestaware,servletcontextaware{
private HttpServletRequest request;
Private ServletContext context;
@Override
Public String Execute () throws Exception {
1. Receive parameters:
String username = request.getparameter ("username");
String Password = request.getparameter ("password");
SYSTEM.OUT.PRINTLN (username + "" +password);
2. Store the values in the request domain:
Request.setattribute ("Reqname", "Li Jian");
3. Store the values in the session field:
Request.getsession (). SetAttribute ("Sessname", "Feng elder sister");
4. Store the values in the application domain.
Context.setattribute ("AppName", "Hibiscus");
return SUCCESS;
}
public void Setservletrequest (HttpServletRequest request) {
This.request = Request;
}
public void Setservletcontext (ServletContext context) {
This.context = context;
}
}
3. Use the static method in Servletactioncontext:
Method:
* Getrequest ();
* GetResponse ();
* Getservetcontext ();
Code:
1. Receive parameters:
HttpServletRequest request = Servletactioncontext.getrequest ();
String username = request.getparameter ("username");
String Password = request.getparameter ("password");
System.out.println (username+ "" +password);
2. Store the values in the request domain:
Request.setattribute ("Reqname", "Kingjian");
3. Store the values in the session field:
Request.getsession (). SetAttribute ("Sessname", "Xiao Feng Feng");
4. Save the value to the application:
Servletactioncontext.getservletcontext (). SetAttribute ("AppName", "Xiao Rong Rong");
The first way is the best! Habit of the third kind of use is very convenient.
STRUTS2 servlet API access mode