One, call the servlet description in Java:
We may sometimes need to invoke the servlet in the Java class to implement some special functionality, and a class of URLs is provided in Javaapi, where the OpenStream () method can open the connection to the URL and return a inputstream that is used for the connection to read in.
Second, Java calls the servlet application example:
Package com.solid.test;
Import java.io.BufferedReader;
Import java.io.IOException;
Import java.io.InputStreamReader;
Import java.net.MalformedURLException;
Import Java.net.URL;
public class Testcallservlet {
Servlet path
private static final String path = " Http://localhost:8080/test/servlet/TestServlet ";
/**
* Test Call servlet method
* @param args
*/
public static void Main (string[] args) {
try {
URL url = new URL (path);
Url.openstream ();
} catch (Malformedurlexception e) {
E.printstacktrace ();
} catch (IOException e) {
E.printstacktrace ();
}
}
}
third, to read a page source code application example:
Package com.solid.test;
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 Testreadersource {
URL path
private static final String path = " http://www.google.cn ";
Read source code method test for page under path path
public static void Main (string[] args) {
BufferedReader br = null;
String source;
try {
URL url = new URL (path);
URLConnection conn = Url.openconnection ();
br = new BufferedReader (New InputStreamReader (Conn.getinputstream ()));
while ((Source=br.readline ())!=null) {
SYSTEM.OUT.PRINTLN (source);
}
} catch (Malformedurlexception e) {
E.printstacktrace ();
} catch (IOException e) {
E.printstacktrace ();
} finally {
try {
if (br! = null) {
Br.close ();
}
} catch (IOException e) {
E.printstacktrace ();
}
}
}
}
Calling Servlet in Java class