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:


  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


  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


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
		//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];
		//Message Header
		Map<String, String> hearder= new HashMap<String,String>();
		String str=null;
		while(!(str=br.readLine()).equals("")) {
			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];
			//Read all the contents of the body into the body;;
			String values=new String(body);
			System.out.println("Request body content"+ new String(body));
			String[] value =values.split("&");
			for(String v:value ) {
		//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("<html><head><title>first html</title></head>");
		bwriter.write("<body><p>hellow Word</p><a href=''>Tencent</a></body>";

html code

<!DOCTYPE html>
		<meta charset="utf-8">
		<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"/>

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!");
PrintWriter out = response.getWriter();
out.print("This is my first web page!!");

Start the server and run: http://localhost:8080/javaweb630/

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