gpt4 book ai didi

tomcat - 使用 servlet 别名的遗留 servlet 链接

转载 作者:行者123 更新时间:2023-11-28 23:06:19 25 4
gpt4 key购买 nike

我正在尝试使用 ServletExec 将现有应用程序从 IIS 迁移到 tomcat。除了有一个带有两个 servlet 的 servlet 链的情况外,我的一切都正常工作。

第一个 servlet 做一些处理,第二个 servlet 做一些翻译。

request-->ProcessServlet--> (untranslated html) -->TranslateServlet --> response

在我的例子中,我可以调用 ProcessServlet,它会生成带有未翻译标签的 html。第二个 servlet 看起来像是获取 html、定位众所周知的翻译标签并翻译它们并为浏览器生成可读的输出。

从谷歌搜索,看起来不再支持/推荐使用 servlet 别名的 servlet 链接(from this page)并且可以使用过滤器实现相同的功能,但类必须实现一个接口(interface),但不幸的是,在我的情况下我必须工作与现有的类文件。

This page指定概念但没有示例。

同样来自 this page

How to Configure a Servlet Chain Using Servlet Aliasing

Using the Servlet Aliasing subsection of the setup section in the Administration Tool, a list of servlets can be named for a particular URL request. In the servlets table, when adding a new mapping, you are allowed to enter a comma-separated list of servlets in the order in which they should be invoked when a request arrives for that particular URL. This configures a servlet chain to be invoked every time a request that matches the URL arrives.

任何人都可以指定一个示例或为我指明正确的方向,以说明如何让这个链从 web.xml 开始工作吗?

更新:

根据 kschneid 在他的回答中的概述,这是对我有用的完整实现

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class TranslateFilter implements Filter {

private FilterConfig config = null;

public void init(FilterConfig config) throws ServletException {
this.config = config;
}

public void destroy() {
config = null;
}

public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {

HttpServletResponse httpResponse = (HttpServletResponse) response;
MyHttpServletResponseWrapper processResponse = new MyHttpServletResponseWrapper(httpResponse);
chain.doFilter(request, processResponse );
String content = processResponse.toString();
config.getServletContext().log("CONTENT: " + content);
HttpServletRequest httpRequest = (HttpServletRequest) request;
MyHttpServletRequestWrapper processResponseAsRequest = new MyHttpServletRequestWrapper(httpRequest, content);

RequestDispatcher dispatch = request.getRequestDispatcher("/Translate");
response.setContentType("text/html");
dispatch.forward(processResponseAsRequest, response); // forward to translate servlet with response from process servlet as the request and the original response
}

}

class MyHttpServletResponseWrapper
extends HttpServletResponseWrapper {

private StringWriter sw = new StringWriter();

public MyHttpServletResponseWrapper(HttpServletResponse response) {
super(response);
}

public PrintWriter getWriter() throws IOException {
return new PrintWriter(sw);
}

public ServletOutputStream getOutputStream() throws IOException {
throw new UnsupportedOperationException();
}

public String toString() {
return sw.toString();
}
}

class MyHttpServletRequestWrapper
extends HttpServletRequestWrapper {
private String content;
public MyHttpServletRequestWrapper(HttpServletRequest request) {
super(request);
}

public MyHttpServletRequestWrapper(HttpServletRequest request, String content) {
super(request);
this.content = content;
}

public ServletInputStream getInputStream()
{
return new MyServletInputStream(content);
}

public BufferedReader getReader()
{
InputStreamReader in= new InputStreamReader(getInputStream());
return new BufferedReader(in);
}
}

class MyServletInputStream extends ServletInputStream
{
private InputStream is;

public MyServletInputStream(String content)
{
is = new ByteArrayInputStream(content.getBytes());
}

public int read() throws IOException
{
return is.read();
}
}

最佳答案

我不太熟悉 servlet 链接的实现细节,但这里有一个可能有效的通用方法。将两个 servlet 映射到不同的 URL:

<servlet-mapping>
<servlet-name>process</servlet-name>
<url-pattern>/process</url-pattern>
</servlet-mapping>

<servlet-mapping>
<servlet-name>translate</servlet-name>
<url-pattern>/translate</url-pattern>
</servlet-mapping>

然后将过滤器映射到 process servlet:

<filter-mapping>
<filter-name>processChain</filter-name>
<servlet-name>process</servlet-name>
</filter-mapping>

processChain 过滤器会执行如下操作:

public void doFilter(ServletRequest request,
ServletResponse response,
FilterChain chain) throws IOException, ServletException {
ServletResponseWrapper processResponse = ...; // response buffer
chain.doFilter(request, processResponse); // let process servlet populate response buffer
ServletRequestWrapper processResponseAsRequest = ...; // use processResponse to create request for translate servlet
RequestDispatcher dispatch = request.getRequestDispatcher("/translate");
dispatch.forward(processResponseAsRequest, response); // forward to translate servlet with response from process servlet as the request and the original response
}

...或类似的东西 ;)

关于tomcat - 使用 servlet 别名的遗留 servlet 链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8974101/

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