STRUTS2 servlet API access mode

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.