- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
突然停滞在生成自定义 servlet 响应上。我想用预定义的替换 servlet 响应:
public class MyCustomResponse extends HttpServletResponseWrapper {
private String customOutput;
public MyCustomResponse(String customOutput, HttpServletResponse response) {
super(response);
// PrintWriter and Outputstream should stream this variable as output
this.customOutput = customOutput;
}
//
// Below I need to override something
//
}
过滤代码如下:
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
//
//
MyCustomResponse customResponse = new MyCustomResponse("Hello world!", (HttpServletResponse) response);
chain.doFilter(request, customResponse);
}
真可惜,但我真的坚持编写这个简单的任务:(
如有任何帮助,我们将不胜感激。
更新:
我想要的只是实现自定义响应包装器,一旦将其放入过滤器链中,它就会始终以一些预定义的文本进行响应。我知道如何从 doFilter() 方法中编写自定义数据,但我希望 MyCustomResponse
对此负责 - 只需实例化并放入链中。也欢迎任何有充分理由的回答“你不能那样做,因为......”。
最佳答案
如您的评论之一所述:
"I want my custom response to return a string in response to getWriter or getOutputStream method invocation"
为此,您必须通过覆盖它们来为 getWriter() 和 getOutputStream() 提供您自己的实现。
//---
private PrintWriter printWriter = null;
private ServletOutputStream outputStream = null;
public PrintWriter getWriter( ) throws IOException {
if (this.outputStream != null) {
throw new IllegalStateException(
"Cannot call getWriter( ) after getOutputStream( )");
}
if (this.printWriter == null) {
// initialize printWriter
}
return this.printWriter;
}
public ServletOutputStream getOutputStream( ) throws IOException {
if (this.printWriter != null) {
throw new IllegalStateException(
"Cannot call getOutputStream( ) after getWriter( )");
}
if (this.outputStream == null) {
// initialize outputStream
}
return this.outputStream;
}
//---
关于java - 在创建自定义 HttpServletResponse 方面需要帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4712920/
我使用 Spring mvc,我的问题是关于 sendError方法来自 HttpServletResponse . 有人可以告诉我哪个最好: @RequestMapping(method = Req
前言: Servlet 中的 doXXX 方法的目的就是根据请求计算得到相应,然后把响应的数据设置到 HttpServletResponse 对象中;然后 Tomcat 就会把这个 HttpServl
在 java 中向 HttpServletResponse 添加一些内容时,我可以获取响应编写器并附加: httpResponse.getWriter().append("Some Content")
我正在开发一个将 http 请求(GET 用于测试目的)发送到 java servlet 的设置。它的工作原理是,ser let 从浏览器获取请求,解析它并通过 TCP 套接字将其发送到“主”服务器,
我正在构建一个网络应用程序。 此 Web 应用程序的一部分是从客户端到服务器的 ajax 请求: $.ajax({ url: url, type: "get", d
关闭。这个问题需要多问focused 。目前不接受答案。 想要改进此问题吗?更新问题,使其仅关注一个问题 editing this post . 已关闭 9 年前。 Improve this ques
在 servlet 应用程序中,Web 服务器将 HttpServletResponse 转发到浏览器,对吗?然后浏览器将渲染它并显示 html。 但是浏览器期望的 Response 对象的确切类型是
我在为我在 Web 应用程序上创建的 CSV 文件设置字符集时遇到问题(在 Tomcat9 服务器上的 Windows 笔记本电脑上进行测试)。这就是我所做的: byte[] data = resul
我有一个 Java 网络服务应用程序,我正在尝试执行导出功能以将一些数据从数据库导出到 excel 文件 为此,我使用了 HttpServletResponse,但即使我设置了文件名和编码类型,导出的
我正在尝试设置一个可用于异步调用 Web 服务的 servlet。在大多数情况下,它运行良好。我有一个带有 doGet 方法的 servlet 和一个调用它的 js。我在 js 中有一个回调方法,当
我使用doGet方法来获取参数,然后进行处理。发送回复时,其中包含状态数据。此外,当第二个客户端连接时,答案包含来自第一个客户端的数据。我希望在第二个客户端连接后发送新的数据。重置缓冲区并向第二个客户
编译以下代码时遇到问题 public void doGet(HttpServletRequest request, HttpServletResponse response) throws Servl
我有以下问题:我有一个 HttpServlet 创建一个文件并将其返回给必须以下载形式接收它的用户 byte[] byteArray = allegato.getFile(); InputStream
我正在寻找一种 Autowiring HttpServletResponse 的方法。它不适用于开箱即用的 Spring ,但我发现 this description .这可行,但有点烦人,在那个 S
我创建了一个自定义过滤器来修改 http 响应的内容。内容替换本身工作正常,但是当内容大小大于原始响应时,它会被截断为与原始响应相同的大小,同时丢失剩余字符。 这是我的自定义过滤器: public c
在我的应用程序中,我有 2 个过滤器: 1) 首先,抛出异常,我调用 response.sendError(UNAUTHORIZED.getStatusCode(), "My message"); 2
我有一个 HttpServletResponse 对象,需要编写一个包含在 jar 中的文件。以下代码段对我不起作用。 URI uri = .class.getResource("/" + filen
只是一个简短的问题 - sendRedirect 使用什么方法(例如 GET、POST 等)?它是否从请求中继承它?而且,如果是这样,是否可以将其更改为另一个?谢谢! 最佳答案 sendRedirec
我看到一些帖子说Servlet Filter中的repsonse.sendError之后需要返回,是否需要?如果是这样,为什么? public class AuthorizationSecurityF
我正在我的网络应用程序过滤器中运行,该过滤器从外部源接收有关用户的信息,无论他是否登录。这是我的过滤器: @Override public void doFilter( ServletRequest
我是一名优秀的程序员,十分优秀!