核心思想

1、ActionContext

HttpServletRequest getAttribute setAttribute

ActionContext get put

//ActionContext.getContext() 获取入口
ActionContext.getContext().put("key", "value");

2、ServletRequestAware

package com.test.action;

public class LoginAction extends ActionSupport implements ServletRequestAware
{
private static final long serialVersionUID = -74906200993380354L;
private HttpServletRequest request; @SuppressWarnings("unchecked")
public String execute() throws Exception
{
if("james".equals(this.getUsername()) && "james".equals(this.getPassword())){
Map map = ActionContext.getContext().getSession();
//ActionContext.getContext() 获取入口
//ActionContext.getContext().put("james", "get from AcionContext");
request.setAttribute("chenkai", "from ServletRequestAware");
map.put("user", "james");
return "success";
}else {
this.addFieldError("username", "username or password is wrong");
return "failer";
}
} /*
* 实现 ServletRequestAware 接口 方法
* struts2 自动加载,【ioc 依赖注入的方式】 将与容器相关的request参数set到应用里
*/
public void setServletRequest(HttpServletRequest request) {
this.request = request;
} }

3、ServletResponseAware 与 Cookie 的使用

public class LoginAction extends ActionSupport implements ServletResponseAware
{
private HttpServletResponse response;
public String execute() throws Exception
{ Cookie cookie = new Cookie("name", "james");
cookie.setMaxAge(100);
response.addCookie(cookie);
return SUCCESS;
}
}
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
</head>
<body>
username:${requestScope.username }<br>
password:${requestScope.password }<br>
Cookie:${cookie.name }
</body>
</html>

4、ServletActionContext

    public String execute() throws Exception
{
HttpServletResponse response = ServletActionContext.getResponse();
Cookie cookie = new Cookie("name", "james");
cookie.setMaxAge(100);
response.addCookie(cookie);
return SUCCESS;
}

建议使用顺序   

a) ActionContext 【首选】

b) ServletActionContext [如果需要使用reponse]

c) ServletRequestAware [必须实现接口]

java学习笔记(10) —— ActionContext、ServletActionContext、ServletRequestAware用法的更多相关文章

  1. Java学习笔记---继承和super的用法

    自从换了个视频教学,感觉比原来那个好多了,就是学校网速太渣,好多视频看一会卡半天,只能先看看已经下载的了. 不过也好,虽然不能从开始开始重新开,但是已经看过一次,在看一次也是好的,就当巩固学习了. 继 ...

  2. Java 学习笔记(10)——容器

    之前学习了java中从语法到常用类的部分.在编程中有这样一类需求,就是要保存批量的相同数据类型.针对这种需求一般都是使用容器来存储.之前说过Java中的数组,但是数组不能改变长度.Java中提供了另一 ...

  3. Java学习笔记10

    31.编写当年龄age大于13且小于18时结果为true的布尔表达式age > 13 && age < 18 32.编写当体重weight大于50或身高大于160时结果为t ...

  4. Java学习笔记10(面向对象三:接口)

    接口: 暂时可以理解为是一种特殊的抽象类 接口是功能的集合,可以看作是一种数据类型,是比抽象类更抽象的"类" 接口只描述所应该具备的方法,并没有具体实现,具体实现由接口的实现类(相 ...

  5. Java学习笔记-10.io流

    1.输入流,只能从中读取数据,而不能向其写出数据.输出流,只能想起写入字节数据,而不能从中读取. 2.InputStream的类型有: ByteArrayInputStream 包含一个内存缓冲区,字 ...

  6. Java学习笔记10(面对对象:构造方法)

    在开发中经常需要在创建初始化对象时候明确对象的属性值, 比如Person对象创建的时候就给Person的属性name,age赋值, 这里就要用到构造方法: 构造方法是类的一种特殊方法,它的特殊性体现在 ...

  7. Java学习笔记31(IO:Properties类)

    Properties类,表示一个持久的j集,可以存在流中,或者从流中加载 是Hashtable的子类 map集合的方法都能用 用途之一:在开发项目中,我们最后交给客户的是一个编译过的class文件,客 ...

  8. 《Java学习笔记(第8版)》学习指导

    <Java学习笔记(第8版)>学习指导 目录 图书简况 学习指导 第一章 Java平台概论 第二章 从JDK到IDE 第三章 基础语法 第四章 认识对象 第五章 对象封装 第六章 继承与多 ...

  9. Java学习笔记4

    Java学习笔记4 1. JDK.JRE和JVM分别是什么,区别是什么? 答: ①.JDK 是整个Java的核心,包括了Java运行环境.Java工具和Java基础类库. ②.JRE(Java Run ...

随机推荐

  1. 构造AJAX参数, 表单元素JSON相互转换

    ajax提交服务器数据, 整理一下转换方法. HTML: <form id="fm" name="fm" action=""> ...

  2. HDOJ 1019 Least Common Multiple(最小公倍数问题)

    Problem Description The least common multiple (LCM) of a set of positive integers is the smallest po ...

  3. Maven搭建环境(Linux& Windows)

    Linux下安装Maven 1.前提条件: 1)下载并安装好JDK .在终端输入命令“java -version”,如果出现类似如下信息说明JDK安装成功. $ java -version java ...

  4. UVA11995【I can guess the data structrue!!】【水】+UVA11991【map用法】

    先看UVA11995 两份代码一份直接用C写的,一份用STL写的 #include <iostream> #include <stdio.h> #include <str ...

  5. redis: 6379端口下set值时出现 CLUSTERDOWN The cluster is down

    1.使用./redis-trib.rb check 192.168.242.134:6379检查出现图中的错误 2.在redis安装目录的bin下执行下列命令去cluster meet 另一个端口为7 ...

  6. C++ —— 库函数的 语法解析

    1.__declspec 用法总结 链接:http://blog.chinaunix.net/uid-24517893-id-2749061.html 详解2:http://www.01yun.com ...

  7. iOS--导航栏样式

    push返回按钮样式: UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBar ...

  8. WebView redirect https to http

    最新项目大改版,刚好对相关sdk版本做了下升级,target也从19升级到21. 意外发现原先在WebView中加载的网页中的图片全都变得一片白,连默认图片都不给显示. 经过一番测试才发现是由于tar ...

  9. UVa 340 Master-Mind Hints (优化查找&复制数组)

    340 - Master-Mind Hints Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_on ...

  10. mysql中判断字段为空

    mysql中判断字段为null或者不为null   在mysql中,查询某字段为空时,切记不可用 = null, 而是 is null,不为空则是 is not null   select nulco ...