Basic JavaWeb Theory

Keywords: Java html5 html http

Basic Theory

Software Framework Classification

1.B/S: browser/server, thin client

2.C/S: client/server, fat client

Http Protocol (Hypertext Transfer Protocol)

Agreements: Rules to be complied with by both parties

Hypertext transfer protocol: Hypertext transfer protocol

Why is it important: The protocol used by the B/S architecture:

Characteristic:

  1. Subprotocol based on TCP/IP protocol is the application layer transport protocol; mainly used for communication between browser and client; ServerSocket, Socket

  2. Stateless, Connectionless Protocol

  3. HTTP is based on request-"response mode for communication;

Components of Request

Three parts:

  1. Request line (first line)

  2. Several headers (multiple lines, from second line to blank line)

  3. Requestor (everything that follows a blank line, or may not have one)

The request consists of three parts, separated by spaces
Get /seedproject/first.html Http/1.1
Get: Request Method
/seedproject/first.html:url Uniform Resource Descriptor
Http/1.1: Protocol Version Number

Components of Respone

Three parts:

  1. Status line

  2. Message Header

  3. Entity Content

The status line is divided into three parts, separated by spaces
HTTP/1.1: Protocol Version
200:Status Code
ok:A brief description of the status

GET and POST

  1. Get requests use request rows to transfer data, POST requests use request bodies to transfer data;

  2. When Get passes data, the length of the data is limited and the clear text is passed (unsafe)

  3. When Post delivers data. Data is unlimited in length, secure, and capable of delivering binary information

Programming to simulate requests for the Http protocol

Mycat

Ideas for developing mycat;
1. Create a Serversocket with port number 8080, accept;
2. Get the Socket and call Scoket

public class MyCat {
	public static void main(String[] args) throws IOException{
		Map<String, String> params=new HashMap<>();
		
		//1.create sever Socket listens on port http 8080
		ServerSocket server=new ServerSocket(8080);
		System.out.println("Start listening");
		//2.get socket ; blocking method
		Socket socket=server.accept();
		//3.get InputStream and wrap
		//Byte Stream to Character Stream to Buffer Stream
		BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
		//Save the array readLine method to read one line at a time
		String requestLine = br.readLine();
		//Output Array
		System.out.println(requestLine);
		//Value GET request line before space in output array
		//The split method splits a string into substrings and returns the result as an array of strings
		String method=requestLine.split(" ")[0];
		System.out.println(method);
		
		
		//Message Header
		Map<String, String> hearder= new HashMap<String,String>();
		String str=null;
		while(!(str=br.readLine()).equals("")) {
			System.out.println(str);
			hearder.put(str.split(":")[0], str.split(":")[1]);	
		}
		//Get the request body equals IgnoreCase regardless of case
		if("post".equalsIgnoreCase(method)) {
			String sizeString=hearder.get("Content-Length");
			Integer length=Integer.valueOf(sizeString.trim());
			
			char[] body=new char[length];
			System.out.println(method);
			//Read all the contents of the body into the body;
			br.read(body);
			String values=new String(body);
			System.out.println("Request body content"+ new String(body));
			String[] value =values.split("&");
			for(String v:value ) {
				params.put(v.split("=")[0],v.split("=")[1]);
			}
		}
		
		//Response Bytes to Character Stream
		BufferedWriter bwriter=new BufferedWriter (new OutputStreamWriter(socket.getOutputStream()));
		//Write Status
		bwriter.write("HTTP/1.1 200 OK\r\n");
		//Write header
		bwriter.write("Content-Type: text/html\r\n");
		//Blank Line
		bwriter.write("\r\n");
		bwriter.write("<html><head><title>first html</title></head>");
		
		bwriter.write("<body><p>hellow Word</p><a href='http://Www.qq.com'>Tencent</a></body>";
		bwriter.write("</html>");
		
		bwriter.flush();
		bwriter.close();
		br.close();
		server.close();
	}
}

html code

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
	</head>
	<body>
		<form action="http://localhost:8080/first.html" method="post">
		id<input type="text" name="name" id="name"/>
		Password<input type="text" name="password" id="password"/>
		<input type="submit" value="Add to"/>
		</form>
	</body>
</html>

Java Web Environment Setup

  1. web: container tomcat

  2. Development environment: Eclipse

Introduction to Tomcat Containers

  1. Free open source web container

  2. Tomcat is a green software hosted by the Apache organization and can be decompressed directly

  3. The directory structure of Tomcat

    • Various commands of bin:tomcat;

    • Configuration file for cofn:tomcat

    • Dependent jar package for lib:tomcat

    • webapps: Store javaweb projects deployed by users

    • Work directory for works:jsp

Configure Eclipse

window->preferences->server->runtime environments->add->tomcat

Create a servlet

 

protected void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
System.out.println("This is my first web page!");
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
out.print("This is my first web page!!");
}·

Start the server and run: http://localhost:8080/javaweb630/First.do

Posted by nrg_alpha on Sat, 09 Oct 2021 11:53:45 -0700