Action间接、直接访问Servlet API
2017-07-31 14:57
357 查看
一、间接访问:
需要经常访问的Servlet API就是HttpServletRequest 、HttpSession 、ServletContext三个类,分别代表着jsp内置对象的request、session、application
struts2 提供一个类ActionContext(当前Action上下文对象,通过这个类可以访问Servlet API。包含以下几个常用方法:
Map getApplication();
static ActionContext Context();
Map getParameters();
Map getSesstion();
void set Apptioncatoin(Map application);
案例1:用户登录验证:
longin2.jsp页面:
Action:LoginActoin2页面:
welcome2.jsp页面:
二、直接访问:
方式1):实现相应的接口
ServletContextAware : 实现该接口的Action可以直接访问ServletContext 对象,Action 必须提供该接口的void setServletContext(ServletContext servletcontext)方法
ServletRequestAware :
ServlerResponseAware:
SessionAware:
修改以上Action中的代码:
方式2)struts2提供了ServletActionContext类:(直接访问中最常用,代码简洁)
有以下几个静态方法:
static PageContext getPageContext() ;取得Web应用的PageContext对象
static HttpServletRequest getRequest();…………
static HttpServletResponse gerResponse;…………
static ServletContext geteServletContext();…………
修改Action代码:
需要经常访问的Servlet API就是HttpServletRequest 、HttpSession 、ServletContext三个类,分别代表着jsp内置对象的request、session、application
struts2 提供一个类ActionContext(当前Action上下文对象,通过这个类可以访问Servlet API。包含以下几个常用方法:
Map getApplication();
static ActionContext Context();
Map getParameters();
Map getSesstion();
void set Apptioncatoin(Map application);
案例1:用户登录验证:
longin2.jsp页面:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <%@ taglib prefix = "s" uri = "/struts-tags" %> <% 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>My JSP 'login2.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> <s:form action = "login2" method = "post"> <s:textfield name = "username" label = "用户名"></s:textfield> <s:password name = "password" label = "密码"></s:password><br><br> <s:submit value = "提交"></s:submit> </s:form> </body> </html>
Action:LoginActoin2页面:
package com.action; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport; public class LoginAction2 extends ActionSupport{ private String username; private String password; public String execute() { ActionContext actionContext = ActionContext.getContext(); //静态方法; if(username.equals("admin") && password.equals("admin")){ actionContext.getSession().put("username", username); actionContext.getSession().put("password", password); return SUCCESS; } return LOGIN; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
welcome2.jsp页面:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <%@ taglib prefix = "s" uri = "/struts-tags" %> <% 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>My JSP 'welcome2.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=" 4000 description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> 欢迎<s:property value = "#session.username"/>访问此页面! </body> </html>
二、直接访问:
方式1):实现相应的接口
ServletContextAware : 实现该接口的Action可以直接访问ServletContext 对象,Action 必须提供该接口的void setServletContext(ServletContext servletcontext)方法
ServletRequestAware :
ServlerResponseAware:
SessionAware:
修改以上Action中的代码:
package com.action; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; import org.apache.struts2.interceptor.ServletRequestAware; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport; public class LoginAction2 extends ActionSupport implements ServletRequestAware{ private String username; private String password; private HttpServletRequest servletRequest; public String execute() { HttpSession session = servletRequest.getSession(); if(username.equals("admin") && password.equals("admin")){ session.setAttribute("username", username); session.setAttribute("password", password); return SUCCESS; } return LOGIN; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public HttpServletRequest getServletRequest() { return servletRequest; } public void setServletRequest(HttpServletRequest servletRequest) { this.servletRequest = servletRequest; }//要使用HttpServletRequest对象,必须实现该方法 }
方式2)struts2提供了ServletActionContext类:(直接访问中最常用,代码简洁)
有以下几个静态方法:
static PageContext getPageContext() ;取得Web应用的PageContext对象
static HttpServletRequest getRequest();…………
static HttpServletResponse gerResponse;…………
static ServletContext geteServletContext();…………
修改Action代码:
public class LoginAction2 extends ActionSupport{ private String username; private String password; public String execute() { if(username.equals("admin") && password.equals("admin")){ ServletActionContext.getRequest().getSession().setAttribute("username", username); return SUCCESS; } return LOGIN; }
相关文章推荐
- Action中直接访问Servlet API
- Struts2(5):Action中直接访问Servlet API---实现XxxAware接口
- 3.5.3: Action直接访问Servlet API
- ACtion直接访问Servlet API中的ServletRequest对象
- Struts2间接访问Servlet API(ActionContex--一般推荐使用)
- Structs2 Action访问Servlet API的三种方式
- 牛客网Java刷题知识点之父类中的私有内容,子类是否具备? 子类不可直接,但可间接访问父类中的私有内容?
- Struts2中Action访问Servlet API
- Struts2的Action控制类的实现及Action访问Servlet API
- struts2 在action访问Servlet API
- 使用ServletActionContext访问Servlet API
- Action直接访问ServletAPI
- 使用ServletActionContext类直接访问Servlet API
- Struts2-Action访问Servlet API
- Struts2的Action访问Servlet API
- javascript 直接创建对象与间接创建对象 私有函数不能访问this变量 是否使用var声明变量几个问题探讨
- Struts2中Action访问Servlet API的三种方法
- Struts2的Action访问Servlet API的方式
- Struts之Action(Action继承类、实现接口、访问servlet API方法)
- Struts2中action 访问servlet API