gpt4 book ai didi

http - 将 servlet 请求转发到另一台服务器

转载 作者:可可西里 更新时间:2023-11-01 15:10:27 33 4
gpt4 key购买 nike

Java Servlet API 可以将请求转发到同一服务器内的另一条路径(相同的主机:端口)。但是,转发到不同的主机:端口——就像代理一样——是另一回事。

我已经尝试用 Jersey Client 来做到这一点,调整 ServletRequest — 方法、 header 、媒体类型和正文 — 到 Jersey ClientRequest (使用不同的 base uri),进行调用,然后调整回 Jersey ClientResponse — 方法、 header 、媒体类型和正文 — 到 ServletResponse .

对我来说,手动调整这些似乎是错误的。

没有纯Servlet API的解决方案吗?或者 HTTP 客户端能够在更改主机:端口时来回调整请求?

最佳答案

HTTP-Proxy-Servlet完全满足您的需求。

快速配置

pom.xml

<dependency>
<groupId>org.mitre.dsmiley.httpproxy</groupId>
<artifactId>smiley-http-proxy-servlet</artifactId>
<version>1.7</version>
</dependency>

web.xml

<servlet>
<servlet-name>solr</servlet-name>
<servlet-class>org.mitre.dsmiley.httpproxy.ProxyServlet</servlet-class>
<init-param>
<param-name>targetUri</param-name>
<param-value>http://solrserver:8983/solr</param-value>
</init-param>
<init-param>
<param-name>log</param-name>
<param-value>true</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>solr</servlet-name>
<url-pattern>/solr/*</url-pattern>
</servlet-mapping>

Spring 整合

另请参阅:HTTP-Proxy-Servlet Issue #15

pom.xml

<dependency>
<groupId>org.mitre.dsmiley.httpproxy</groupId>
<artifactId>smiley-http-proxy-servlet</artifactId>
<version>1.7</version>
</dependency>

ServletWrappingControllerExt.java

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.ServletWrappingController;

public class ServletWrappingControllerExt extends ServletWrappingController
{
private String pathToStrip;

public void setPathToStrip(String pathToStrip)
{
this.pathToStrip = pathToStrip;
}

@Override
protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response)
throws Exception
{
final HttpServletRequest wrapper = new HttpServletRequestWrapper(request)
{
@Override
public String getPathInfo()
{
//Please note that getPathInfo returns null if DispatcherServlet is configured to track url-pattern "/"
//It should be configured to track url-pattern "/*". Below is a sample DispatcherServlet configuration
/*
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
*/
String path = super.getPathInfo();
if (path.startsWith(pathToStrip))
{
final int length = pathToStrip.length();
path = path.substring(length);
}
return path;
}

@Override
public String getServletPath()
{
return super.getServletPath();
}
};

return super.handleRequestInternal(wrapper, response);
}
}

Bean 配置

<bean id="myServletWrapper" class="ServletWrappingControllerExt">
<property name="pathToStrip" value="/solr"/>
<property name="servletClass" value="org.mitre.dsmiley.httpproxy.ProxyServlet" />
<property name="servletName" value="solr" />
<property name="initParameters">
<props>
<prop key="targetUri">http://solrserver:8983/solr</prop>
<prop key="log">true</prop>
</props>
</property>
</bean>

<bean id="myServletUrlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="urlMap">
<map>
<entry key="/solr/**" value-ref="myServletWrapper" />
</map>
</property>
<property name="order" value="1" />
</bean>

关于http - 将 servlet 请求转发到另一台服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10860569/

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