如何通过JAVA在浏览器中打开网站,而无需在浏览器中打开新选项卡

时间:2021-01-22 17:41:35

Introduction

I am making a proxy server in java. Whenever the user enters an unknown host (URL) in the browser, the proxy server handles UnknownHostException by executing the code below:

我在java中制作代理服务器。每当用户在浏览器中输入未知主机(URL)时,代理服务器通过执行以下代码来处理UnknownHostException:

try {
        Process p=Runtime.getRuntime().exec("cmd /c start http://www.mysite.com/unknownhosterror.htm"); 
    }
    catch(IOException io) {
        System.out.println("Error");
    }

What these lines of code do is to display an html file containing "This page could not be displayed." whenever the user entered a non-existing URL.

这些代码行的作用是显示包含“无法显示此页面”的html文件。每当用户输入不存在的URL时。

Problem

The code above opens a new tab and displays the content of www.mysite.com/unknownhosterror.htm. What I want is to redirect to it.

上面的代码打开一个新选项卡并显示www.mysite.com/unknownhosterror.htm的内容。我想要的是重定向到它。

For example, I wrote www.nosuchsite.com in the URL bar. Suppose there is no such site, it will automatically redirect to www.mysite.com/unknownhosterror.htm and display "This page could not be displayed.".

例如,我在URL栏中写了www.nosuchsite.com。假设没有这样的站点,它将自动重定向到www.mysite.com/unknownhosterror.htm并显示“此页面无法显示”。

How can I do this?

我怎样才能做到这一点?

EDIT NOTE: I do not use Servlet.

编辑注意:我不使用Servlet。

1 个解决方案

#1


0  

I may be misunderstanding what you mean but if you indeed have a proxy server, they you should be able to issue a 301 redirect back to the browser when the proxy server detects the UnknownHostException.

我可能误解了你的意思,但如果你确实有一个代理服务器,那么当代理服务器检测到UnknownHostException时,你应该能够将301重定向发回给浏览器。

In the response to the browser, you need to add something like the following lines to the header of your response:

在对浏览器的响应中,您需要在响应的标题中添加以下行:

HTTP/1.1 301 Moved Permanently
Location: http://www.mysite.com/unknownhosterror.htm

How to add that to your headers depends highly on how you are handling the requests. If you show a little bit of your proxy handler code, I can provide more information.

如何将其添加到标题中很大程度上取决于您处理请求的方式。如果您显示一些代理处理程序代码,我可以提供更多信息。

#1


0  

I may be misunderstanding what you mean but if you indeed have a proxy server, they you should be able to issue a 301 redirect back to the browser when the proxy server detects the UnknownHostException.

我可能误解了你的意思,但如果你确实有一个代理服务器,那么当代理服务器检测到UnknownHostException时,你应该能够将301重定向发回给浏览器。

In the response to the browser, you need to add something like the following lines to the header of your response:

在对浏览器的响应中,您需要在响应的标题中添加以下行:

HTTP/1.1 301 Moved Permanently
Location: http://www.mysite.com/unknownhosterror.htm

How to add that to your headers depends highly on how you are handling the requests. If you show a little bit of your proxy handler code, I can provide more information.

如何将其添加到标题中很大程度上取决于您处理请求的方式。如果您显示一些代理处理程序代码,我可以提供更多信息。