Saturday, April 7, 2012

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();
}

}
}

No comments:

Post a Comment