我编写了一个用于检查http标头的servlet,但我不知道为什么在加载页面时会自动开始下载。
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
// ===================================================
package HttpHeader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;
import java.util.zip.GZIPOutputStream;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
// ===================================================
public class HeaderServlet extends HttpServlet {
private static final long serialVersionUID = 1234L;
private ServletConfig m_ServletConfig;
private ServletContext m_ServletContext;
private boolean m_HasCompress;
private int m_ContentLength;
private Cookie[] m_Cookies;
private String m_AuthenticationType;
private String m_RemoteUser;
private String m_ContentType;
private String m_Method;
private String m_RequestURI;
private String m_Date;
private String m_QueryStr;
private String m_Protocol;
private Enumeration<String> m_HeaderNames;
private HttpSession m_Session;
//======================================================
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
/* TODO output your page here
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet HeaderServlet</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Servlet HeaderServlet at " + request.getContextPath () + "</h1>");
out.println("</body>");
out.println("</html>");
*/
} finally {
out.close();
}
}
@Override
protected void doHead(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String encoding = request.getHeader("Accept-Header");
if(encoding != null && encoding.indexOf("gzip") != -1)
{
m_HasCompress = true;
}
else
{
m_HasCompress = false;
}
m_ContentLength = request.getContentLength();
m_Cookies = request.getCookies();
// Basic, Form, Client certification authentication,
// digest authentication
// Same with CGI AUTH_TYPE
m_AuthenticationType = request.getAuthType();
m_RemoteUser = request.getRemoteUser();
m_ContentType = request.getContentType();
// Return get, post, delete, put
// Same with CGI REQUEST_METHOD
m_Method = request.getMethod();
m_RequestURI = request.getRequestURI();
// Same with CGI QUERY_STRING
m_QueryStr = request.getQueryString();
// Same with CGI SERVER_PROTOCOL
m_Protocol = request.getProtocol();
m_HeaderNames = request.getHeaderNames();
m_Session = request.getSession(true);
/* Accept
* = MIME type
*
* Accept-Charset
* = UTF-8
*
* Accpet-Encoding
* = Compression - gzip
*
* Accept-Language
* = us
*
* Authorization
* = Login
*
* Content-Length
* = Post request
*
* Cookie
* Host
*
* If-Modified-since
* = Implements getLastModified
* = Get request
*
* If-Unmodified-since
* = Post request
*
* Referer
* = Request origin
*
* User Agent
* = Mozilla
*/
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
PrintWriter out = null;
try {
doHead(request, response);
response.setContentType("/text/html;charset=UTF-8");
response.setBufferSize(8192);
if(m_HasCompress)
{
out = new PrintWriter(new GZIPOutputStream(response.getOutputStream()));
response.setHeader("Content-Encoding", "gzip");
}
else
{
out = response.getWriter();
}
if (out != null) {
String docType = "<!DOCTYPE HTML PUBLIC "
+ "\"-//W3C//DTD HTML 4.01 "
+ "Transitional//EN\">\n\n";
out.println(docType
+ "<html> <head> "
+ " <title> Http Request Header<title>"
+ "Requst Method : " + m_Method + "<p></p>"
+ "Request URI : " + m_RequestURI + "<p></p>"
+ "Request Protocol : " + m_Protocol + "<p></p><p></p>"
+ "<table BORDER=1 ALIGN =\"CENTER\">"
+ "<TH>Header Name <TH>Header Value");
String headerName = new String();
String headerValue = new String();
while(m_HeaderNames.hasMoreElements())
{
headerName = m_HeaderNames.nextElement();
if(headerName != null)
{
headerValue = request.getHeader(headerName);
out.println("<TR><TD>" + headerName);
out.println(" <TD>" + headerValue);
}
}
out.println("</table>" + "<p></p>"
+ "</head></html>");
out.close();
}
}
finally {
out.close();
}
/*
* HTTP 1.1 request headers
* Request Header set by browser
* Get Reqeust - Query Data same line
* POst Request - Query Data next line
*
*/
//processRequest(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// processRequest(request, response);
}
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>
}
// ===================================================
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib prefix="f" uri="http://java.sun.com/jsf/core"%>
<%@taglib prefix="h" uri="http://java.sun.com/jsf/html"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<%--
This file is an entry point for JavaServer Faces application.
--%>
<f:view>
<html>
<head>
<meta http-equiv="REFRESH" content="0; url=HeaderServlet; charset=UTF-8"/>
<title>JSP Page</title>
</head>
<body>
<h1><h:outputText value="JavaServer Faces"/></h1>
</body>
</html>
</f:view>
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<context-param>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Development</param-value>
</context-param>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet>
<servlet-name>HeaderServlet</servlet-name>
<servlet-class>HttpHeader.HeaderServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>/faces/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>HeaderServlet</servlet-name>
<url-pattern>/HeaderServlet</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>faces/welcomeJSF.jsp</welcome-file>
</welcome-file-list>
</web-app>
加载的网址为http://localhost:8080/HttpHeader/。 我尝试调试但从未发现任何奇怪的东西。 请帮我。
感谢。
答案 0 :(得分:2)
变化
response.setContentType("text/html;charset=UTF-8");
//instead of:
//response.setContentType("/text/html;charset=UTF-8");
小小的adivce: 记住servlet的无状态特性 - servlet是单例。这意味着,在您的情况下,如果2个客户端调用您的servlet,结果将是不可预测的。这是因为你有许多字段应该是每个客户端特定的,实际上这些字段可能被错误地修改,因为同时调用Servlet:对于一个客户端它将工作,对于多个servlet可以被打破 - 如果您有多个客户,请更改您的实施。
答案 1 :(得分:0)
o从代码中删除这些行(或注释它们)
response.setContentType("/text/html;charset=UTF-8");
response.setBufferSize(8192);
response.setHeader("Content-Encoding", "gzip");
这将停止下载页面
您将获得一个包含请求详细信息的输出...
希望这会有所帮助......