获取HttpServletRequest / HttpSession / ServletContext / HttpServletResponse对象
程序员文章站
2022-04-23 16:55:56
...
获得这些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、部署
.......
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、部署
.......
上一篇: JSP九大隐式(内置)对象详解(转)
下一篇: session的生命周期
推荐阅读
-
SpringBoot实现任意位置获取HttpServletRequest对象
-
spring 获取HttpSession ,HttpServletRequest ,HttpServletResponse
-
javaEE ServletContext, 域对象, 获取绝对路径, servlet共享数据
-
strust2 获取HttpServletResponse对象
-
获取HttpServletRequest / HttpSession / ServletContext / HttpServletResponse对象
-
在Servlet使用getServletContext()获取ServletContext对象出现java.lang.NullPointerException(空指针)异常的解决办法
-
在Servlet使用getServletContext()获取ServletContext对象出现java.lang.NullPointerException(空指针)异常的解决办法...
-
strust2 获取HttpServletResponse对象
-
SpringBoot实现任意位置获取HttpServletRequest对象