共四种方式:
其中前两种得到的是Map<String,Object> 后两种得到的才是真正的request对象
而Map就是把request对象中的属性取出做成了键值对而已。
【方法一】
public class LoginAction { private Map request; private Map session; private Map application; public String execute() { request = (Map)ActionContext.getContext().get("request"); session = ActionContext.getContext().getSession(); application = ActionContext.getContext().getApplication(); request.put("username1", "jingjing1"); session.put("username2", "jingjing2"); application.put("username3", "jingjing3"); return "success"; } }
取出结果
<body> <% String username1 = (String)request.getAttribute("username1"); String username2 = (String)session.getAttribute("username2"); String username3 = (String)application.getAttribute("username3"); %> <%=username1 %> <%=username2 %> <%=username3 %> </body>
【方法二】
public class LoginAction implements RequestAware,SessionAware,ApplicationAware { private Map<String, Object> request; private Map<String, Object> session; private Map<String, Object> application; public void setRequest(Map<String, Object> request) { // TODO Auto-generated method stub this.request = request; } public void setSession(Map<String, Object> session) { // TODO Auto-generated method stub this.session = session; } public void setApplication(Map<String, Object> application) { // TODO Auto-generated method stub this.application = application; } public String execute() { request.put("username1", "jingjing11"); session.put("username2", "jingjing222"); application.put("username3", "jingjing33"); return "success"; } }
【方法三】
public class LoginAction { private HttpServletRequest request; private HttpSession session; private ServletContext application; public String execute() { request = ServletActionContext.getRequest(); session = request.getSession(); application = session.getServletContext(); //application = ServletActionContext.getRequest().getSession().getServletContext(); request.setAttribute("aaa", "aaa"); session.setAttribute("bbb", "bbb"); application.setAttribute("ccc", "ccc"); return "success"; } }
【方法四】
public class LoginAction implements ServletRequestAware { private HttpServletRequest request; private HttpSession session; private ServletContext application; public void setServletRequest(HttpServletRequest request) { // TODO Auto-generated method stub this.request = request; this.session = request.getSession(); this.application = session.getServletContext(); } public String execute() { request.setAttribute("111", "111"); session.setAttribute("222", "222"); application.setAttribute("333", "333"); return "success"; } }
HttpServletRequest方法总结:
HttpServletRequest接口是继承自ServletRequest接口的,增加了和HTTP相关的一些方法
getScheme() 方法返回请求的计划,比如http,https或者ftp.
getServerName() 方法返回被发送请求的服务器的主机名
getServerPort() 方法返回被发送请求的端口号。
getContextPath() 返回请求地址的根目录,以”/”开关,但不是以”/”结尾。
一个常用的获得服务器地址的连接字符串是:
String path = request.getContextPath();
String basePath = request.getScheme()+”://”+request.getServerName()+”:”+request.getServerPort()+path+”/”;
getCookies() 取得cookie
getMethod() 取得请求方法,如get,post或put
getRequestURL() 取得请求URL(统一资源定位符)
getRequestURI() 取得请求URI(统一资源标识符)
getSession() 取得对应session
public HttpSession getSession();
public HttpSession getSession(boolean create);
返回与这个请求关联的当前的有效的session。如果调用这个方法时没带参数,那么在没有session与这个请求关联的情况下,将会新建一个session。如果调用这个方法时带入了一个布尔型的参数,只有当这个参数为真时,session才会被建立。
ServletRequestAware与RequestAware的区别
ServletRequestAware——提供对HttpServletRequest对象的访问
RequestAware ——通过Map来提供对所有request属性的访问
原创文章,作者:Maggie-Hunter,如若转载,请注明出处:https://blog.ytso.com/14385.html