First, use java as the server, use browser as the client, let browser link to java Server
Every browser will send a "secret code" first when sending a request. If the other party can match the secret code, it is his own person, and then he will send data This code is our http protocol
Use java as the client and tomcat as the server:
package Network programming; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.net.Socket; import java.net.UnknownHostException; public class Client { public static void main(String[] args) throws UnknownHostException, IOException { System.out.println("Did I do it"); Socket tomcat=new Socket("localhost",8080); System.out.println("Did I do it"); //Receiving information InputStream in=tomcat.getInputStream(); InputStreamReader isr=new InputStreamReader(in); BufferedReader br=new BufferedReader(isr); //Send password OutputStream out=tomcat.getOutputStream(); OutputStreamWriter osw=new OutputStreamWriter(out); BufferedWriter bw=new BufferedWriter(osw); //Password, imitating http protocol bw.write("GET /baidu/1.html HTTP/1.1"); bw.newLine(); bw.write("Host: localhost:8080"); bw.newLine(); bw.write("Connection: close"); bw.newLine(); bw.write("Cache-Control: max-age=0"); bw.newLine(); bw.write("Upgrade-Insecure-Requests: 1"); bw.newLine(); bw.write("User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36"); bw.newLine(); bw.write("Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8"); bw.newLine(); bw.write("Accept-Encoding: gzip, deflate, br"); bw.newLine(); bw.write("Accept-Language: zh-CN,zh;q=0.9,en;q=0.8"); bw.newLine(); bw.write("Cookie: Webstorm-9770a2b9=e9c5983e-de71-494b-add2-1f771d40a956"); bw.newLine(); bw.newLine(); //Note that there are two new lines after the protocol, which is also the content of the protocol. bw.newLine(); bw.flush(); //Receive String msg=null; while((msg=br.readLine())!=null){ System.out.println(msg); } System.out.println("over"); br.close(); } }
In this case, your Tomcat service port is occupied. I don't know what to do.
**Second, use browser as client, java as server, send html code to browser, let browser display page
package Network programming; import java.io.BufferedWriter; import java.io.IOException; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.net.ServerSocket; import java.net.Socket; public class Server { public static void main(String[] args) throws IOException { ServerSocket server=new ServerSocket(8888); System.out.println("Server to start.."); Socket llq=server.accept(); System.out.println("With client access"); OutputStream out=llq.getOutputStream(); OutputStreamWriter osw=new OutputStreamWriter(out); BufferedWriter bw=new BufferedWriter(osw); bw.write("HTTP/1.1 200 OK"); bw.newLine(); bw.write("Server: Apache-Coyote/1.1"); bw.newLine(); bw.write("ETag: W/\"7347-1184876416000\""); bw.newLine(); bw.write("Last-Modified: Thu, 19 Jul 2007 20:20:16 GMT"); bw.newLine(); bw.write("Content-Type: text/html"); bw.newLine(); bw.write("Content-Length: 7347"); bw.newLine(); bw.write("Date: Sat, 11 May 2019 08:22:31 GMT"); bw.newLine(); bw.newLine(); bw.newLine(); bw.flush(); bw.write("<div style='border:1px solid red; width:100px; height:100px'></div>"); bw.flush(); bw.close(); System.out.println("over"); } }
Output results: display on the web page: