Java Tutorial/JSP/Request

Материал из Java эксперт
Перейти к: навигация, поиск

Decoding an HTTP Request

   <source lang="java">

<HTML>

   <HEAD>
       <TITLE>Decoding an HTTP Request</TITLE>
   </HEAD>

   <BODY>

Request Information

       JSP request method: <%= request.getMethod() %>
       
URL for the request: <%= request.getRequestURI() %>
Protocol of the request: <%= request.getProtocol() %>
Server name: <%= request.getServerName() %>
Path: <%= request.getServletPath() %>
Server port: <%= request.getServerPort() %>
Remote address: <%= request.getRemoteAddr() %>
Remote host: <%= request.getRemoteHost() %>
Locale: <%= request.getLocale() %>
User agent: <%= request.getHeader("User-Agent") %> </BODY>

</HTML></source>





Get HttpServletRequest

   <source lang="java">

<%--

 Copyright (c) 2002 by Phil Hanna
 All rights reserved.
 
 You may study, use, modify, and distribute this
 software for any purpose provided that this
 copyright notice appears in all copies.
 
 This software is provided without warranty
 either expressed or implied.

--%> <%@ page import="java.util.*" %> <%

  String title = "HttpServletRequest Method Values";
  Map entries = new TreeMap();
  entries.put("getCharacterEncoding", request.getCharacterEncoding());
  entries.put("getContentLength", "" + request.getContentLength());
  entries.put("getContentType", request.getContentType());
  entries.put("getLocale", request.getLocale());
  entries.put("getProtocol", request.getProtocol());
  entries.put("getRemoteAddr", request.getRemoteAddr());
  entries.put("getRemoteHost", request.getRemoteHost());
  entries.put("getScheme", request.getScheme());
  entries.put("getServerName", request.getServerName());
  entries.put("getServerPort", "" + request.getServerPort());
  entries.put("isSecure", "" + request.isSecure());
  request.setAttribute("_table_title", title);
  request.setAttribute("_table_entries", entries);
  
  out.println(request.getCharacterEncoding());

%></source>





Get Parameter Name and Value from TreeMap

   <source lang="java">

<%--

 Copyright (c) 2002 by Phil Hanna
 All rights reserved.
 
 You may study, use, modify, and distribute this
 software for any purpose provided that this
 copyright notice appears in all copies.
 
 This software is provided without warranty
 either expressed or implied.

--%> <%@ page import="java.util.*" %> <%

  Enumeration eNames = request.getParameterNames();
  if (eNames.hasMoreElements()) {
     String title = "Parameters";
     Map entries = new TreeMap();
     while (eNames.hasMoreElements()) {
        String name = (String) eNames.nextElement();
        String[] values = request.getParameterValues(name);
        if (values.length > 0) {
           String value = values[0];
           for (int i = 1; i < values.length; i++) {
              value += "," + values[i];
           }
           entries.put(name, value);
        }
     }
     request.setAttribute("_table_title", title);
     request.setAttribute("_table_entries", entries);
  }

%></source>





Get Servlet Request

   <source lang="java">

<%--

 Copyright (c) 2002 by Phil Hanna
 All rights reserved.
 
 You may study, use, modify, and distribute this
 software for any purpose provided that this
 copyright notice appears in all copies.
 
 This software is provided without warranty
 either expressed or implied.

--%> <%@ page import="java.util.*" %> <%

  String title = "ServletRequest Method Values";
  Map entries = new TreeMap();
  entries.put("getAuthType", request.getAuthType());
  entries.put("getContextPath", request.getContextPath());
  entries.put("getMethod", request.getMethod());
  entries.put("getPathInfo", request.getPathInfo());
  entries.put("getPathTranslated", request.getPathTranslated());
  entries.put("getQueryString", request.getQueryString());
  entries.put("getRemoteUser", request.getRemoteUser());
  entries.put("getRequestURI", request.getRequestURI());
  entries.put("getRequestedSessionId", request.getRequestedSessionId());
  entries.put("getServletPath", request.getServletPath());
  entries.put("isRequestedSessionIdFromCookie", "" + request.isRequestedSessionIdFromCookie());
  entries.put("isRequestedSessionIdFromURL", "" + request.isRequestedSessionIdFromURL());
  entries.put("isRequestedSessionIdValid", "" + request.isRequestedSessionIdValid());
  request.setAttribute("_table_title", title);
  request.setAttribute("_table_entries", entries);

%></source>





Put Request Header Name and Value to TreeMap

   <source lang="java">

<%--

 Copyright (c) 2002 by Phil Hanna
 All rights reserved.
 
 You may study, use, modify, and distribute this
 software for any purpose provided that this
 copyright notice appears in all copies.
 
 This software is provided without warranty
 either expressed or implied.

--%> <%@ page import="java.util.*" %> <%

  Enumeration eNames = request.getHeaderNames();
  if (eNames.hasMoreElements()) {
     String title = "Request Headers";
     Map entries = new TreeMap();
     while (eNames.hasMoreElements()) {
        String name = (String) eNames.nextElement();
        String value = request.getHeader(name);
        entries.put(name, value);
     }
     request.setAttribute("_table_title", title);
     request.setAttribute("_table_entries", entries);
  }

%></source>





Reference Http Servlet Request in Bean

Jsp code



   <source lang="java">

<html>

<jsp:useBean id="cb" scope="session" class="beans.MyBean" /> <jsp:setProperty name="cb" property="*" /> <%

 cb.processRequest(request);

%> <body bgcolor=<%= cb.getColor1() %>> <font size=6 color=<%= cb.getColor2() %>>

<% if (cb.getHint()==true) { %> Hint #1: Vampires prey at night! Hint #2: Nancy without the n. <% } %> <% if (cb.getSuccess()==true) { %> <P> CONGRATULATIONS!! <% if (cb.getHintTaken()==true) { %> <P> ( although I know you cheated and peeked into the hints) <% } %> <% } %> <P> Total attempts so far: <%= cb.getAttempts() %> <form method=POST action=index.jsp> Color #1: <input type=text name= color1 size=16>
Color #2: <input type=text name= color2 size=16> <P> <input type=submit name=action value="Submit"> <input type=submit name=action value="Hint"> </form> </font> </body> </html></source>

Retrieving the Original URI

   <source lang="java">

<%--

 Copyright (c) 2002 by Phil Hanna
 All rights reserved.
 
 You may study, use, modify, and distribute this
 software for any purpose provided that this
 copyright notice appears in all copies.
 
 This software is provided without warranty
 either expressed or implied.

--%> <html> <head> <title>Retrieving the Original URI</title> </head> <body>

In ShowPath1.jsp:
   request.getRequestURI() =
      <%= request.getRequestURI() %>
   request.getServletPath() =
      <%= request.getServletPath() %>

<jsp:include page="ShowPath2.jsp" flush="true"/> </body> </html></source>