获得这些stuctsHttpServletRequest / HttpSession / ServletContext /
HttpServletResponse对象方法有两种:
第一种:直接得到对象方式
<1、编写Action
public class ServletObject {
public String rsa()
{
HttpServletRequest request=ServletActionContext.getRequest();
request.setAttribute("request", "Request");
ServletContext servletContext = ServletActionContext.getServletContext();
servletContext.setAttribute("application", "Application");
request.getSession().setAttribute("request", "Request");
HttpServletResponse response = ServletActionContext.getResponse();
return "scope";
}
}
<2、配置struts.xml文件
<action name="sevletScope" class="com.liyong.ServletInlayObject.ServletObject" method="rsa" >
<result name="scope">/WEB-INF/page/scope.jsp</result>
<!-- 访问路径 http://localhost:8080/Structs2/test/sevletScope -->
</action>
<3、编写scope.jsp得到写入的值
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'hello.jsp' starting page</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>
Application:${applicationScope.application}<br/>
Session:${sessionScope.session}<br/>
Request:${requestScope.request}<br/>
</body>
</html>
<4、部署
第二种:implements ServletRequestAware, ServletResponseAware, ServletContextAware这些接口在实现方法中得到对象
<1、编写Action
public class HelloWorldAction implements ServletRequestAware,
ServletResponseAware, ServletContextAware {
private HttpServletRequest request;
private ServletContext servletContext;
private HttpServletResponse response;
public void setServletRequest(HttpServletRequest request) {
this.request=request;
}
public void setServletResponse(HttpServletResponse response) {
this.response=response;
}
public void setServletContext(ServletContext servletContext) {
this.servletContext=servletContext;
}
public String rsa()
{
//HttpServletRequest request=ServletActionContext.getRequest();
request.setAttribute("request", "Request");
//ServletContext servletContext = ServletActionContext.getServletContext();
servletContext.setAttribute("application", "Application");
request.getSession().setAttribute("request", "Request");
//HttpServletResponse response = ServletActionContext.getResponse();
return "scope";
}
}
<2、配置strus.xml文件
<action name="sevlet2Scope" class="com.liyong.ServletInlayObject.HelloWorldAction" method="rsa" >
<result name="scope">/WEB-INF/page/scope.jsp</result>
<!-- 访问路径 http://localhost:8080/Structs2/test/sevletScope -->
</action>
<3、编写scope.jsp文件得到写入的值
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'hello.jsp' starting page</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>
Application:${applicationScope.application}<br/>
Session:${sessionScope.session}<br/>
Request:${requestScope.request}<br/>
</body>
</html>
<4、部署
.......