简体   繁体   English

服务器端Web应用程序中的页面重定向

[英]Page redirecting in a web application at server side

I have two websites say 我有两个网站说

1. firstwebsite.com
2. secondwebsite.com

All the pages in firstwebsite are static. firstwebsite中的所有页面都是静态的。 Now I want to move all pages in firstwebsite.com to secondwebsite.com. 现在,我想将firstwebsite.com中的所有页面移至secondwebsite.com。 I want to shutdown all servers of firstwebsite.com 我想关闭firstwebsite.com的所有服务器

I also want to make sure that when the URL firstwebsite.com/** should redirect to secondwebsite.com/** 我还想确保当URL firstwebsite.com/**重定向到secondwebsite.com/**时

firstwebsite.com/abc.html should redirect to secodnwebsite.com/abc.html firstwebsite.com/abc.html应该重定向到secodnwebsite.com/abc.html

I am using springs. 我正在使用弹簧。 Can some one guide me how to do it. 有人可以指导我怎么做。 Later I also want to do some processing based on device from which request is sent. 稍后,我也想根据发送请求的设备进行一些处理。

It would be really great if I can get some references for above. 如果能从上面获得一些参考,那将是非常不错的。

Java EE has Filters which run in every request. Java EE具有可在每个请求中运行的过滤器。 All request to firstwebsite.com will be handled ,first in the code, by a filter then redirected to secodnwebsite.com . firstwebsite.com所有请求将首先在代码中由过滤器处理,然后重定向到secodnwebsite.com

public class RedirectionFilter implements Filter {

    /**
     * Default constructor.
     */
    public RedirectionFilter () {
        // TODO Auto-generated constructor stub
    }

    /**
     * @see Filter#destroy()
     */
    @Override
    public void destroy() {
        // TODO Auto-generated method stub
    }

    @Override
    public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {

            HttpServletRequest request = (HttpServletRequest) req;
            HttpServletResponse response = (HttpServletResponse) res;

            String servletPath = request.getServletPath();

            if (servletPath.endsWith(".html")) {
                response.sendRedirect("http://secondwebsite.com" + servletPath);
            }
            else {
                chain.doFilter(request, response);
            }
}
@Override
    public void init(FilterConfig fConfig) throws ServletException {
        // TODO Auto-generated method stub

    }
}

Do not forget to add this filter to deployment descriptor (web.xml). 不要忘记将此过滤器添加到部署描述符(web.xml)。 Put it to first order if you have another filters so that this filter runs first. 如果您还有其他过滤器,则将其放在第一位,以便该过滤器先运行。

<filter>
    <display-name>RedirectionFilter</display-name>
    <filter-name>RedirectionFilter</filter-name>
    <filter-class>your.package.RedirectionFilter</filter-class>

  </filter>
  <filter-mapping>
    <filter-name>RedirectionFilter</filter-name>
     <servlet-name>Servlet which firstwebsite.com run</servlet-name>
  </filter-mapping>

See also 也可以看看

Servlet Filters Servlet过滤器

Filter Interface 筛选介面

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Java Socket Server-重定向到Web应用程序(Web服务) - Java Socket Server — redirecting to Web Application (web service) 从服务器端触发网页打印 - Triggering a web page print from server side 过滤器:客户端和服务器端重定向 - Filters: client and server side redirecting 在java web应用程序中如何从客户端打印服务器文件 - In java web application how to print server file from client side 服务器端Java Web应用程序中的Google OAuth2流 - Google OAuth2 Flow in Server Side Web Application in Java 在Java Web应用程序中获取JSON数据服务器端 - Get json data server side in java web application 可以创建一个运行服务器端程序的Java Web Start应用程序吗? - Possible to create a Java Web Start application that runs server side programs? 从服务器端脚本中打印出Java Web应用程序 - print out of java web application from server side script Struts Web应用程序:可重用的验证客户端和服务器端 - Struts Web Application: Reusable Validation Client-Side & Server-Side 对于任何Web或应用程序服务器,我们如何提高或增加服务器端的Spring连接处理能力? - How could we improve or increase spring connection handling capacity on the server side for any web or application server?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM