• struts2 servlet之间通信


    Servlet之间通信的方式有两大类,每个类有三种不同的方法

    1.request 

    2.session

    3.application

    不实现ServletContextAware,SessionAware,ServletRequestAware这三个接口的通信

    		HttpServletRequest request = ServletActionContext.getRequest();
    		request.setAttribute("request_username", "username");
    		HttpServletResponse response = ServletActionContext.getResponse();
    		
    		//这个是Servlet的session
    //		HttpSession session = request.getSession();
    //		session.setAttribute("session_username", "username");
    ServletContext servletContext = ServletActionContext.getServletContext(); servletContext.setAttribute("application_username", "username");

      

    实现ServletContextAware,SessionAware,ServletRequestAware这三个接口的通信

    public class Servlet2Action extends ActionSupport implements
    		ServletContextAware,SessionAware,ServletRequestAware {
    
    	private HttpServletRequest request;
    	private ServletContext servletContext;
    	private Map<String,Object> sessionMap;
    
    	/**
    	 * {}struts2包装以后的session,本质上是一个map
    		org.apache.catalina.core.ApplicationContextFacade@72898540 servlet中的application
    		org.apache.struts2.dispatcher.StrutsRequestWrapper@228b677f  struts2包装后的request
    	 */
    	public void setSession(Map<String, Object> arg0) {
    		this.sessionMap=arg0;
    		sessionMap.
    	}
    	public void setServletContext(ServletContext arg0) {
    		this.servletContext=arg0;
    	}
    	
    	public void setServletRequest(HttpServletRequest arg0) {
    		this.request=arg0;
    	}
    	public String testServelet()
    	{
    		System.out.println(sessionMap.toString());
    		System.out.println(servletContext.toString());
    		System.out.println(request.toString());
    		return "servelet";
    	}
    
    }
    

      

    在目标页面取出

       	request:${requestScope.request_username }<br>
       	session:${sessionScope.session_username }<br>
       	application:${applicationScope.application_username }<br>
    

      

  • 相关阅读:
    ASP.NET Zero--10.一个例子(3)商品分类管理-新建
    ASP.NET Zero--9.一个例子(2)商品分类管理-列表
    ASP.NET Zero--8.一个例子(1)菜单添加
    ASP.NET Zero--7.控制器加权限
    ASP.NET Zero--6.菜单加权限
    ASP.NET Zero--5.配置权限
    ASP.NET Zero--4.不使用谷歌字体,提升加载速度
    ASP.NET Zero--2.如何启动
    C# mongoDB Driver 使用对象方式最完善查询语法大全
    ef 数据库连接字符串加密
  • 原文地址:https://www.cnblogs.com/callyblog/p/7506620.html
Copyright © 2020-2023  润新知