Action类:
import java.io.UnsupportedEncodingException;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
/**
* 获取Servlet API
* @author 够潮
*
*/
@SuppressWarnings("serial")
public class GetServletApiAction extends ActionSupport {
/**
* 用户登录
*/
public void loginAction(){
/**
* 获取Action上下文
*/
ActionContext ac = ActionContext.getContext();
/**
* 获取Appication
*/
Map map = ac.getApplication();
/**
* 获取Session
*/
Map sessionMap = ac.getSession();
/**
* 获取request
*/
HttpServletRequest request = ServletActionContext.getRequest();
/**
* 获取response
*/
HttpServletResponse response = ServletActionContext.getResponse();
/**
* 获取前台传过来的参数
*/
String userName = request.getParameter("userName");
String password = request.getParameter("password");
try {
/**
* 设置编码格式
*/
request.setCharacterEncoding("utf-8");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("userName"+userName);
System.out.println("password:"+password);
ActionContext ac2 = ServletActionContext.getActionContext(request);
System.out.println("ac2== ac --->"+(ac2==ac));
System.out.println("sessionMap == map--->"+(sessionMap == ac2.getSession()));
}
}
页面:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>在Action中获取Servlet Api 专题</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<center>
<h3>在Action中获取Servlet Api 专题</h3>
<form action="servletApiAction!loginAction.action">
userName:<input type="text" name="userName"><br>
password:<input type="password" name="password"><br>
<input type="submit" >
</form>
</center>
</body>
</html>
配置文件:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="admin" namespace="/" extends="struts-default">
<!-- ServletapiAction -->
<action name="servletApiAction" class="action.GetServletApiAction">
</action>
</package>
</struts>
测试:
- 大小: 46.7 KB
- 大小: 31.1 KB
分享到:
相关推荐
AnyFo - Util - ... AnyFoAction中的那个类,提供多个方法来访问Servlet中的API,这些方法能提供的功能大致如下: 1.获得HttpServletResponse 2.获得HttpSession 3.获得ServletContext,即俗称的Application
Struts 2以WebWork为核心,采用拦截器的机制来处理用户的请求,这样的设计也使得业务逻辑控制器能够与Servlet API完全脱离开,所以Struts 2可以理解为WebWork的更新产品。虽然从Struts 1到Struts 2有着太大的变化,...
[1]相对于2007年发布的Struts 2.0,Struts 2.1改变较大,Struts 2.1不仅修正了Struts 2.0中少量Bug(这些Bug在本书第一版中已经指出)。而且新增了REST、Convention和Java Templates,这些都是Struts 2未来的发展...
相对于2007年发布的Struts 2.0,Struts 2.1改变较大,Struts 2.1不仅修正了Struts 2.0中少量Bug(这些Bug在本书第一版中已经指出)。而且新增了REST、Convention和Java Templates,这些都是Struts 2未来的发展方向...
相对于2007年发布的Struts 2.0,Struts 2.1改变较大,Struts 2.1不仅修正了Struts 2.0中少量Bug(这些Bug在本书第一版中已经指出)。而且新增了REST、Convention和Java Templates,这些都是Struts 2未来的发展方向...
相对于2007年发布的Struts 2.0,Struts 2.1改变较大,Struts 2.1不仅修正了Struts 2.0中少量Bug(这些Bug在本书第一版中已经指出)。而且新增了REST、Convention和Java Templates,这些都是Struts 2未来的发展方向...
相对于2007年发布的Struts 2.0,Struts 2.1改变较大,Struts 2.1不仅修正了Struts 2.0中少量Bug(这些Bug在本书第一版中已经指出)。而且新增了REST、Convention和Java Templates,这些都是Struts 2未来的发展方向...
相对于2007年发布的Struts 2.0,Struts 2.1改变较大,Struts 2.1不仅修正了Struts 2.0中少量Bug(这些Bug在本书第一版中已经指出)。而且新增了REST、Convention和Java Templates,这些都是Struts 2未来的发展方向...
相对于2007年发布的Struts 2.0,Struts 2.1改变较大,Struts 2.1不仅修正了Struts 2.0中少量Bug(这些Bug在本书第一版中已经指出)。而且新增了REST、Convention和Java Templates,这些都是Struts 2未来的发展方向...
相对于2007年发布的Struts 2.0,Struts 2.1改变较大,Struts 2.1不仅修正了Struts 2.0中少量Bug(这些Bug在本书第一版中已经指出)。而且新增了REST、Convention和Java Templates,这些都是Struts 2未来的发展方向...
相对于2007年发布的Struts 2.0,Struts 2.1改变较大,Struts 2.1不仅修正了Struts 2.0中少量Bug(这些Bug在本书第一版中已经指出)。而且新增了REST、Convention和Java Templates,这些都是Struts 2未来的发展方向...
相对于2007年发布的Struts 2.0,Struts 2.1改变较大,Struts 2.1不仅修正了Struts 2.0中少量Bug(这些Bug在本书第一版中已经指出)。而且新增了REST、Convention和Java Templates,这些都是Struts 2未来的发展方向...
相对于2007年发布的Struts 2.0,Struts 2.1改变较大,Struts 2.1不仅修正了Struts 2.0中少量Bug(这些Bug在本书第一版中已经指出)。而且新增了REST、Convention和Java Templates,这些都是Struts 2未来的发展方向...
相对于2007年发布的Struts 2.0,Struts 2.1改变较大,Struts 2.1不仅修正了Struts 2.0中少量Bug(这些Bug在本书第一版中已经指出)。而且新增了REST、Convention和Java Templates,这些都是Struts 2未来的发展方向...
相对于2007年发布的Struts 2.0,Struts 2.1改变较大,Struts 2.1不仅修正了Struts 2.0中少量Bug(这些Bug在本书第一版中已经指出)。而且新增了REST、Convention和Java Templates,这些都是Struts 2未来的发展方向...
相对于2007年发布的Struts 2.0,Struts 2.1改变较大,Struts 2.1不仅修正了Struts 2.0中少量Bug(这些Bug在本书第一版中已经指出)。而且新增了REST、Convention和Java Templates,这些都是Struts 2未来的发展方向...
主要对WEB框架常面试的点做了一下整理,希望可帮助到你,下面文档的部分内容 Java---SSH(MVC) 1. 谈谈你mvc的理解 ... Struts2.0 Action可以通过初始化、设置属性、调用方法来测试,“依赖注入”也使得测试更容易。
有java api中英文版,Ajax_XMLHttpRequest帮助文档,Apache+Ant+1.7,Comm2.0_参考手册,DHTML_网页制作完全手册_中文,DHTML文档对象模型中文手册,DOS_练就高手,Hibernate3.1.2_帮助文档_中文,Hibernate3.2_帮助...
集中了J2EE开发中用到的几种中文API参考文档包括JDK_API_1_6_zh_CN、Hibernate 中文api文档、java web 标签大全、jquery1.7.2_2API文档、Servlet+API[China]、struts2.0中文帮助手册、w3school
HTML参考手册 css2.0中文手册 J2EE全实例教程 Javascript参考手册 Jsp帮助文档 JSTL中英文对照版 servlet api 中文版chm格式 JAVA设计模式 Hibernate 2.1 中文参考手册 EL表达式 Tomcat中文帮助 掌握自定义JSP标签 ...