GVKun编程网logo

如何在Java Servlet内以分块响应的形式发送Http预告片/页脚?(servlet分发器)

17

对于想了解如何在JavaServlet内以分块响应的形式发送Http预告片/页脚?的读者,本文将是一篇不可错过的文章,我们将详细介绍servlet分发器,并且为您提供关于"javax.servlet.

对于想了解如何在Java Servlet内以分块响应的形式发送Http预告片/页脚?的读者,本文将是一篇不可错过的文章,我们将详细介绍servlet分发器,并且为您提供关于"javax.servlet.http.HttpServlet" was not found、2018/9/6 jsp页面出现找不到"javax.servlet.http.HttpServlet、eclipse JavaEE版"javax.servlet.http.HttpServlet" was not found on the Java Build Path问题的解决办法、groovy servlet java.lang.NoSuchMethodError: javax.servlet.http.HttpServletResponse.getStatus()I的有价值信息。

本文目录一览:

如何在Java Servlet内以分块响应的形式发送Http预告片/页脚?(servlet分发器)

如何在Java Servlet内以分块响应的形式发送Http预告片/页脚?(servlet分发器)

基本上我的响应头包含

Transfer-encoding =分块,

Trailer = [我想发送的一些预告片,例如“ SomeTrailer”]

一旦完成将数据写入Servlet输出流,就将编写预告片“
SomeTrailer:[value]”,但是httpclient无法正确解析此预告片。httpclient将整个输入流(包括尾部)视为单个块。在将数据写入输出流之后,我也尝试过在响应标头中写入预告片,但没有成功。

请帮忙

我还没有找到任何好的资源。

答案1

小编典典

我最终为此编写了一个简单的单线程Web服务器。原来这很容易。服务器非常简单。虽然代码有点粗糙,但是主要思想就在那里。

它执行的操作是将文件内容作为第一个块发送,并将文件的校验和作为页脚发送。

import java.io.BufferedReader;import java.io.DataOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.net.ServerSocket;import java.net.Socket;import org.apache.commons.codec.digest.DigestUtils;import org.apache.commons.io.IOUtils;import org.apache.log4j.Logger;public class ChunkedResponseServer implements Runnable {  private static final Logger LOGGER = Logger.getLogger(ChunkedResponseServer.class);  // Space '' ''  static final byte SP = 32;  // Tab '' ''  static final byte HT = 9;  // Carriage return  static final byte CR = 13;  // Line feed character  static final byte LF = 10;  final int port;  private volatile boolean cancelled = false;  public ChunkedResponseServer(int port) {    LOGGER.info("Chunked response server running on port " + port);    this.port = port;  }  @Override  public void run() {    ServerSocket serverSocket = null;    try {      serverSocket = new ServerSocket(port);      while (!cancelled) {        final Socket connectionSocket = serverSocket.accept();        handle(connectionSocket);      }    } catch (final IOException e) {      throw new RuntimeException(e);    }  }  public void cancel() {    LOGGER.info("Shutting down Chunked response Server");    cancelled = true;  }  private void handle(Socket socket) throws IOException {    BufferedReader input = null;    DataOutputStream output = null;    try {      input = new BufferedReader(new InputStreamReader(socket.getInputStream()));      output = new DataOutputStream(socket.getOutputStream());      addHeaders(output);      addCRLR(output);      final String filename = readFilename(input);      final byte[] content = readContent(filename);      addContentAsChunk(output, content);      final String checksum = DigestUtils.md5Hex(content);      addLastChunkAndChecksumFooter(output, checksum);      addCRLR(output);    } finally {      IOUtils.closeQuietly(input);      IOUtils.closeQuietly(output);    }  }  private void addLastChunkAndChecksumFooter(DataOutputStream output, String checksum) throws IOException {    output.writeBytes("0");    addCRLR(output);    output.writeBytes("checksum: " + checksum);    addCRLR(output);  }  private void addContentAsChunk(DataOutputStream output, byte[] content) throws IOException {    output.writeBytes(Integer.toHexString(content.length));    addCRLR(output);    output.write(content);    addCRLR(output);  }  private void addCRLR(DataOutputStream output) throws IOException {    output.writeByte(CR);    output.writeByte(LF);  }  private void addHeaders(DataOutputStream output) throws IOException {    output.writeBytes("HTTP/1.1 200 OK");    addCRLR(output);    output.writeBytes("Content-type: text/plain");    addCRLR(output);    output.writeBytes("Transfer-encoding: chunked");    addCRLR(output);    output.writeBytes("Trailer: checksum");    addCRLR(output);  }  private String readFilename(BufferedReader input) throws IOException {    final String initialLine = input.readLine();    final String filePath = initialLine.split(" ")[1];    final String[] components = filePath.split("/");    return components[components.length - 1];  }  private byte[] readContent(String filename) throws IOException {    final InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream(filename);    return IOUtils.toByteArray(in);  }}

"javax.servlet.http.HttpServlet" was not found

在 eclipse 中,用 maven 新建 web 项目时,jsp 页面报错:

The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path.

说明缺少 httpServlet,解决方法是:

右键项目 - Properties-Targeted Runtimes - 选择自己要运行的服务器,比如 Apache Tomcat v7.0-ok!

据说也可以在 pom.xml 中添加

<dependency>
	<groupId>javax.servlet</groupId>
	<artifactId>servlet-api</artifactId>
	<version>2.5</version>
</dependency>

2018/9/6 jsp页面出现找不到

2018/9/6 jsp页面出现找不到"javax.servlet.http.HttpServlet

新建jsp文件后出现报错

The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path

解决:

在出现此错误的项目上单击鼠标右键》Build Path》Configure Build Path

 

这样就好了

eclipse JavaEE版

eclipse JavaEE版"javax.servlet.http.HttpServlet" was not found on the Java Build Path问题的解决办法

使用eclipse JavaEE 版,新建 Dynamic Web Project 项目。在项目里添加 JSP 文件,会在文件头部出现错误提示。提示语句为:The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path。如下图所示:

 

 

 

1. 选中项目右键打开快捷菜单。

2. 在快捷菜单中点击 Properties 打开对话框。

3. 在左边栏选择 Project Facets,相应的最右边栏选择 Runtimes 选项卡,选中对应的apache tomcat 并点击OK,

    即可解决该问题。如下图所示: 

 

 

groovy servlet java.lang.NoSuchMethodError: javax.servlet.http.HttpServletResponse.getStatus()I

groovy servlet java.lang.NoSuchMethodError: javax.servlet.http.HttpServletResponse.getStatus()I

java.lang.NoSuchMethodError: javax.servlet.http.HttpServletResponse.getStatus()I
at org.eclipse.jetty.server.handler.ErrorHandler.handle(ErrorHandler.java:111)
at org.eclipse.jetty.server.Response.sendError(Response.java:597)
at javax.servlet.http.HttpServlet.doGet(HttpServlet.java:187)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:689)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:769)
at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:585)
at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:143)
at org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:577)
at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:223)
at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1125)
at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:515)
at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:185)
at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1059)
at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:141)
at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:97)
at org.eclipse.jetty.server.Server.handle(Server.java:497)
at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:311)
at org.eclipse.jetty.server.httpconnection.onFillable(httpconnection.java:248)
at org.eclipse.jetty.io.AbstractConnection$2.run(AbstractConnection.java:540)
at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:610)
at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:539)

at java.lang.Thread.run(Thread.java:745)


问题原因: groovy libraries 中有个servlet api 那个是比较老的.而且被优先加载了.

解决方法: 将自己引用的servlet api 放在前面 

今天关于如何在Java Servlet内以分块响应的形式发送Http预告片/页脚?servlet分发器的讲解已经结束,谢谢您的阅读,如果想了解更多关于"javax.servlet.http.HttpServlet" was not found、2018/9/6 jsp页面出现找不到"javax.servlet.http.HttpServlet、eclipse JavaEE版"javax.servlet.http.HttpServlet" was not found on the Java Build Path问题的解决办法、groovy servlet java.lang.NoSuchMethodError: javax.servlet.http.HttpServletResponse.getStatus()I的相关知识,请在本站搜索。

本文标签: