Java web learning notes

Javaweb

concept

Technology for dynamic web resource development is collectively referred to as Java Web

web application

web applications: programs that provide browser access

For web applications to provide external access: a server is required to unify Tomcat management;

web application composition:

  • html,CSS,js
  • jsp/servlet
  • Java Programs
  • jar package
  • Properties

Dynamic web

[External chain picture transfer failed, source station may have anti-theft chain mechanism, it is recommended to save the picture and upload it directly (img-VNBQq8kY-1638718858450)(JavaWeb concept.assets/image-20211203235107788.png)]

Development Technology

ASP: Microsoft, the earliest popular in China;
php:

  • PHP is fast, powerful, cross-platform, and code is simple (70%, WP)
  • Unable to host large traffic (limitations)

JSP/Servlet:

  • B/S architecture maintained by sun Company;
  • Java-based (all large companies, or some open source components, are written in Java);
  • It can carry the impact of the three high problems; (high concurrency, high availability, high parallelism)
  • Grammar like ASP;

web server

Server is a passive operation that handles some requests and gives some response information to users.

IIS: Microsoft, running ASP, Windows comes with it;
tomcat: Apache Software Foundation, written by java, lightweight, best for beginners on the web. Run the JSP page and Servlet.

Tomcat

  1. install
  2. Understanding configuration files and directory structure
  3. The function of this thing

1. Directory structure

[External chain picture transfer failed, source station may have anti-theft chain mechanism, it is recommended to save the picture and upload it directly (img-QBZovCh2-1638718858451)(JavaWeb concept.assets/image-20211204000343316.png)]

2. Startup and Configuration

Start access: http://localhost:8080/

Configuration: server.xml file

Configure port number

  • tomcat default port: 8080
  • mysql: 3306
  • http: 80
  • https: 443
    <Connector port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />

Host Name

  • Default host name: localhost->127.0.0.1
  • Default site application location: webapps
      <Host name="localhost"  appBase="webapps"
            unpackWARs="true" autoDeploy="true">

3. Publish a web site

  • Put your own website under the web application folder (webapps) specified in the Tomcat server and you can access it.

How the website is accessed

  1. Enter a domain name; Enter

  2. Check for this domain name mapping under the local C:\Windows\System32\driversetc\hosts configuration file;

    1. Yes: Return directly to the corresponding ip address, where there are web programs we need to access that can be accessed directly

      127.0.0.1	localhost
      
    2. No: go to DNS server to find, find the chemical will return, find it cannot find it

The difference between localhost and 127.0.0.1

localhot (local) is transmitted without a network card! This is important because it is not restricted by network firewalls and network card-related restrictions.

127.0.0.1 is transmitted through a network card, relies on the network card, and is restricted by network firewall and network card-related restrictions.

HTTP

1. What is Http

HTTP (Hypertext Transfer Protocol) is a simple request-response protocol that typically runs over TCP

  • Text: html, string...
  • Hypertext: Pictures, Music, Videos, Location, Maps...
  • 80

Https: Secure

  • 443

2. Two Times of http

http1.0: When HTTP/1.0 clients can connect to the web server, they can only get one web resource and disconnect.

http2.0: After an HTTP/1.1 client can connect to a web server, it can acquire multiple web resources;

3. Http Request

1. Request line

  • Request method: Get, Post, HEAD, DELETE, PUT, TRCT...

2. Message Header

Baidu example:

General Overview

Request URL: https://www.baidu.com/ 		 Request Address
Request Method: GET						Request Method get/post
Status Code: 200 OK						Status Code
Remote Address: 14.215.177.39:443		Remote Address
Referrer Policy: origin-when-cross-origin

Request Headers Request Header

Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9			Tell the browser what data types it supports
Accept-Encoding: gzip, deflate, br			Tell the browser what encoding formats it supports: GBK utf-8 GB2312 io Is 859-1
Accept-Language: zh-CN,zh;q=0.9				Tell the browser its locale
Cache-Control: max-age=0					Cache Control
Connection: keep-alive						Tell the browser whether to disconnect or stay connected when the request is complete
Cookie: BAIDUID=50A354D020AE4E363F6B193397CE5D10:FG=1; __yjs_duid=1_8deec826354462b4964e8656959b22981636727703661; BIDUPSID=50A354D020AE4E363F6B193397CE5D10; PSTM=1636728447; channel=baidusearch; baikeVisitId=d7ef514f-7af5-4de7-9903-0d62a70de219; BAIDUID_BFESS=7D3EAC75153CE930B7A71A0EA68D5FB7:FG=1; BDUSS=XlzOHVsSWtiSFBvODg4RzlSRGpKSHlSRC1VZnRLYXZVQ2lKdEtndjZCUENYc2hoRVFBQUFBJCQAAAAAAAAAAAEAAABvpNwxcWNoNzM2AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAMLRoGHC0aBhY; BDUSS_BFESS=XlzOHVsSWtiSFBvODg4RzlSRGpKSHlSRC1VZnRLYXZVQ2lKdEtndjZCUENYc2hoRVFBQUFBJCQAAAAAAAAAAAEAAABvpNwxcWNoNzM2AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAMLRoGHC0aBhY; BD_UPN=12314753; ZD_ENTRY=bing; ab_sr=1.0.1_NjZmNjUxZjUyYjY0NjUzODU0NDhjM2I2ZTczMjNjODAxZWZmZjJkZGQ5ODM2NWIwZWZhMmUyYTA3OGQ4ODAzODVhYmRmYjkyMTFiY2Q1MjY0MDYzYjYyNDllMTcyNWY5YmJhMmE2MDhiNWVlNTk5NTYzNzNhNjc1M2ZhZmM2MmYyMzk5MzhmYTY3NDkwNmJlZGQwY2I1NDYwZGIyOTc2MTE2YjZkMzlkN2I3MjVhZGExMWY2MjNiZGFjNjY0YTZm; BD_HOME=1; H_PS_PSSID=35359_35105_31253_34584_34504_35245_34578_34606_35325_26350_35301; BA_HECTOR=8l258h2g85ak0501hs1gqkinp0q
Host: www.baidu.com
Referer: https://cn.bing.com/
sec-ch-ua: " Not A;Brand";v="99", "Chromium";v="96", "Google Chrome";v="96"
sec-ch-ua-mobile: ?0
sec-ch-ua-platform: "Windows"
Sec-Fetch-Dest: document
Sec-Fetch-Mode: navigate
Sec-Fetch-Site: cross-site
Sec-Fetch-User: ?1
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.45 Safari/537.36

4. http response

ResponseHeaders Response Headers

Cache-Control: private					Cache Control
Connection: keep-alive					Connect
Content-Encoding: gzip					Code
Content-Type: text/html;charset=utf-8	type
Date: Fri, 03 Dec 2021 16:51:09 GMT
Expires: Fri, 03 Dec 2021 16:51:09 GMT
Server: BWS/1.1
Set-Cookie: BDSVRTM=303; path=/
Set-Cookie: BD_HOME=1; path=/
Set-Cookie: H_PS_PSSID=35359_35105_31253_34584_34504_35245_34578_34606_35325_26350_35301; path=/; domain=.baidu.com
Strict-Transport-Security: max-age=172800
Traceid: 1638550269025443994618078961669056469914
Transfer-Encoding: chunked
X-Frame-Options: sameorigin
X-Ua-Compatible: IE=Edge,chrome=1

Response Body

Accept:text/html		Tell the browser what data types it supports
Accept-Encoding: 		Tell the browser what encoding formats it supports: GBK utf-8 GB2312 io Is 859-1
Accept-Language:		Tell the browser its locale
Cache-Control: 			Cache Control
Connection: 			Tell the browser whether to disconnect or stay connected when the request is complete
Host:					Host
Refresh:				Tell clients how often to refresh
Location:				Relocate Web Pages

5. Response Status Code

Status CodeMeaning
200Request Response Successful
3xxrequest redirections
4xxResource not found
5xxServer Code Error

Servlet

brief introduction

  • Servlets are a technology sun Corporation uses to develop dynamic web.

  • sun provides a Servlet interface in these API s, which only takes two steps to develop a Servlet program:

    • Write a class to implement the Servlet interface;
    • Deploy the developed Java classes to the web server;

Implement Servlet

1. Environment

  1. Create a new maven-web project
  2. Add servlet, jsp dependencies
  3. web.xml is up to date
  4. Directory Setup

2. Write a Servlet program

  1. Write a generic class

  2. To implement the Servlet interface, we inherit the HttpServlet and override the get and post methods

    package com.zui.servlet;
    
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.io.IOException;
    import java.io.PrintWriter;
    
    public class HelloServlet extends HttpServlet {
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            PrintWriter writer = resp.getWriter();// Response Flow
            writer.print("holle servlet");
        }
    
        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            doGet(req,resp); // Similar to business logic, requests are invoked directly in different ways;
        }
    }
    
  3. Write Servlet Mapping

    Why mapping is required: We write Java programs, but to access them through a browser, which needs to connect to a web server, so we need to register our Servlet s on the web server, and we also need to give a path that the browser can access;

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
                          http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
             version="4.0"
             metadata-complete="true">
        <!--register servlet
            name:Service name
            class: Registered Classes
        -->
        <servlet>
            <servlet-name>hello</servlet-name>
            <servlet-class>com.zui.servlet.HelloServlet</servlet-class>
        </servlet>
    
        <!--Servlet Request path for
            name:Name of the requested service
            pattern: Request Path
        -->
        <servlet-mapping>
            <servlet-name>hello</servlet-name>
            <url-pattern>/hello</url-pattern>
        </servlet-mapping>
    
    </web-app>
    
  4. Configure Tomcat

    [External chain picture transfer failed, source station may have anti-theft chain mechanism, it is recommended to save the picture and upload it directly (img-BOgjVxPD-1638718858452)(JavaWeb concept.assets/image-20211205182311067.png)]

    [External chain picture transfer failed, source station may have anti-theft chain mechanism, it is recommended to save the picture and upload it directly (img-xJz3qwBP-1638718858452)(JavaWeb concept.assets/image-20211205201601654.png)]

  5. start-up

Servlet Principle

Servlet invoked by the web server

[External chain picture transfer failed, source station may have anti-theft chain mechanism, it is recommended to save the picture and upload it directly (img-Fk9mVo6M-1638718858453)(JavaWeb concept.assets/image-20211205221509188.png)]

mapping

  1. A Servlet can correspond to a mapping path
  2. A Servlet can correspond to multiple mapping paths

Priority: Inherent path has the highest priority if no default request is found

	<!--Default Request Path-->
	<servlet-mapping>
        <servlet-name>hello</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>
	<!--Specify some prefixes and suffixes-->
	<servlet-mapping>
        <servlet-name>hello</servlet-name>
        <url-pattern>*.abc</url-pattern>
    </servlet-mapping>

Keywords: Apache Tomcat server

Added by Nexus10 on Mon, 06 Dec 2021 04:45:42 +0200