gpt4 book ai didi

Java Servlet 下载

转载 作者:行者123 更新时间:2023-12-02 08:20:39 27 4
gpt4 key购买 nike

我编写了一个servlet,用于检查http header ,但我不知道为什么当页面加载时,它会自动开始下载。

    /*
* 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>

加载的 URL 为 http://localhost:8080/HttpHeader/ 。我尝试调试但从未发现任何奇怪的地方。请帮我。

谢谢。

最佳答案

改变

response.setContentType("text/html;charset=UTF-8");
//instead of:
//response.setContentType("/text/html;charset=UTF-8");
<小时/>

小建议:请记住 servlet 的无状态性质 - servlet 是单例。这意味着,在您的情况下,如果 2 个客户端调用您的 servlet,结果将是不可预测的。这是因为您有许多字段应该针对每个客户端,实际上这些字段可能会被错误地修改,因为同时调用 Servlet:对于一个客户端,它会工作,对于多个客户端,您的 servlet 可能会被破坏 -如果您有多个客户,请更改您的实现方式。

关于Java Servlet 下载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5528699/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com