struts 访问servlet api (ServletActionContext 方式)
来源:互联网 发布:如何提高网络延迟 编辑:程序博客网 时间:2024/06/06 11:15
ServletActionContext 是ActionContext的子类,还实现了StrutsStatics接口,可以说ServletActionContext 就是为了struts 的action获得servlet相关api而设计的。
public class ServletActionContext extends ActionContext implements StrutsStaticsServletActionContext 提供了一些静态的方法来获得想要的 api 。
PageContext :
public static PageContext getPageContext() { return (PageContext) ActionContext.getContext().get(PAGE_CONTEXT); }Request:
public static HttpServletRequest getRequest() { return (HttpServletRequest) ActionContext.getContext().get(HTTP_REQUEST); }Response:
public static HttpServletResponse getResponse() { return (HttpServletResponse) ActionContext.getContext().get(HTTP_RESPONSE); }ServletContext:
public static ServletContext getServletContext() { return (ServletContext) ActionContext.getContext().get(SERVLET_CONTEXT); }下面仍然是一个登录测试的小例子:
package com.cd.action;import org.apache.struts2.ServletActionContext;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.HttpSession;import java.io.IOException;public class LoginAction { public String login() { //获取request对象 HttpServletRequest request = (HttpServletRequest) ServletActionContext.getRequest(); Object username = request.getParameter("username"); Object password = request.getParameter("password"); if(username != null && password != null) { if("jack".equals(username)&&"jack".equals(password)) { //获得session对象 HttpSession session = ServletActionContext.getRequest().getSession(); session.setAttribute("username",username); return "success"; } else { //获得response对象 HttpServletResponse response = ServletActionContext.getResponse(); try { response.getWriter().write("failed"); } catch (IOException e) { e.printStackTrace(); } return null; } } else { //获得response对象 HttpServletResponse response = ServletActionContext.getResponse(); try { response.getWriter().write("check input"); } catch (IOException e) { e.printStackTrace(); } return null; } }}xml配置,返回界面和测试结果和上一篇博客一样,这里就不贴出来了。
0 0
- struts 访问servlet api (ServletActionContext 方式)
- struts 访问servlet api (Servlet...Aware方式)
- struts 访问 servlet api (ActionContext方式)
- Struts2(6):使用ServletActionContext访问Servlet API
- 使用ServletActionContext类直接访问Servlet API
- Struts2中访问Servlet中的API(ActionContext,ServletActionContext)两种方式
- Struts访问Servlet API
- Struts与Servlet API解耦的访问方式
- Struts中访问Servlet对象的方式(与Servlet API解耦的访问方式)
- struts中访问Servlet api
- Struts 2访问Servlet API
- Struts2_008_Servlet API耦合的访问方式值之ServletActionContext
- 在Struts中Action访问Servlet API
- Struts 2读书笔记-----Action访问Servlet API
- Struts 2读书笔记-----Action访问Servlet API
- struts 和servlet APi的耦合方式
- struts对于servlet的访问方式
- ActionContext和ServletActionContext区别以及action访问servlet API的三种方法
- android中Sqlite查询的数据返回排序问题
- swift 二维码识别的实现
- Flatten Binary Tree to Linked List
- linux下 gcc编译初学者使用教程
- day11--回顾
- struts 访问servlet api (ServletActionContext 方式)
- Android开发入门——推箱子游戏开发实战(十二)
- CVM ubuntux641604 搭建L2TP VPN
- HDU 1061 Rightmost Digit (求n^n的最低位)
- 利用NodeMcu和matplotlib的温度远程实时测量显示方案
- Android 小问题锦集
- java中路径问题详解
- LeetCode题解:Max Consecutive Ones
- Docker - Ubuntu 16.04安装Docker