Java Learning lesson 63rd-About client server && URL classes & URLConnection

Source: Internet
Author: User

Common client and server side

Client:

Browser: IE: Pop-up window, Cheetah: Pop-up window, multi-tab, gaining effect

Service side:

Server: tomcat:1. Processing request 2. Give answer

To get Tomcat to work for us, the related class classes written by Java must implement Interface Serverlet

The power of the browser lies in the analytic ability, many formats, can be resolved

Service side and Client principle

Custom Service side:

public static void Main (string[] args) throws IOException {ServerSocket ss = new ServerSocket (9090);//tomcat is 8080Socket so Cket = Ss.accept (); InputStream in = Socket.getinputstream (); System.out.println ("IP:" +socket.getinetaddress (). gethostaddress ()); byte[] buf = new Byte[1024];int len = In.read (BUF) ; String text = new string (Buf,0,len); System.out.println (text); PrintWriter out = new PrintWriter (Socket.getoutputstream (), true); Out.println ("Hello"); Socket.close (); Ss.close ();}


Use existing client IE to understand what requests the client sends to the server?

Request Line: Request Method /1.html Request Resource path: HTTP protocol version

Request message Header: Property Name: Property value

Request Body

Simulate a browser to get information

public static void Main (string[] args) throws IOException {socket s = new socket ("192.168.1.1", 8080);//access to Tomcat, browser information pri Ntwriter out = new PrintWriter (S.getoutputstream (), true); Out.println ("Get/http/..."); Out.println ("...."); O Ut.println ("...."); Out.println ("..."); Out.println ("..."); Out.println ("...."); InputStream in = S.getinputstream ( ); byte[] buf = new Byte[1024];int len = In.read (BUF); String line = new string (Buf,0,len); System.out.println (line);//Get is the local write HTML code s.close ();}

The resulting response:

Answer line: Protocol version of HTTP answer status code answer status description information

The reply message property. Property Name: Property value

Answer body:-> The above HTML source code

PS: The browser is too difficult to write, so many vendors are the application of the original IE kernel


Class URL

Class URL represents a Uniform resource locator, which is a pointer to the Internet "resource". A resource can be a simple file or directory, or a reference to a more complex object, such as a query to a database or search engine

Each URL is a URI, but not every URI is a URL

public static void Url_demo () throws malformedurlexception {String strURL = "Http://127.0.0.1:8080/myhtml/1.html?name= Wang ";//http://www.baidu.com/baidu?word=java, in the Baidu Address bar search javaurl url = new URL (strurl); System.out.println ("Protocol:" +url.getprotocol ());//Get Host Protocol System.out.println ("host:" +url.gethost ());// Gets the hostname System.out.println ("Port:" +url.getport ());//Gets the port System.out.println ("File:" +url.getfile ());// Gets the file System.out.println ("Path:" +url.getpath ());//Gets the path part System.out.println ("Query:" +url.getquery ());//Get Query part}

Protocol:http
host:127.0.0.1
port:8080
File:/myhtml/1.html?name=wang
Path:/myhtml/1.html
Query:name=wang

public static void Url_demo () throws IOException {String strURL = "Http://127.0.0.1:8080/myhtml/1.html?name=wang"; URL url = new URL (strurl); InputStream in = Url.openstream ();//Opens a connection to this URL and returns a inputstream that is used to read from the connection. /* * Principle: * OpenConnection () * Returns a URLConnection object that represents the connection to the remote object referenced by the URL. * URLConnection conn = url.openconnection (); * SYSTEM.OUT.PRINTLN (connection); * The bottom of the package is the HTTP parsing method of the URL, this object is the Java built-in can resolve the specific protocol object +socket * URLConnection class * 1.String str = Conn.getheaderfield (" Contend-type "); * SYSTEM.OUT.PRINTLN (str);//reply to a text type of message * then you can determine the type of text according to the result of the answer, according to the specified parser, parsing the data * is actually using the socket, plus protocol * 2.InputStream in = Conn. getInputStream (); also get 1.html response body * * So the principle of Url.openstream () is: * URLConnection conn = url.openconnection (); * InputStream in = conn. getInputStream (); */byte[] buf = new Byte[1024];int len = In.read (BUF); String str = new string (Buf,0,len); System.out.println (str);//Get 1.html response body, URL Help parse into HTML source in.close ();}


Common network Architectures
1.C/S: Client/server (client, server)

Early:

Vc++,vb are all doing C/s

Characteristics:

The structure of the software, the client and the service side need to write, development costs high, maintenance trouble

Benefits:

A client can share a subset of operations locally. 360 anti-virus, game 3D effect are all in the local operation

2.b/s: Brows er/server (browser, server)

Characteristics:

The structure of the software, only the development of the server, the client is directly replaced by the browser

Relatively low development costs and easier maintenance

Disadvantage: All operations are in the server

Java Learning lesson 63rd-About client server && URL classes & URLConnection

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.