Java Reference
In-Depth Information
try
{
theSite
=
args
[
0
];
// trim trailing slash
if
(
theSite
.
endsWith
(
"/"
))
{
theSite
=
theSite
.
substring
(
0
,
theSite
.
length
()
-
1
);
}
}
catch
(
RuntimeException
ex
)
{
System
.
out
.
println
(
"Usage: java Redirector http://www.newsite.com/ port"
);
return
;
}
try
{
thePort
=
Integer
.
parseInt
(
args
[
1
]);
}
catch
(
RuntimeException
ex
)
{
thePort
=
80
;
}
Redirector
redirector
=
new
Redirector
(
theSite
,
thePort
);
redirector
.
start
();
}
}
In order to start the redirector on port 80 and redirect incoming requests to
http://
www.cafeconleche.org/
, type:
D:
\
JAVA
\
JNP4
\
examples
\
09
&
gt
;
java
Redirector
http:
//www.cafeconleche.org/
Redirecting
connections
on
port
80
to
http:
//www.cafeconleche.org/
If you connect to this server via Telnet, this is what you'll see:
%
<
userinput
moreinfo
=
"none"
>
telnet
macfaq
.
dialup
.
cloud9
.
net
80
Trying
168.100
.
203.234
...
Connected
to
macfaq
.
dialup
.
cloud9
.
net
.
Escape
character
is
'
^]
'
.
GET
/
HTTP
/
1.0
HTTP
/
1.0
302
FOUND
Date:
Sun
Mar
31
12
:
38
:
42
EDT
2013
Server:
Redirector
1.1
Location:
http:
//www.cafeconleche.org/
Content
-
type:
text
/
html
&
lt
;
HTML
&
gt
;&
lt
;
HEAD
&
gt
;&
lt
;
TITLE
&
gt
;
Document
moved
&
lt
;/
TITLE
&
gt
;&
lt
;/
HEAD
&
gt
;
&
lt
;
BODY
&
gt
;&
lt
;
H1
&
gt
;
Document
moved
&
lt
;/
H1
&
gt
;
The
document
/
has
moved
to
&
lt
;
A
HREF
=
"http://www.cafeconleche.org/"
&
gt
;
http:
//www.cafeconleche.
org
/&
lt
;/
A
&
gt
;.
Please
update
your
bookmarks
&
lt
;
P
&
gt
;&
lt
;/
BODY
&
gt
;&
lt
;/
HTML
&
gt
;
Connection
closed
by
foreign
host
.
If, however, you connect with a web browser, you should be sent to
http://www.cafe‐
conleche.org/
with only a slight delay. You should never see the HTML added after the