在Action中使用Servlet API

本文介绍三种在Struts2框架中访问Servlet API的方法:使用ActionContext对象、实现特定接口进行注入及直接通过ServletActionContext获取。每种方法都附带示例代码和配置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.在action中以解耦合的方式来访问Servlet API——–使用ActionContext对象 
在Struts2中Action API 已经与Servlet API解耦合了 
Servlet API 常见操作:表单提交 请求参数,获取参数,向request、session、application三个范围内存取数据。 
ActionContext actionContext = ActionContext.getContext(); 
1)actionContext.getParameters(); 获取所有请求参数 
2)actionContext.put("company", "公司"); / actionContext.get("company") 对request范围内存取数据 
3)actionContext.getSession(); 获取session数据Map,对session范围内存取数据 
4)actionContext.getApplication(); 获取ServletContext数据Map,对应访问存取数据

例子: 
新建LoginAction.java

package com.cn.struts2.action;

import java.util.Map;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport{
    @Override
    public String execute() throws Exception {
        ActionContext actionContext = ActionContext.getContext();
        //1.获取请求参数
        Map<String,Object> par = actionContext.getParameters();
        Object value = par.get("username");
        System.out.println(value);
        //2.读取request的Attribute
        actionContext.put("company", "公司");
        System.out.println(actionContext.get("company"));
        //3.存取session的Attribute
        Map<String, Object> sessionMap = actionContext.getSession();
        sessionMap.put("age",20);
        System.out.println(sessionMap.get("age"));

        //4.存取Application的Attribute
        Map<String,Object> applicationMap = actionContext.getApplication();
        applicationMap.put("info", "yaoxiayu");
        System.out.println(applicationMap.get("info"));

        return SUCCESS;
    }
}

 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32

新建form.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">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
    <h1>Login</h1>
    <form action="${pageContext.request.contextPath}/login1.action" method="post">
        请输入您的姓名:<input type="text" name="username"/>
        <input type="submit" value="提交"/>
    </form>
</body>
</html>
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

新建success.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">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
    <h1>success</h1>
    <h6>${requestScope.company}</h6>
    <h6>${sessionScope.age}</h6>
    <h6>${applicationScope.info}</h6>
</body>
</html>
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

在struts.xml中添加

<action name="login1"   class="com.cn.struts2.action.LoginAction">
            <result name="success">/jsp/success.jsp</result>
        </action>
 
  • 1
  • 2
  • 3

访问http://localhost:8080/web/jsp/from.jsp 输入名字然后点击提交,跳转到success页面,页面显示success、公司、20、yaoxiayu,即表示成功。

2.使用接口注入的方式,操作Servlet API

ServletContextAware(接口):注入ServletContext对象 
ServletRequestAware(接口): 注入request对象 
ServletResponseAware(接口):注入response对象 
使用哪个接口就实现哪个接口即可。 
例子: 
新建LoginAction2.java

package com.cn.struts2.action;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.util.ServletContextAware;

import com.opensymphony.xwork2.ActionSupport;

public class LoginAction2 extends ActionSupport implements ServletRequestAware,ServletContextAware{
    private HttpServletRequest request;
    private ServletContext context; 
    @Override
    public String execute() throws Exception {
        //获取请求参数
        System.out.println(request.getParameter("username"));
        //存取request
        request.setAttribute("company", "公司");
        //存取session
        request.getSession().setAttribute("age", 20);
        //存取Application
        context.setAttribute("info", "下雨了");
        return SUCCESS;
    }
    @Override
    //struts2会自动将request对象设置进来
    public void setServletRequest(HttpServletRequest request) {
        this.request = request;
    }
    @Override
    public void setServletContext(ServletContext context) {
        this.context = context;
    }
}

 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36

新建form2.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">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>  
    <h1>Login2</h1>
    <form action="${pageContext.request.contextPath}/login2.action" method="post">
        请输入您的姓名:<input type="text" name="username"/>
        <input type="submit" value="提交"/>
    </form>
</body>
</html>
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

在struts.xml中添加配置

<action name="login2"   class="com.cn.struts2.action.LoginAction2">
            <result>jsp/success.jsp</result>
        </action>
 
  • 1
  • 2
  • 3

访问http://localhost:8081/web/jsp/form2.jsp 然后输入姓名点击提交,跳转到新页面显示如下图即表示成功了。 
这里写图片描述

3.在Action中直接通过ServletActionContext获得Servlet API 
ServletActionContext.getRequest() : 获得request对象(session) 
ServletActionContext.getResponse() :获得response对象 
ServletActionContext.getServletContext() :获得ServletContext对象 
静态方法没有线程问题,ThreadLocal

例子: 
新建LoginAction3.java类

package com.cn.struts2.action;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

public class LoginAction3 extends ActionSupport{
    @Override
    public String execute() throws Exception {
        //1.获取请求参数
        System.out.println(ServletActionContext.getRequest().getParameter("username"));
        //2.保存request范围数据
        ServletActionContext.getRequest().setAttribute("company", "公司");
        // 3 保存session 范围数据
        ServletActionContext.getRequest().getSession().setAttribute("age", 19);
        // 4 保存application范围数据
        ServletActionContext.getServletContext().setAttribute("info", "雨很大");
        return SUCCESS;
    }
}


 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

新建form3.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">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>  
    <h1>Login3</h1>
    <form action="${pageContext.request.contextPath}/login3.action" method="post">
        请输入您的姓名:<input type="text" name="username"/>
        <input type="submit" value="提交"/>
    </form>
</body>
</html>
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

在struts.xml配置文件中添加

<action name="login3"   class="com.cn.struts2.action.LoginAction3">
            <result>jsp/success.jsp</result>
        </action>
 
  • 1
  • 2
  • 3

访问http://localhost:8081/web/jsp/form3.jsp 然后输入姓名点击提交,跳转到新页面显示如下图即表示成功了。 
这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值