将servlet请求转发给另一台服务器 [英] Forward a servlet request to another server

查看:1311
本文介绍了将servlet请求转发给另一台服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Java Servlet API可以将请求转发到同一服务器(相同主机:端口)中的另一个路径。但是,转发到另一个主机:端口 - 就像代理做 - 是另一个故事。

Java Servlet API can forward requests to another path within the same server (identical host:port). But, forwarding to a different host:port — like proxy do — is another story.

我试图用Jersey Client做到这一点,调整 ServletRequest - 方法,标题,媒体类型和正文 - 泽西岛 ClientRequest 使用不同的基础uri ),进行呼叫,并适应泽西岛 ClientResponse - 方法,标题,媒体类型和正文 - 来自 ServletResponse

I've tried to do that with Jersey Client, adapting the ServletRequest — method, headers, mediatype and body — to a Jersey ClientRequest (with a different base uri), making the call, and adapting back the Jersey ClientResponse — method, headers, mediatype and body — to the ServletResponse.

我手动调整这些似乎不对。

Adapting those manually seems wrong to me.

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

Isn't there a pure Servlet API solution? Or an HTTP client capable of adapting requests back and forth when changing the host:port?

推荐答案

HTTP-Proxy-Servlet 完全符合您的需要。

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



web.xml



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 Integration



另见:< a href =https://github.com/mitre/HTTP-Proxy-Servlet/issues/15 =noreferrer> HTTP-Proxy-Servlet Issue#15

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



ServletWrappingControllerExt.java



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);
    }
}



Beans配置



Beans configuration

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

这篇关于将servlet请求转发给另一台服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆