Tuesday, September 15, 2009

call servlet from java, call the URL using java stand alone program

1. MyJsp.jsp

<html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>Insert title here</title></head><body>THIS IS BINOD JSP PAGE<%
System.out.println("THIS IS OUTPUT FROM JSP PAGE");
%></body></html>

2. Stand alone Java Code
callURL.java

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

public class CallURL {
public static void main(String[] args) {
callURL();
}
public static void callURL(){
String urlName = "http://localhost:8080/TestServlet/MyJsp.jsp";
URL url;
try {
url = new URL(urlName);
URLConnection conn = url.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
System.out.println("Output from Server :: "+line);
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

}
}

Output from Client
Output from Server :: <html>Output from Server :: <head>Output from Server :: <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">Output from Server :: <title>Insert title here</title>Output from Server :: </head>Output from Server :: <body>Output from Server :: THIS IS BINOD JSP PAGEOutput from Server :: Output from Server :: </body>Output from Server :: </html>

Output from Server Console:
21:46:11,453 INFO [STDOUT] THIS IS OUTPUT FROM JSP PAGE

No comments:

Post a Comment

Please put your feedback or your any question related to this blog. Thanks.