URL

package cn.php;

import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class MyServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
    ServletConfig config = null;

    @Override
    public void init(ServletConfig config) throws ServletException {
        System.out.println("调用init方法开始");
        super.init(config);
        String color = config.getInitParameter("color");
        ServletContext context = config.getServletContext();
        System.out.println(color);
        System.out.println(context.getInitParameter("userName"));
        System.out.println("调用介绍方法结束");
    }

    protected void doGet(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }

    protected void doPost(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {

        URL url = new URL("http://localhost:8080/servletDemo/TestHttpURLConnectionPro");
        HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();

        // 设置是否向httpUrlConnection输出,因为这个是post请求,参数要放在
        // http正文内,因此需要设为true, 默认情况下是false;
        urlConn.setDoOutput(true);

        // 设置是否从httpUrlConnection读入,默认情况下是true;
        urlConn.setDoInput(true);

        // Post 请求不能使用缓存
        urlConn.setUseCaches(false);

        // 设定传送的内容类型是可序列化的java对象
        // (如果不设此项,在传送序列化对象时,当WEB服务默认的不是这种类型时可能抛java.io.EOFException)
        urlConn.setRequestProperty("Content-type",
                "application/x-java-serialized-object");

        // 设定请求的方法为"POST",默认是GET
        urlConn.setRequestMethod("POST");

        // 连接,上面对urlConn的所有配置必须要在connect之前完成,
        urlConn.connect();

        // 此处getOutputStream会隐含的进行connect (即:如同调用上面的connect()方法,
        // 所以在开发中不调用上述的connect()也可以)。
        OutputStream outStrm = urlConn.getOutputStream();

        // 现在通过输出流对象构建对象输出流对象,以实现输出可序列化的对象。
        ObjectOutputStream oos = new ObjectOutputStream(outStrm);

        // 向对象输出流写出数据,这些数据将存到内存缓冲区中
        oos.writeObject(new String("我是测试数据"));

        // 刷新对象输出流,将任何字节都写入潜在的流中(些处为ObjectOutputStream)
        oos.flush();

        // 关闭流对象。此时,不能再向对象输出流写入任何数据,先前写入的数据存在于内存缓冲区中,
        // 再调用下边的getInputStream()函数时才把准备好的http请求正式发送到服务器
        oos.close();

        // 调用HttpURLConnection连接对象的getInputStream()函数,
        // 将内存缓冲区中封装好的完整的HTTP请求电文发送到服务端。
        InputStream inStrm = urlConn.getInputStream(); // <===注意,实际发送请求的代码段就在这里
        int b=0;
        byte[] bt = new byte[16];
        while((b=inStrm.read(bt))!=-1){
            String str =  new String(bt, 0, b);  
            System.out.println(str);
        }
        inStrm.close();
    }
}
package cn.php;

import java.io.IOException;
import java.io.ObjectInputStream;

import javax.servlet.ServletException;
import javax.servlet.ServletInputStream;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class TestHttpURLConnectionPro
 */
public class TestHttpURLConnectionPro extends HttpServlet {
    private static final long serialVersionUID = 1L;
       
  
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }


    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println(request.getMethod());
        ServletInputStream inStrm = request.getInputStream();
        ObjectInputStream objStrm = new ObjectInputStream(inStrm);
        try {
            String str = (String)objStrm.readObject();
            System.out.println(str);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        objStrm.close();
        ServletOutputStream outStrm = response.getOutputStream();
        String str = "手里的分类算法理论是否落实陆地上飞机啊累死了发生了法拉利是轮流发生了放湿漉漉的粮食浪费流量";
        outStrm.write(str.getBytes());
        outStrm.flush();
        outStrm.close();
        
    }

}
原文地址:https://www.cnblogs.com/dapeng520/p/4622837.html