Three ways to get SERVLETAPI in the action class in 5.struts2

Source: Internet
Author: User
Tags key string

Access to **servlet APIs (occasionally used in development) **1. In the action class, you can also get to the servlet some common APIs, there are three ways to get the * fully decoupled way * Using interface injection method * Use static methods in Servletactioncontext to access the api* requirements of the servlet directly: Provide data for the JSP's form page, receive it in the action using the Servlet's API, then save it to three domain objects, and then display it on the JSP page. * Provide JSP registration page, demonstrate the following three ways:

Demo4.jsp:

<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "pageencoding=" UTF-8 "%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >

Struts_demo4.xml:

<?xml version= "1.0" encoding= "UTF-8"? ><! DOCTYPE struts Public "-//apache software foundation//dtd struts Configuration 2.3//en" "http://struts.apache.org/dtds/ Struts-2.3.dtd "><struts><package name=" Demo4 "extends=" Struts-default "namespace="/"><!-- The fully decoupled way--><action name= "regist" class= "Demo4. Useraction "method=" regist "><result>/demo4/success.jsp</result></action><!--Interface Injection Method-- ><action name= "Regist2" class= "Demo4. UserAction2 "method=" Regist "><result>/demo4/success.jsp</result></action> <!-- Use the Servletactioncontext object--><action name= "Regist3" class= "Demo4". UserAction3 "method=" Regist "><result>/demo4/success.jsp</result></action></package> </struts>

Action:

Package Demo4;import Java.util.map;import Com.opensymphony.xwork2.actioncontext;import com.opensymphony.xwork2.actionsupport;/** * Fully decoupled way * @author MJL * */public class Useraction extends Actionsupport{publ IC String regist () {//Receive form data//Get to Actioncontext object Actioncontext context=actioncontext.getcontext ();//Get the requested parameter map <string, object> map = Context.getparameters ();//Get value by key string[] Username= (string[]) map.get ("username"); String[] password= (string[]) map.get ("password"); System.out.println ("User name:" +username[0]+ ", Password:" +password[0]);//Get sessionmap<string, object> sessionmap = Context.getsession ();//Like the map deposit specific values Sessionmap.put ("Sessname", "Meimei"); return SUCCESS;}} Package Demo4;import Javax.servlet.http.httpservletrequest;import Org.apache.struts2.interceptor.servletrequestaware;import Com.opensymphony.xwork2.actionsupport;public Class UserAction2 extends Actionsupport implements servletrequestaware{private httpservletrequest request;public void Setservletrequest (HttpServletRequest RequeST) {this.request=request;} Public String Regist () {string Username=request.getparameter ("username"); request.getsession (). SetAttribute (" Sessname ", username); return SUCCESS;}} Package Demo4;import Javax.servlet.http.httpservletrequest;import Org.apache.struts2.servletactioncontext;import com.opensymphony.xwork2.actionsupport;/** * Method of using Servletactioncontext Object * @author MJL * */public class UserAction3 Extends Actionsupport{public String regist () {HttpServletRequest request = Servletactioncontext.getrequest (); String Username=request.getparameter ("username"); request.getsession (). SetAttribute ("Sessname", username); return SUCCESS;}}

  

  

Three ways to get SERVLETAPI in the action class in 5.struts2

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.