Java Tutorial/JSP/Request
Содержание
Decoding an HTTP Request
<HTML>
<HEAD>
<TITLE>Decoding an HTTP Request</TITLE>
</HEAD>
<BODY>
<H1> Request Information </H1>
JSP request method: <%= request.getMethod() %>
<BR>
URL for the request: <%= request.getRequestURI() %>
<BR>
Protocol of the request: <%= request.getProtocol() %>
<BR>
Server name: <%= request.getServerName() %>
<BR>
Path: <%= request.getServletPath() %>
<BR>
Server port: <%= request.getServerPort() %>
<BR>
Remote address: <%= request.getRemoteAddr() %>
<BR>
Remote host: <%= request.getRemoteHost() %>
<BR>
Locale: <%= request.getLocale() %>
<BR>
User agent: <%= request.getHeader("User-Agent") %>
</BODY>
</HTML>
Get HttpServletRequest
<%--
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());
%>
Get Parameter Name and Value from TreeMap
<%--
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);
}
%>
Get Servlet Request
<%--
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);
%>
Put Request Header Name and Value to TreeMap
<%--
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);
}
%>
Reference Http Servlet Request in Bean
Jsp code
<html>
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<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() %>>
<P>
<% 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>
<br>
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>
Retrieving the Original URI
<%--
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>
<pre>
In ShowPath1.jsp:
request.getRequestURI() =
<%= request.getRequestURI() %>
request.getServletPath() =
<%= request.getServletPath() %>
</pre>
<jsp:include page="ShowPath2.jsp" flush="true"/>
</body>
</html>