获得这些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、部署
.......
获取HttpServletRequest / HttpSession
原创青年IT男1 博主文章分类:stucts 2学习笔记 ©著作权
文章标签 获取HttpServletRequest HttpSession ServletContext html 文章分类 后端开发
上一篇:文件上传
下一篇:自定义全局类型转换器
-
获取IP地址
端点(API接口)访问获取IP地址!
IP Java SpringBoot\ -
Struts Action里获取 HttpSession
struts2.0框架提供的session注入方法注入的是Map类型的session。如果要使用httpsession,可以考虑这个方法。
职场 休闲 struts2.0 httpsession -
【Servlet】多个Servlet之间数据共享实现方案(ServletContext/Cookie/HttpSession/HttpServletRequest)
多个Servlet之间数据共享实现方案文章目录多个Servlet之间数据共享实现方案前言一、ServletContext1、介绍2、工作原理3、全局作用域对象的生命周期4、代码实现5、ServletCoext原理图![请添加图片描述](https://s4.51cto.com/images/blog/202203/28015446_62
网络 HttpSession ServletContext Cookie 多个Servlet数据共享 -
springboot 预警熔断
最终结果报错原因是因为guava-15.0.jar的包下载错误,原因可能是我在下载过程中中断过,导致包不可用,但是也加载不到正确的包,把仓库里所有的父节点的包和自己本身多版本的号全删提再下载,一切正常,springBoot2.0.3直接在spring-cloud-starter-netflix-hystrix 有依赖,不需要去加其它包 下面是一步步排错的路线spr
springboot 预警熔断 hystix springBoot2.0.3 springCloud spring