简体   繁体   中英

How do I open a website in browser through JAVA without having the browser open a new tab

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:

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.

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.

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

How can I do this?

EDIT NOTE: I do not use Servlet .

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 .

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM