下面列出了javax.servlet.http.HttpServletRequest#getProtocol() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
protected void writeErrorPage(HttpServletRequest request, Writer writer, int code, String message, boolean showStacks) throws IOException {
if (message == null)
message=HttpStatus.getMessage(code);
// Write a very short body (this makes people pushing with collectd happy).
writer.write(String.valueOf(code));
writer.write(" -- ");
writer.write(message);
// Log the request error.
Throwable th = (Throwable)request.getAttribute(RequestDispatcher.ERROR_EXCEPTION);
if (th != null) {
final String http_request = request.getMethod() + ' ' + request.getRequestURI() + ' ' + request.getProtocol();
LOG.log(Level.WARNING, http_request, th);
}
}
public HttpRequestInfo(HttpServletRequest request) {
this.authType = request.getAuthType();
this.contextPath = request.getContextPath();
populateHeaders(request);
this.method = request.getMethod();
this.pathInfo = request.getPathInfo();
this.queryString = request.getQueryString();
this.requestURI = request.getRequestURI();
this.servletPath = request.getServletPath();
this.contentType = request.getContentType();
this.characterEncoding = request.getCharacterEncoding();
this.contentLength = request.getContentLength();
this.localName = request.getLocalName();
this.localPort = request.getLocalPort();
populateParameters(request);
this.protocol = request.getProtocol();
this.remoteAddr = request.getRemoteAddr();
this.remoteHost = request.getRemoteHost();
this.remotePort = request.getRemotePort();
this.serverName = request.getServerName();
this.secure = request.isSecure();
populateAttributes(request);
}
private void doService(HttpServletRequest req, HttpServletResponse resp, HttpMethod httpMethod) {
if (log.isDebugEnabled()) {
log.debug("DispatcherServlet accept request for [{}] on method [{}]", req.getServletPath(),
httpMethod);
}
String requestUri = req.getRequestURI();
String queryString = req.getQueryString();
if (queryString != null && queryString.length() > 0) {
requestUri += Strings.QUESTION_MARK + queryString;
}
final Request request = new Request(httpMethod, requestUri, req.getProtocol());
request.addAttribute(Request.ORIGINAL_REQUEST, req);
request.addAttribute(Response.ORIGINAL_RESPONSE, resp).local();
final Response response = new Response(request);
response.local();
final RoutingContext ctx = new RoutingContextImpl(request, response);
try {
Context.dispatch(ctx);
} catch (Exception e) {
Context.routeError(ctx, e);
} finally {
Request.clear();
Response.clear();
copyResponse(response, resp);
copyStream(response, resp);
}
}
protected void notSupported(HttpServletRequest req, HttpServletResponse resp) throws IOException {
String protocol = req.getProtocol();
String msg = "HTTP "+req.getMethod()+" not supported" ;
if (protocol.endsWith("1.1")) {
ServletOps.error(HttpServletResponse.SC_METHOD_NOT_ALLOWED, msg);
} else {
ServletOps.error(HttpServletResponse.SC_BAD_REQUEST, msg);
}
}
private void serviceTrace(HttpServletRequest req, HttpServletResponse resp)
throws IOException
{
int responseLength;
final String CRLF = "\r\n";
String responseString =
"TRACE " + req.getRequestURI() + " " + req.getProtocol();
@SuppressWarnings("unchecked")
final Enumeration<String> reqHeaderEnum = req.getHeaderNames();
while (reqHeaderEnum.hasMoreElements()) {
final String headerName = reqHeaderEnum.nextElement();
responseString += CRLF + headerName + ": " + req.getHeader(headerName);
}
responseString += CRLF;
responseLength = responseString.length();
resp.setContentType("message/http");
resp.setContentLength(responseLength);
final ServletOutputStream out = resp.getOutputStream();
out.print(responseString);
out.close();
}
@Override
public void handle(String target, Request baseRequest,
HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
LOGGER.info("request from " + request.getRemoteAddr());
response.setContentType("text/html;charset=utf-8");
final Reader reader = request.getReader();
try {
JAXBContext ctx = JAXBContext.newInstance(Mmi.class);
final Unmarshaller unmarshaller = ctx.createUnmarshaller();
final Object o = unmarshaller.unmarshal(reader);
if (o instanceof Mmi) {
final Mmi mmi = (Mmi) o;
LOGGER.info("received MMI event: " + mmi);
final String protocol = request.getProtocol();
final DecoratedMMIEvent event = new DecoratedMMIEvent(this, mmi);
final Runnable runnable = new Runnable() {
@Override
public void run() {
notifyMMIEvent(protocol, event);
}
};
final Thread thread = new Thread(runnable);
thread.start();
response.setStatus(HttpServletResponse.SC_OK);
} else {
LOGGER.warn("received unknown MMI object: " + o);
response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
}
} catch (JAXBException e) {
LOGGER.error("unable to read input", e);
response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
}
baseRequest.setHandled(true);
}
public HttpServletRequestSnapshot(HttpServletRequest request) {
super(request);
authType = request.getAuthType();
characterEncoding = request.getCharacterEncoding();
contentLength = request.getContentLength();
contentType = request.getContentType();
contextPath = request.getContextPath();
cookies = request.getCookies();
requestHeaderNames = request.getHeaderNames();
Enumeration<String> tmp = request.getHeaderNames();
while (tmp.hasMoreElements()) {
String key = tmp.nextElement();
headersMap.put(key, request.getHeaders(key));
}
localAddr = request.getLocalAddr();
local = request.getLocale();
localName = request.getLocalName();
localPort = request.getLocalPort();
method = request.getMethod();
pathInfo = request.getPathInfo();
pathTranslated = request.getPathTranslated();
protocol = request.getProtocol();
queryString = request.getQueryString();
remoteAddr = request.getRemoteAddr();
remoteHost = request.getRemoteHost();
remotePort = request.getRemotePort();
remoteUser = request.getRemoteUser();
requestURI = request.getRequestURI();
requestURL = request.getRequestURL();
requestedSessionId = request.getRequestedSessionId();
schema = request.getScheme();
serverName = request.getServerName();
serverPort = request.getServerPort();
servletPath = request.getServletPath();
if (request.isRequestedSessionIdValid()) {
session = request.getSession();
}
principal = request.getUserPrincipal();
}
public RequestData(final HttpServletRequest request) {
method = request.getMethod();
uri = request.getRequestURI();
protocol = request.getProtocol();
servletPath = request.getServletPath();
pathInfo = request.getPathInfo();
pathTranslated = request.getPathTranslated();
characterEncoding = request.getCharacterEncoding();
queryString = request.getQueryString();
contentLength = request.getContentLength();
contentType = request.getContentType();
serverName = request.getServerName();
serverPort = request.getServerPort();
remoteUser = request.getRemoteUser();
remoteAddress = request.getRemoteAddr();
remoteHost = request.getRemoteHost();
remotePort = request.getRemotePort();
localAddress = request.getLocalAddr();
localHost = request.getLocalName();
localPort = request.getLocalPort();
authorizationScheme = request.getAuthType();
preferredClientLocale = request.getLocale();
allClientLocales = Collections.list(request.getLocales());
contextPath = request.getContextPath();
userPrincipal = request.getUserPrincipal();
requestHeaders = getRequestHeaders(request);
cookies = getCookies(request.getCookies());
requestAttributes = getRequestAttributes(request);
}
public ServletRequestCopy(HttpServletRequest request) {
this.servletPath = request.getServletPath();
this.contextPath = request.getContextPath();
this.pathInfo = request.getPathInfo();
this.requestUri = request.getRequestURI();
this.requestURL = request.getRequestURL();
this.method = request.getMethod();
this.serverName = request.getServerName();
this.serverPort = request.getServerPort();
this.protocol = request.getProtocol();
this.scheme = request.getScheme();
/*
* have to comment out below two lines as otherwise web socket will
* report UnSupportedOperationException upon connection
*/
//this.characterEncoding = request.getCharacterEncoding();
//this.contentType = request.getContentType();
//this.requestedSessionId = request.getRequestedSessionId();
this.characterEncoding = null;
this.contentType = null;
this.requestedSessionId = null;
this.locale = request.getLocale();
this.locales = request.getLocales();
this.isSecure = request.isSecure();
this.remoteUser = request.getRemoteUser();
this.remoteAddr = request.getRemoteAddr();
this.remoteHost = request.getRemoteHost();
this.remotePort = request.getRemotePort();
this.localAddr = request.getLocalAddr();
this.localName = request.getLocalName();
this.localPort = request.getLocalPort();
this.pathTranslated = request.getPathTranslated();
this.principal = request.getUserPrincipal();
HttpSession session = request.getSession(true);
httpSession = new HttpSessionCopy(session);
String s;
Enumeration<String> e = request.getHeaderNames();
while (e != null && e.hasMoreElements()) {
s = e.nextElement();
Enumeration<String> headerValues = request.getHeaders(s);
this.headers.put(s, headerValues);
}
e = request.getAttributeNames();
while (e != null && e.hasMoreElements()) {
s = e.nextElement();
attributes.put(s, request.getAttribute(s));
}
e = request.getParameterNames();
while (e != null && e.hasMoreElements()) {
s = e.nextElement();
parameters.put(s, request.getParameterValues(s));
}
}
/**
* Creates an instance of ClientData based on the request
*/
private void recordClientData(ClientData cd,
HttpServletRequest request) {
String protocol = request.getProtocol();
while(protocol.endsWith("\n")) //NOI18N
protocol = protocol.substring(0, protocol.length()-2);
cd.setAttributeValue("protocol", protocol); //NOI18N
cd.setAttributeValue("remoteAddress", //NOI18N
request.getRemoteAddr());
Enumeration hvals;
StringBuffer valueBuf;
int counter;
// Software used
valueBuf = new StringBuffer(128);
counter = 0;
hvals = request.getHeaders(Constants.Http.userAgent);
if(hvals != null) {
while(hvals.hasMoreElements()) {
if(counter > 0) valueBuf.append(", "); // NOI18N
valueBuf.append((String)hvals.nextElement());
++counter;
}
}
cd.setAttributeValue("software", valueBuf.toString()); //NOI18N
//Languages
valueBuf = new StringBuffer(128);
counter = 0;
hvals = request.getHeaders(Constants.Http.acceptLang);
if(hvals != null) {
while(hvals.hasMoreElements()) {
if(counter > 0) valueBuf.append(", "); // NOI18N
valueBuf.append((String)hvals.nextElement());
++counter;
}
}
cd.setAttributeValue("locale", valueBuf.toString()); //NOI18N
// File formats
valueBuf = new StringBuffer(128);
counter = 0;
hvals = request.getHeaders(Constants.Http.accept);
if(hvals != null) {
while(hvals.hasMoreElements()) {
if(counter > 0) valueBuf.append(", "); // NOI18N
valueBuf.append((String)hvals.nextElement());
++counter;
}
}
cd.setAttributeValue("formatsAccepted", valueBuf.toString()); //NOI18N
// Encoding
valueBuf = new StringBuffer(128);
counter = 0;
hvals = request.getHeaders(Constants.Http.acceptEncoding);
if(hvals != null) {
while(hvals.hasMoreElements()) {
if(counter > 0) valueBuf.append(", "); // NOI18N
valueBuf.append((String)hvals.nextElement());
++counter;
}
}
cd.setAttributeValue("encodingsAccepted", valueBuf.toString()); //NOI18N
//Char sets
valueBuf = new StringBuffer(128);
counter = 0;
hvals = request.getHeaders(Constants.Http.acceptCharset);
if(hvals != null) {
while(hvals.hasMoreElements()) {
if(counter > 0) valueBuf.append(", "); // NOI18N
valueBuf.append((String)hvals.nextElement());
++counter;
}
}
cd.setAttributeValue("charsetsAccepted", valueBuf.toString()); //NOI18N
}
/**
* Creates an instance of RequestData based on the request
*/
private void recordRequestData(RequestData rd,
HttpServletRequest request) {
if(debug) log(" recordRequestData()"); //NOI18N
// The method variable is used again below
String method = request.getMethod();
rd.setAttributeValue("uri", request.getRequestURI()); //NOI18N
rd.setAttributeValue("method", method); //NOI18N
String protocol = request.getProtocol();
while(protocol.endsWith("\n")) //NOI18N
protocol = protocol.substring(0, protocol.length()-2);
rd.setAttributeValue("protocol", protocol); //NOI18N
rd.setAttributeValue("ipaddress", request.getRemoteAddr());//NOI18N
if(debug) log(" doing query string"); //NOI18N
String queryString = request.getQueryString();
if(queryString == null || queryString.trim().equals("")) { //NOI18N
queryString = ""; //NOI18N
}
if(debug) log("Query string is: " + queryString); // NOI18N
// Parse it the way we do with the errors...
rd.setAttributeValue("queryString", queryString); //NOI18N
//NOI18N
rd.setAttributeValue("scheme", request.getScheme()); //NOI18N
if(debug) log(" doing headers"); //NOI18N
Headers headers = new Headers();
headers.setParam(recordHeaders(request));
rd.setHeaders(headers);
if(debug) log(" doing request attributes...in"); //NOI18N
RequestAttributesIn reqattrin = new RequestAttributesIn();
reqattrin.setParam(recordRequestAttributes(request));
rd.setRequestAttributesIn(reqattrin);
}
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
logger.info("访问 doGet");
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html");
String authType = request.getAuthType();
String localAddr = request.getLocalAddr();
Locale locale = request.getLocale();
String localName = request.getLocalName();
String contextPath = request.getContextPath();
int localPort = request.getLocalPort();
String method = request.getMethod();
String pathInfo = request.getPathInfo();
String pathTranslated = request.getPathTranslated();
String protocol = request.getProtocol();
String queryString = request.getQueryString();
String remoteAddr = request.getRemoteAddr();
int port = request.getRemotePort();
String remoteUser = request.getRemoteUser();
String requestedSessionId = request.getRequestedSessionId();
String requestURI = request.getRequestURI();
StringBuffer requestURL = request.getRequestURL();
String scheme = request.getScheme();
String serverName = request.getServerName();
int serverPort = request.getServerPort();
String servletPath = request.getServletPath();
Principal userPrincipal = request.getUserPrincipal();
String accept = request.getHeader("accept");
String referer = request.getHeader("referer");
String userAgent = request.getHeader("user-agent");
String serverInfo = this.getServletContext().getServerInfo();
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
// 这里<title></title>之间的信息在浏览器中显示为标题
out.println(" <HEAD><TITLE>Request Servlet</TITLE></HEAD>");
out.println(" <style>body, font, td, div {font-size:12px; line-height:18px; }</style>");
out.println(" <BODY>");
out.println("<b>您的IP为</b> " + remoteAddr + "<b>;您使用</b> " + getOS(userAgent) + " <b>操作系统</b>,"
+ getNavigator(userAgent) + " <b>。您使用</b> " + getLocale(locale) + "。<br/>");
out.println("<b>服务器IP为</b> " + localAddr + localAddr + "<b>;服务器使用</b> " + serverPort + " <b>端口,您的浏览器使用了</b> "
+ port + " <b>端口访问本网页。</b><br/>");
out.println("<b>服务器软件为</b>:" + serverInfo + "。<b>服务器名称为</b> " + localName + "。<br/>");
out.println("<b>您的浏览器接受</b> " + getAccept(accept) + "。<br/>");
out.println("<b>您从</b> " + referer + " <b>访问到该页面。</b><br/>");
out.println("<b>使用的协议为</b> " + protocol + "。<b>URL协议头</b> " + scheme + ",<b>服务器名称</b> " + serverName
+ ",<b>您访问的URI为</b> " + requestURI + "。<br/>");
out.println("<b>该 Servlet 路径为</b> " + servletPath + ",<b>该 Servlet 类名为</b> " + this.getClass().getName()
+ "。<br/>");
out.println("<b>本应用程序在硬盘的根目录为</b> " + this.getServletContext().getRealPath("") + ",<b>网络相对路径为</b> "
+ contextPath + "。 <br/>");
out.println("<br/>");
out.println("<br/><br/><a href=" + requestURI + "> 点击刷新本页面 </a>");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
private List<DebugInfo> createParts() throws ODataException {
List<DebugInfo> parts = new ArrayList<DebugInfo>();
// request
final HttpServletRequest servletRequest =
(HttpServletRequest) context.getParameter(ODataContext.HTTP_SERVLET_REQUEST_OBJECT);
final String protocol = servletRequest == null ? null : servletRequest.getProtocol();
parts.add(new DebugInfoRequest(context.getHttpMethod(), context.getPathInfo().getRequestUri(), protocol,
context.getRequestHeaders()));
// response
parts.add(new DebugInfoResponse(response, context.getPathInfo().getServiceRoot().toASCIIString()));
// server
if (servletRequest != null) {
parts.add(new DebugInfoServer(servletRequest));
}
// URI
Throwable candidate = exception;
while (candidate != null && !(candidate instanceof ExpressionParserException)) {
candidate = candidate.getCause();
}
final ExpressionParserException expressionParserException = (ExpressionParserException) candidate;
if (uriInfo != null
&& (uriInfo.getFilter() != null || uriInfo.getOrderBy() != null
|| !uriInfo.getExpand().isEmpty() || !uriInfo.getSelect().isEmpty())
|| expressionParserException != null && expressionParserException.getFilterTree() != null) {
parts.add(new DebugInfoUri(uriInfo, expressionParserException));
}
// runtime measurements
if (context.getRuntimeMeasurements() != null) {
parts.add(new DebugInfoRuntime(context.getRuntimeMeasurements()));
}
// exceptions
if (exception != null) {
final Locale locale = MessageService.getSupportedLocale(context.getAcceptableLanguages(), Locale.ENGLISH);
parts.add(new DebugInfoException(exception, locale));
}
return parts;
}