码迷,mamicode.com
首页 > Windows程序 > 详细

Struts2之获取ServletAPI

时间:2018-08-12 00:29:05      阅读:159      评论:0      收藏:0      [点我收藏+]

标签:row   string   his   处理   write   contex   void   cat   over   

1.通过ServletActionContext类

//获取request对象
HttpServletRequest request = ServletActionContext.getRequest();
//保存值到application中
ServletActionContext.getActionContext(request).setApplication(new HashMap<>());

2.通过ServletRequestAware 接口

    public class UserAction extends ActionSupport implements ServletRequestAware {

    private String username;

    private String userpwd;

    private HttpServletRequest request;


    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getUserpwd() {
        return userpwd;
    }

    public void setUserpwd(String userpwd) {
        this.userpwd = userpwd;
    }

    @Override
    public void setServletRequest(HttpServletRequest arg0) {

        this.request = arg0;
    }

    public void executeAjax() throws IOException {

        // 处理Ajax请求

        String name = request.getParameter("username");
        
        String pwd = request.getParameter("userpwd");

        HttpServletResponse response = ServletActionContext.getResponse();
        
        response.getWriter().print("hello"+username+"hello"+userpwd);
        
    }

    }

Struts2之获取ServletAPI

标签:row   string   his   处理   write   contex   void   cat   over   

原文地址:https://www.cnblogs.com/jiangwenwen1/p/9461608.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!