001 /** 002 * 003 * Copyright 2003-2004 The Apache Software Foundation 004 * 005 * Licensed under the Apache License, Version 2.0 (the "License"); 006 * you may not use this file except in compliance with the License. 007 * You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 018 // 019 // This source code implements specifications defined by the Java 020 // Community Process. In order to remain compliant with the specification 021 // DO NOT add / change / or delete method signatures! 022 // 023 024 package javax.servlet; 025 026 import java.io.BufferedReader; 027 import java.io.IOException; 028 import java.util.Enumeration; 029 import java.util.Locale; 030 import java.util.Map; 031 032 033 /** 034 * Defines an object to provide client request information to a servlet. The 035 * servlet container creates a <code>ServletRequest</code> object and passes 036 * it as an argument to the servlet's <code>service</code> method. 037 * 038 * <p>A <code>ServletRequest</code> object provides data including 039 * parameter name and values, attributes, and an input stream. 040 * Interfaces that extend <code>ServletRequest</code> can provide 041 * additional protocol-specific data (for example, HTTP data is 042 * provided by {@link javax.servlet.http.HttpServletRequest}. 043 * 044 * @see javax.servlet.http.HttpServletRequest 045 * 046 * @version $Rev: 46019 $ $Date: 2004-09-14 02:56:06 -0700 (Tue, 14 Sep 2004) $ 047 */ 048 public interface ServletRequest { 049 /** 050 * Returns the value of the named attribute as an <code>Object</code>, 051 * or <code>null</code> if no attribute of the given name exists. 052 * 053 * <p> Attributes can be set two ways. The servlet container may set 054 * attributes to make available custom information about a request. 055 * For example, for requests made using HTTPS, the attribute 056 * <code>javax.servlet.request.X509Certificate</code> can be used to 057 * retrieve information on the certificate of the client. Attributes 058 * can also be set programatically using 059 * {@link ServletRequest#setAttribute}. This allows information to be 060 * embedded into a request before a {@link RequestDispatcher} call. 061 * 062 * <p>Attribute names should follow the same conventions as package 063 * names. This specification reserves names matching <code>java.*</code>, 064 * <code>javax.*</code>, and <code>sun.*</code>. 065 * 066 * @param name a <code>String</code> specifying the name of 067 * the attribute 068 * 069 * @return an <code>Object</code> containing the value 070 * of the attribute, or <code>null</code> if the attribute does not exist 071 */ 072 public Object getAttribute(String name); 073 074 /** 075 * Returns an <code>Enumeration</code> containing the 076 * names of the attributes available to this request. 077 * This method returns an empty <code>Enumeration</code> 078 * if the request has no attributes available to it. 079 * 080 * 081 * @return an <code>Enumeration</code> of strings 082 * containing the names of the request's attributes 083 */ 084 public Enumeration getAttributeNames(); 085 086 /** 087 * Returns the name of the character encoding used in the body of this 088 * request. This method returns <code>null</code> if the request 089 * does not specify a character encoding 090 * 091 * @return a <code>String</code> containing the name of 092 * the chararacter encoding, or <code>null</code> 093 * if the request does not specify a character encoding 094 */ 095 public String getCharacterEncoding(); 096 097 /** 098 * Overrides the name of the character encoding used in the body of this 099 * request. This method must be called prior to reading request parameters 100 * or reading input using getReader(). 101 * 102 * @param env a <code>String</code> containing the name of 103 * the chararacter encoding. 104 * 105 * @throws java.io.UnsupportedEncodingException if this is not a valid encoding 106 */ 107 public void setCharacterEncoding(String env) throws java.io.UnsupportedEncodingException; 108 109 /** 110 * Returns the length, in bytes, of the request body 111 * and made available by the input stream, or -1 if the 112 * length is not known. For HTTP servlets, same as the value 113 * of the CGI variable CONTENT_LENGTH. 114 * 115 * @return an integer containing the length of the 116 * request body or -1 if the length is not known 117 */ 118 public int getContentLength(); 119 120 /** 121 * Returns the MIME type of the body of the request, or 122 * <code>null</code> if the type is not known. For HTTP servlets, 123 * same as the value of the CGI variable CONTENT_TYPE. 124 * 125 * @return a <code>String</code> containing the name 126 * of the MIME type of the request, or null if the type is not known 127 */ 128 public String getContentType(); 129 130 /** 131 * Retrieves the body of the request as binary data using 132 * a {@link ServletInputStream}. Either this method or 133 * {@link #getReader} may be called to read the body, not both. 134 * 135 * @return a {@link ServletInputStream} object containing 136 * the body of the request 137 * 138 * @exception IllegalStateException if the {@link #getReader} method 139 * has already been called for this request 140 * 141 * @exception IOException if an input or output exception occurred 142 */ 143 public ServletInputStream getInputStream() throws IOException; 144 145 /** 146 * Returns the value of a request parameter as a <code>String</code>, 147 * or <code>null</code> if the parameter does not exist. Request parameters 148 * are extra information sent with the request. For HTTP servlets, 149 * parameters are contained in the query string or posted form data. 150 * 151 * <p>You should only use this method when you are sure the 152 * parameter has only one value. If the parameter might have 153 * more than one value, use {@link #getParameterValues}. 154 * 155 * <p>If you use this method with a multivalued 156 * parameter, the value returned is equal to the first value 157 * in the array returned by <code>getParameterValues</code>. 158 * 159 * <p>If the parameter data was sent in the request body, such as occurs 160 * with an HTTP POST request, then reading the body directly via {@link 161 * #getInputStream} or {@link #getReader} can interfere 162 * with the execution of this method. 163 * 164 * @param name a <code>String</code> specifying the 165 * name of the parameter 166 * 167 * @return a <code>String</code> representing the 168 * single value of the parameter 169 * 170 * @see #getParameterValues 171 */ 172 public String getParameter(String name); 173 174 /** 175 * Returns an <code>Enumeration</code> of <code>String</code> 176 * objects containing the names of the parameters contained 177 * in this request. If the request has 178 * no parameters, the method returns an 179 * empty <code>Enumeration</code>. 180 * 181 * @return an <code>Enumeration</code> of <code>String</code> 182 * objects, each <code>String</code> containing 183 * the name of a request parameter; or an 184 * empty <code>Enumeration</code> if the 185 * request has no parameters 186 */ 187 public Enumeration getParameterNames(); 188 189 /** 190 * Returns an array of <code>String</code> objects containing 191 * all of the values the given request parameter has, or 192 * <code>null</code> if the parameter does not exist. 193 * 194 * <p>If the parameter has a single value, the array has a length 195 * of 1. 196 * 197 * @param name a <code>String</code> containing the name of 198 * the parameter whose value is requested 199 * 200 * @return an array of <code>String</code> objects 201 * containing the parameter's values 202 * 203 * @see #getParameter 204 */ 205 public String[] getParameterValues(String name); 206 207 /** 208 * Returns a java.util.Map of the parameters of this request. 209 * Request parameters are extra information sent with the request. 210 * For HTTP servlets, parameters are contained in the query 211 * string or posted form data. 212 * 213 * @return an immutable java.util.Map containing parameter names as 214 * keys and parameter values as map values. The keys in the parameter 215 * map are of type String. The values in the parameter map are of type 216 * String array. 217 */ 218 public Map getParameterMap(); 219 220 /** 221 * Returns the name and version of the protocol the request uses 222 * in the form <i>protocol/majorVersion.minorVersion</i>, for 223 * example, HTTP/1.1. For HTTP servlets, the value 224 * returned is the same as the value of the CGI variable 225 * <code>SERVER_PROTOCOL</code>. 226 * 227 * @return a <code>String</code> containing the protocol 228 * name and version number 229 */ 230 public String getProtocol(); 231 232 /** 233 * Returns the name of the scheme used to make this request, 234 * for example, 235 * <code>http</code>, <code>https</code>, or <code>ftp</code>. 236 * Different schemes have different rules for constructing URLs, 237 * as noted in RFC 1738. 238 * 239 * @return a <code>String</code> containing the name 240 * of the scheme used to make this request 241 */ 242 public String getScheme(); 243 244 /** 245 * Returns the host name of the server to which the request was sent. 246 * It is the value of the part before ":" in the <code>Host</code> 247 * header, if any, or the resolved server name, or the server IP address. 248 * 249 * @return a <code>String</code> containing the name 250 * of the server 251 */ 252 public String getServerName(); 253 254 /** 255 * Returns the port number to which the request was sent. 256 * It is the value of the part after ":" in the <code>Host</code> 257 * header, if any, or the server port where the client connection 258 * was accepted on. 259 * 260 * @return an integer specifying the port number 261 */ 262 public int getServerPort(); 263 264 /** 265 * Retrieves the body of the request as character data using 266 * a <code>BufferedReader</code>. The reader translates the character 267 * data according to the character encoding used on the body. 268 * Either this method or {@link #getInputStream} may be called to read the 269 * body, not both. 270 * 271 * @return a <code>BufferedReader</code> containing the body of the request 272 * 273 * @exception UnsupportedEncodingException if the character set encoding 274 * used is not supported and the text cannot be decoded 275 * 276 * @exception IllegalStateException if {@link #getInputStream} method 277 * has been called on this request 278 * 279 * @exception IOException if an input or output exception occurred 280 * 281 * @see #getInputStream 282 */ 283 public BufferedReader getReader() throws IOException; 284 285 /** 286 * Returns the Internet Protocol (IP) address of the client 287 * or last proxy that sent the request. 288 * For HTTP servlets, same as the value of the 289 * CGI variable <code>REMOTE_ADDR</code>. 290 * 291 * @return a <code>String</code> containing the 292 * IP address of the client that sent the request 293 */ 294 public String getRemoteAddr(); 295 296 /** 297 * Returns the fully qualified name of the client 298 * or the last proxy that sent the request. 299 * If the engine cannot or chooses not to resolve the hostname 300 * (to improve performance), this method returns the dotted-string form of 301 * the IP address. For HTTP servlets, same as the value of the CGI variable 302 * <code>REMOTE_HOST</code>. 303 * 304 * @return a <code>String</code> containing the fully 305 * qualified name of the client 306 */ 307 public String getRemoteHost(); 308 309 /** 310 * Stores an attribute in this request. 311 * Attributes are reset between requests. This method is most 312 * often used in conjunction with {@link RequestDispatcher}. 313 * 314 * <p>Attribute names should follow the same conventions as 315 * package names. Names beginning with <code>java.*</code>, 316 * <code>javax.*</code>, and <code>com.sun.*</code>, are 317 * reserved for use by Sun Microsystems. 318 *<br> If the object passed in is null, the effect is the same as 319 * calling {@link #removeAttribute}. 320 * 321 * @param name a <code>String</code> specifying the name of the attribute 322 * 323 * @param o the <code>Object</code> to be stored 324 */ 325 public void setAttribute(String name, Object o); 326 327 /** 328 * Removes an attribute from this request. This method is not 329 * generally needed as attributes only persist as long as the request 330 * is being handled. 331 * 332 * <p>Attribute names should follow the same conventions as 333 * package names. Names beginning with <code>java.*</code>, 334 * <code>javax.*</code>, and <code>com.sun.*</code>, are 335 * reserved for use by Sun Microsystems. 336 * 337 * @param name a <code>String</code> specifying 338 * the name of the attribute to remove 339 */ 340 public void removeAttribute(String name); 341 342 /** 343 * Returns the preferred <code>Locale</code> that the client will 344 * accept content in, based on the Accept-Language header. 345 * If the client request doesn't provide an Accept-Language header, 346 * this method returns the default locale for the server. 347 * 348 * @return the preferred <code>Locale</code> for the client 349 */ 350 public Locale getLocale(); 351 352 /** 353 * Returns an <code>Enumeration</code> of <code>Locale</code> objects 354 * indicating, in decreasing order starting with the preferred locale, the 355 * locales that are acceptable to the client based on the Accept-Language 356 * header. 357 * If the client request doesn't provide an Accept-Language header, 358 * this method returns an <code>Enumeration</code> containing one 359 * <code>Locale</code>, the default locale for the server. 360 * 361 * @return an <code>Enumeration</code> of preferred 362 * <code>Locale</code> objects for the client 363 */ 364 public Enumeration getLocales(); 365 366 /** 367 * Returns a boolean indicating whether this request was made using a 368 * secure channel, such as HTTPS. 369 * 370 * @return a boolean indicating if the request was made using a 371 * secure channel 372 */ 373 public boolean isSecure(); 374 375 /** 376 * Returns a {@link RequestDispatcher} object that acts as a wrapper for 377 * the resource located at the given path. 378 * A <code>RequestDispatcher</code> object can be used to forward 379 * a request to the resource or to include the resource in a response. 380 * The resource can be dynamic or static. 381 * 382 * <p>The pathname specified may be relative, although it cannot extend 383 * outside the current servlet context. If the path begins with 384 * a "/" it is interpreted as relative to the current context root. 385 * This method returns <code>null</code> if the servlet container 386 * cannot return a <code>RequestDispatcher</code>. 387 * 388 * <p>The difference between this method and {@link 389 * ServletContext#getRequestDispatcher} is that this method can take a 390 * relative path. 391 * 392 * @param path a <code>String</code> specifying the pathname to the 393 * resource. If it is relative, it must be relative against the 394 * current servlet. 395 * 396 * @return a <code>RequestDispatcher</code> object that acts as a 397 * wrapper for the resource at the specified path, or <code>null</code> 398 * if the servlet container cannot return a 399 * <code>RequestDispatcher</code> 400 * 401 * @see RequestDispatcher 402 * @see ServletContext#getRequestDispatcher 403 */ 404 public RequestDispatcher getRequestDispatcher(String path); 405 406 /** 407 * @deprecated As of Version 2.1 of the Java Servlet API, 408 * use {@link ServletContext#getRealPath} instead. 409 */ 410 public String getRealPath(String path); 411 412 /** 413 * Returns the Internet Protocol (IP) source port of the client 414 * or last proxy that sent the request. 415 * 416 * @since Servlet 2.4 417 */ 418 public int getRemotePort(); 419 420 /** 421 * Returns the host name of the Internet Protocol (IP) interface on 422 * which the request was received. 423 * 424 * @return a <code>String</code> containing the host 425 * name of the IP on which the request was received. 426 * 427 * @since Servlet 2.4 428 */ 429 public String getLocalName(); 430 431 /** 432 * Returns the Internet Protocol (IP) address of the interface on 433 * which the request was received. 434 * 435 * @return a <code>String</code> containing the 436 * IP address on which the request was received. 437 * 438 * @since Servlet 2.4 439 */ 440 public String getLocalAddr(); 441 442 /** 443 * Returns the Internet Protocol (IP) port number of the interface 444 * on which the request was received. 445 * 446 * @return an integer specifying the port number 447 * 448 * @since Servlet 2.4 449 */ 450 public int getLocalPort(); 451 } 452