package com.hongdian;

import java.util.Enumeration;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; /**
*
* @author yjmao
* @描述 HttpServletRequest接口学习
* @继承于 ServletRequest
*/
@WebServlet(name = "SpanningTreeServlet", urlPatterns = {"/SpanningTreeServlet.html"})
public class HttpServletRequestLearn extends HttpServlet { @Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//authType(request);
//getContextPath(request);
//getCookies(request);
//getDateHeader(request);
//getHeader(request);
//getHeaderNames(request);
//getHeaders(request);
//getIntHeader(request);
//getMethod(request);
//getPathInfo(request);
//getPathTranslated(request);
//getQueryString(request);
//getRemoteUser(request);
//getRequestedSessionId(request);
//getRequestURI(request);
//getRequestURL(request);
//getServletPath(request);
//getSession(request);
//getSessionBollean(request);
//getUserPrincipal(request);
//isRequestedSessionIdFromCookie(request);
//isRequestedSessionIdFromUrl(request)
//isRequestedSessionIdFromURL(request);
//isRequestedSessionIdValid(request) ;
//isUserInRole(request);
System.err.println("GET进来啦!");
} /*
* isRequestedSessionIdFromURL():返回一个布尔值,表示经过身份验证的用户是否包含在指定的逻辑“角色”。
*
*/
public static void isUserInRole(HttpServletRequest request) {
Boolean isUserInRole = request.isUserInRole("mao");
System.out.println("isUserInRole = " + isUserInRole);
}
/*
* isRequestedSessionIdFromURL():检查请求的会话ID是否仍然有效。
*
*/
public static void isRequestedSessionIdValid(HttpServletRequest request) {
Boolean isRequestedSessionIdValid = request.isRequestedSessionIdValid();
System.out.println("isRequestedSessionIdValid = " + isRequestedSessionIdValid);
}
/*
* isRequestedSessionIdFromURL():检查请求的会话ID来作为请求URL的一部分。
*
*/
public static void isRequestedSessionIdFromURL(HttpServletRequest request) {
Boolean isRequestedSessionIdFromURL = request.isRequestedSessionIdFromURL();
System.out.println("isRequestedSessionIdFromURL = " + isRequestedSessionIdFromURL);
}
/*
* isRequestedSessionIdFromCookie():检查请求的会话ID作为一个cookie。
*
*/
public static void isRequestedSessionIdFromCookie(HttpServletRequest request) {
Boolean isRequestedSessionIdFromCookie = request.isRequestedSessionIdFromCookie();
System.out.println("isRequestedSessionIdFromCookie = " + isRequestedSessionIdFromCookie);
}
/*
* getSession():返回与此请求关联的HttpSession中,如果没有当前会话,并创建是真实的,则返回一个新的会话。
*
*/
public static void getSessionBollean(HttpServletRequest request) {
HttpSession getSession = request.getSession(true);
String getId = getSession.getId();
System.out.println("getId = " + getId);
} /*
* getSession():返回与此请求关联的当前会话,或者如果请求没有会话,创建一个。
*
*/
public static void getSession(HttpServletRequest request) {
HttpSession getSession = request.getSession();
String getId = getSession.getId();
System.out.println("getId = " + getId);
} /*
* getServletPath():返回调用servlet的请求的URL的一部分。
* 如:/SpanningTreeServlet.html
*/
public static void getServletPath(HttpServletRequest request) {
String getServletPath = request.getServletPath();
System.out.println("getServletPath = " + getServletPath);
} /*
* getRequestURL():重建的要求,使客户端使用的URL。
* 如:http://localhost:8088/LargeAmountOfDataSpanningTree/SpanningTreeServlet.html
*/
public static void getRequestURL(HttpServletRequest request) {
StringBuffer getRequestURL = request.getRequestURL();
System.out.println("getRequestURL = " + getRequestURL.toString());
} /*
* getRequestURI():返回此请求的URL的一部分从HTTP请求中的第一行中的查询字符串的协议名称。
* 如:/LargeAmountOfDataSpanningTree/SpanningTreeServlet.html
*/
public static void getRequestURI(HttpServletRequest request) {
String getRequestURI = request.getRequestURI();
System.out.println("getRequestURI = " + getRequestURI);
} /*
* getRequestedSessionId():返回指定的客户端的会话ID。
*
*/
public static void getRequestedSessionId(HttpServletRequest request) {
String getRequestedSessionId = request.getRequestedSessionId();
System.out.println("getRequestedSessionId = " + getRequestedSessionId);
} /*
* getMethod():返回这个请求的HTTP方法的名称,例如,GET,POST或PUT。
*
*/
public static void getMethod(HttpServletRequest request) {
String getMethod = request.getMethod();
System.out.println("getMethod = " + getMethod);
} /*
* getIntHeader(String name):返回指定的请求头为一个int值。
* 参数name:请求头中的参数
* 不是很懂
*/
public static void getIntHeader(HttpServletRequest request) {
int enums = request.getIntHeader("mm");
System.out.println("enums = " + enums);
} /*
* getHeaders(String name):返回一个String对象的枚举指定的请求标头的所有值。
* 参数name:请求头中的参数
*/
public static void getHeaders(HttpServletRequest request) {
Enumeration enums = request.getHeaders("Accept-Charset");
while (enums.hasMoreElements()) {
System.out.println("******head=" + enums.nextElement() + "************");
}
} /*
* getHeaderNames():返回一个枚举这个请求包含所有的头名。
*
*/
public static void getHeaderNames(HttpServletRequest request) {
Enumeration enums = request.getHeaderNames();
String str = null;
while (enums.hasMoreElements()) {
str = enums.nextElement().toString();
System.out.print(str + ":");
System.out.println(request.getHeader(str));
Enumeration a = request.getHeaders(str);
while (a.hasMoreElements()) {
System.out.println("******head=" + a.nextElement() + "************");
}
}
} /*
* getHeader(String name):返回值作为一个String指定的请求头。
* 参数name:请求头中的参数,
* 如:
Accept-Charset:GBK,utf-8;q=0.7,*;q=0.3
Accept-Encoding:gzip,deflate,sdch
Accept-Language:zh-CN,zh;q=0.8
Connection:keep-alive
Cookie:JSESSIONID=461DB37360713AFBFB9E0B50FF716191
Host:localhost:8088
Referer:http://localhost:8088/LargeAmountOfDataSpanningTree/
User-Agent:Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11
X-Requested-With:XMLHttpRequest
*/
public static void getHeader(HttpServletRequest request) {
String getHeader = request.getHeader("Accept-Charset");
System.err.println("getHeader = " + getHeader);
String Host = request.getHeader("Host");
System.err.println("Host = " + Host);
} /*
* getCookies():返回一个数组,包含所有客户端发送申请的Cookie对象。
*
*/
public static void getCookies(HttpServletRequest request) {
Cookie[] getCookies = request.getCookies();
int i = ;
for (Cookie cook : getCookies) {
i++;
System.err.println("第" + i + "个cook = " + cook.getName() + ";value = " + cook.getValue());
}
} /*
* getContextPath():指示上下文请求的URI
* 如http://localhost:8088/LargeAmountOfDataSpanningTree/
* 返回的就是/LargeAmountOfDataSpanningTree
*/
public static void getContextPath(HttpServletRequest request) {
String getContextPath = request.getContextPath();
System.err.println("getContextPath = " + getContextPath);
} /*
* authType():返回的身份验证方案,用于保护的servlet的名称。
*/
public static void authType(HttpServletRequest request) {
String authType = request.getAuthType();
System.err.println("authType = " + authType);
} /**
* ***************************不是很清楚怎么样的***********************************************
*/
/*
* getUserPrincipal():返回一个java.security.Principal对象,它包含当前经过身份验证的用户的名称。
*
*/
public static void getUserPrincipal(HttpServletRequest request) {
java.security.Principal pp = request.getUserPrincipal();
System.err.println("pp = " + pp);
} /*
* isRequestedSessionIdFromCookie():检查请求的会话ID作为一个cookie。
* 方法已经过时
*/
public static void isRequestedSessionIdFromUrl(HttpServletRequest request) {
Boolean isRequestedSessionIdFromUrl = request.isRequestedSessionIdFromUrl();
System.out.println("isRequestedSessionIdFromUrl = " + isRequestedSessionIdFromUrl);
} /*
* getPathTranslated():提出这个要求时,客户端发送的URL返回任何额外的路径信息。
*
*/
public static void getPathTranslated(HttpServletRequest request) {
String getPathTranslated = request.getPathTranslated();
System.out.println("getPathTranslated = " + getPathTranslated);
} /*
* getPathInfo():提出这个要求时,客户端发送的URL返回任何额外的路径信息。
*
*/
public static void getPathInfo(HttpServletRequest request) {
String getPathInfo = request.getPathInfo();
System.out.println("getPathInfo = " + getPathInfo);
}
/*
* getQueryString():返回的查询字符串中包含请求的URL路径后。
*
*/ public static void getQueryString(HttpServletRequest request) {
String getQueryString = request.getQueryString();
System.out.println("getQueryString = " + getQueryString);
} /*
* getRemoteUser():提出这个要求返回登录的用户,如果用户已通过身份验证,或者如果用户未通过身份验证,则返回null。
*
*/
public static void getRemoteUser(HttpServletRequest request) {
String getRemoteUser = request.getRemoteUser();
System.out.println("getRemoteUser = " + getRemoteUser);
}
/*
* getDateHeader(String name):返回指定的请求头作为一项长期的价值,代表一个日期对象的价值。
* 注明:不是很懂
*/ public static void getDateHeader(HttpServletRequest request) {
long tie = request.getDateHeader("mm");
System.err.println("tie = " + tie);
} @Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//authType(request);
getMethod(request);
System.err.println("Put进来啦!");
}
}
05-12 17:29