JAVA 63rd-about Client Server & amp; URL Class & amp; URLConnection

Source: Internet
Author: User

JAVA 63rd-about Client Server & amp; URL Class & amp; URLConnection

Common clients and servers

Client:

Browser: IE: pop-up window, Cheetah: pop-up window, multiple tags, competitive effect

Server:

Server: TomCat: 1. process the request 2. Respond

To make TomCat work for us, the related classes written in java must implement interface Serverlet.

The powerful aspect of a browser is its parsing capability. It can be parsed in many formats.

Server and client principles

Custom Server:

Public static void main (String [] args) throws IOException {ServerSocket ss = new ServerSocket (9090); // Tomcat is 8080 Socket socket = 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 the existing client IE to find out 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 obtain information

Public static void main (String [] args) throws IOException {Socket s = new Socket ("192.168.1.1", 8080); // get it from tomcat, browser information PrintWriter out = new PrintWriter (s. getOutputStream (), true); out. println ("GET/HTTP /..... "); out. println (".... "); out. 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); // obtain the locally written html code s. close ();}

Response:

Response line: http Protocol version response status code Description

Response Message attributes. Property name: Property Value

Response body:-> above html source code

PS: the browser is too hard to write, so many vendors are applying the original IE Kernel


Class URL

ClassURLRepresents a unified resource identifier, 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 of a database or search engine.

Every 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, search for javaURL url = new URL (strurl) in the Baidu address bar; System. out. println ("Protocol:" + url. getProtocol (); // obtain the host protocol System. out. println ("Host:" + url. getHost (); // obtain the host name System. out. println ("Port:" + url. getPort (); // obtain the port System. out. println ("File:" + url. getFile (); // obtain the file System. out. println ("Path:" + url. getPath (); // obtain the path part of the System. out. println ("Query:" + url. getQuery (); // obtain the 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 (); // open the connection to this URL and return an InputStream used to read from this connection. /** Principle: * openConnection () * returns a URLConnection object, which indicates the connection to the remote object referenced by the URL. * URLConnection conn = url. openConnection (); * System. out. println (connection); * The underlying layer encapsulates the http resolution method for URLs. This object is a java built-in object that can parse specific protocols + * 1 in the socket * URLConnection class. string str = conn. getHeaderField ("Contend-Type"); * System. out. println (str); // responds to a message of the text type * then you can determine the type of the text based on the response result. Based on the specified parser, parse data * actually uses Socket and Protocol * 2. inputStream in = conn.getinputstream()); to get the response body of 1.html ** so the url. the principle of 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 the response body of 1.html. The url help is parsed into the html source code in. close ();}


Common Network Architecture
1. C/S: client/server (client, server)

Early stage:

Both VC ++ and VB are C/S.

Features:

The software of this structure, both the client and the server, must be written, and the development cost is high and the maintenance is troublesome.

Benefits:

The client can share part of the operation locally. 360 anti-virus: the 3D effects of the game are all calculated locally.

2. B/S: browsEr/server (browser, server)

Features:

The software of this structure is only developed on the server, and the client is directly replaced by a browser

Relatively low development costs and simpler Maintenance

Disadvantage: all operations are performed on the server.

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.