集册 Java实例教程 调用Servlet并输出结果

调用Servlet并输出结果

欢马劈雪     最近更新时间:2020-01-02 10:19:05

530
调用Servlet并输出结果


import java.io.*;

import java.net.*;
//来 自 nowjava.com - 时  代  Java

public class Reverse {

    public static void main(String[] args) throws Exception {


        if (args.length != 2) {

            System.err.println("Usage:  java Reverse "

                    + "http://<location of your servlet/script>"

                    + " string_to_reverse");

            System.exit(1);

        }


        String stringToReverse = URLEncoder.encode(args[1], "UTF-8");


        URL url = new URL(args[0]);/**时代Java - nowjava.com**/

        URLConnection connection = url.openConnection();

        connection.setDoOutput(true);


        OutputStreamWriter out = new OutputStreamWriter(

                connection.getOutputStream());

        out.write("string=" + stringToReverse);

        out.close();


        BufferedReader in = new BufferedReader(new InputStreamReader(

                connection.getInputStream()));

        String decodedString;

        while ((decodedString = in.readLine()) != null) {

            System.out.println(decodedString);

        }

        in.close();

    }

}


展开阅读全文