1 64 65 package com.jcorporate.expresso.core.jsdkapi; 66 67 73 74 import javax.servlet.ServletException ; 75 import javax.servlet.http.HttpServletRequest ; 76 import javax.servlet.http.HttpSession ; 77 import java.io.Serializable ; 78 import java.util.Enumeration ; 79 80 81 84 public class ServletAPI2_1Session 85 implements APIAwareSession { 86 private static final String thisClass = ServletAPI2_1Session.class.getName() + "."; 87 88 89 97 public Serializable getAttribute(HttpServletRequest req, String code) 98 throws ServletException { 99 String myName = thisClass + 100 "getAttribute(HttpServletRequest, String)"; 101 102 if (req == null) { 103 throw new ServletException (myName + ":Request may not be null"); 104 } 105 106 HttpSession session = req.getSession(true); 107 Object o = session.getValue(code); 108 109 if (o == null) { 110 return null; 111 } 112 if (!(o instanceof Serializable )) { 113 throw new ServletException (myName + 114 ":Object stored in session with key '" + 115 code + 116 "' was not Serializable - it was of class '" + 117 o.getClass().getName() + "'"); 118 } 119 120 return (Serializable ) o; 121 } 122 123 124 131 public Enumeration getAttributeNames(HttpServletRequest req) 132 throws ServletException { 133 HttpSession session = req.getSession(true); 134 135 return session.getAttributeNames(); 136 } 137 138 145 public void setAttribute(HttpServletRequest req, String code, 146 Serializable value) 147 throws ServletException { 148 HttpSession session = req.getSession(true); 149 session.putValue(code, value); 150 } 151 152 153 160 public void removeAttribute(HttpServletRequest req, String code) 161 throws ServletException { 162 HttpSession session = req.getSession(true); 163 session.removeValue(code); 164 } 165 166 167 173 174 public void invalidate(HttpServletRequest req) { 175 HttpSession session = req.getSession(true); 176 session.invalidate(); 177 } 178 179 186 public String getId(HttpServletRequest req) { 187 HttpSession session = req.getSession(true); 188 189 return session.getId(); 190 } 191 192 199 public String getContextPath(HttpServletRequest req) { 200 String uri = req.getRequestURI(); 201 int lastPos = uri.lastIndexOf("/"); 202 203 if (lastPos > 0) { 204 return uri.substring(0, lastPos); 205 } else { 206 return uri; 207 } 208 } 209 210 } 211 212 | Popular Tags |