Organize javaWeb study notes (on)

Software Architecture:

B/S architecture: browser/server architecture, the client uses the browser to access the server, the user does not need to install the client, only the computer has a browser to access,

C/S architecture: client/server architecture: the client uses the client program provided by the software provider to access, and the user needs to download the client program and install it

C/S

  • C/S structure is client/server (Client/Server), such as QQ;
  • It is necessary to write server-side programs and client-side programs, for example, what we installed is the QQ client-side program;
  • Disadvantages: When updating the software, both the client and the server need to be updated at the same time, which is troublesome;
  • Advantages: better security.

B/S

  • The B/S structure is the browser/server (Browser/Server);
  • Advantages: only need to write server-side programs;
  • Disadvantages: poor security.

Access web resources:

Access to dynamic resources: The server needs to run dynamic resources, and respond to the client with the results (static resources) after running

Accessed static resources: The server directly responds to the client with static resources

server

Server: Indicates a hardware server and also a software server

Hardware server: computer

Software Server: Software JavaWeb uses Software Server

  • Tomcat (Apache): Apache Foundation, a small and medium-sized JavaEE server, only supports a small number of JavaEE standard servlets/jsp. open source, free (for development)
  • JBoss (JBOSS): A large JavaEE server that supports all JavaEE specifications and is charged.
  • Weblogic (Oracle): A large JavaEE server that supports all JavaEE specifications and is charged.
  • Websphere (IBM): IBM Corporation, a large-scale JavaEE server, supports all JavaEE specifications, and charges.

Structure of the web project

  1. Static web project structure understanding

    Create a directory under which to store static resources

    • bin: This directory stores binary executable files. If it is the installation version, there will be two exe files in this directory: tomcat6.exe and tomcat6w.exe. The former is to start Tomcat under the console, and the latter is to start Tomcat by popping up a UGI window; if it is the decompressed version, there will be startup.bat and shutdown.bat files.
    • conf: This is a very, very important directory. There are four most important files in this directory:
    • server.xml: Configure the entire server information. For example, modify the port number, add a virtual host, etc.; tomcat-users.xml: store the file of tomcat users, where the user name and password of tomcat and the role information of the user are saved. You can add tomcat users according to the comment information in the file, and then you can enter the Tomcat Manager page on the Tomcat home page;
    • web.xml: Deployment descriptor file, many MIME types are registered in this file, that is, document types. These MIME types are used to describe the document type between the client and the server. If the user requests an html web page, the server will also tell the client browser that the document responded by the browser is of text/html type, which is a MIME type. The client browser knows how to handle it through this MIME type. Of course, this html file is displayed in the browser. But if the server responds with an exe file, it is impossible for the browser to display it, but a download window should pop up instead. MIME is used to describe what type of content the document is!
    • context.xml: Unified configuration for all applications, usually we don't configure it.
    • lib: Tomcat's class library, which contains a lot of jar files. If you need to add a jar file that Tomcat depends on, you can put it in this directory. Of course, you can also put the jar file that the application depends on in this directory. All jar projects in this directory can be shared, but when your application is placed under other Tomcats, you can no longer share the Jar package in this directory. Therefore, it is recommended to only put the Jar package required by Tomcat in this directory;
    • logs: This directory is full of log files, which record information about Tomcat startup and shutdown. If there is an error when starting Tomcat, the exception will also be recorded in the log file.
    • temp: store the temporary files of Tomcat, the things in this directory can be deleted after stopping Tomcat!
    • webapps: directory for storing web projects, each of which is a project; if there are already directories in this directory, they are all brought by tomcat. project. Among them, ROOT is a special project. When the project directory is not given in the address bar, it corresponds to the ROOT project. http://localhost:8080/examples, enter the example project. Among them, examples is the project name, that is, the name of the folder.
    • work: The files generated at runtime, and the files that are finally run are all here. Generated by projects in webapps! You can delete the contents of this directory, and the work directory will be generated again when you run it again. When a client user accesses a JSP file, Tomcat will generate a Java file through JSP, and then compile the Java file to generate a class file, and the generated java and class files will be stored in this directory.
    • LICENSE: License.
    • NOTICE: Documentation.

    Project deployment: operation and maintenance

  2. Copy the project directory (into a war package) to the tomcat installation directory/webapps directory

jar: java project into jar

war: The web project is marked as war and copied to the webapps directory of tomcat. When tomcat starts, the war will be automatically decompressed

  1. In the tomcat installation directory/conf/server.xml file, <Host>add a label <context>to publish the project
<Context docBase="项目路径" path="/访问项目名" />
  1. Create an xml file with any name in conf/Catalina/localhost. write in the file

Accessed project name: xml file name

Unpublish: Add .backup to the xml file name

This method: idea release project method

javaWeb dynamic project structure

  1. project root directory

    ​ |–WEB-INF

    ​ |–lib third-party jar

    ​ |--classes class path src directory, storage path of bytecode files after class compilation, configuration files

    ​ |–web.xml file configuration file of the current web project

    |– Static resources

The resources under WEB-INF cannot be directly accessed by the browser

Servlet

Three components of javaWeb:

Servlet: Receive user requests, process requests, and respond to results

Filter: filter, used to intercept requests

Listener: Listener, monitor a certain behavior, as long as the user triggers this behavior, execute the code

ask:

Contains request header and request body (get has no request body)

response:

Contains response headers, response body (also known as response body)

There are three ways to implement Servlet:

  • Implement the javax.servlet.Servlet interface;

  • Inherit the javax.servlet.GenericServlet class;

  • Inherit the javax.servlet.http.HttpServlet class; commonly used for development

HttpServlet is an abstract class, a subclass of the GenericServlet class, used to handle HTTP requests

Most of the methods in the three major components in javaweb do not need to be called by us, and are generally called by the server. And the Servlet object is not created by us, but by the server!

Servlet life cycle (common in interviews)

Born : By default, the Servlet is created by the server and created only once when it is accessed for the first time, and the init() method is called once after the Servlet is created

Service : The service() method is called every time the user requests

Death : before the server shuts down, call the destroy() method to destroy all created objects

Other Objects in Servlets

  1. ServletConfig object: corresponding to the configuration of Servlet, created by Tomcat,

    1. Get the initialization parameter parameter name of the Servlet, and the parameter value is String type

    String getInitParameter(String name) Get the initialization parameter value according to the initialization parameter name

    Enumeration getInitParameterNames() Get all initialization parameter names

    Early collection of jdk: Enumeration

    [External link picture transfer failed, the source site may have an anti-theft link mechanism, it is recommended to save the picture and upload it directly (img-DoNZxysk-1667533413184) (D:/%E9%A3%9E%E6%80%9D%E5%AE%9E%E8%AE%AD%E4%B8%89/6.JavaWeb/%E8%AF%BE%E4%BB%B6/%E8% 80%81%E5%B8%88%E7%AC%94%E8%AE%B0/10-17%E7%AC%94%E8%AE%B0/assets/image-20221017112339511.png)]

    In the configuration of the Servlet,

    <servlet>
       <!-- 名称 随意取, 一般取名类名-->
       <servlet-name>AServlet</servlet-name>
       <!--对应的Servlet类, 全限定名(包.类名) -->
       <servlet-class>com.fs.web.AServlet</servlet-class>
    
       <!--配置初始化参数-->
       <init-param>
           <param-name>name</param-name>
           <param-value>张三</param-value>
       </init-param>
       <init-param>
           <param-name>age</param-name>
           <param-value>21</param-value>
       </init-param>
    </servlet>
    
    1. ServletContext getServletContext() Get ServletContext, the largest domain object (Application domain)
  2. ServletRequest object: request object, the core Tomcat creation will be described in detail later

​ Core method: Get request parameters

​ String getParameter(String name) Get the request parameter value according to the request parameter name

3. ServletResponse object: Response object, the core Tomcat creation will be described in detail later

getWriter() gets the character response output stream, and the server writes the data to the client's browser page by changing the output stream

System.out.println(): Print data to the server console

What are the ways for the front end to send requests:

  1. The action of the form can be get, post method: default value get, set post, send post request
  2. hyperlink <a href="">can only get
  3. Enter the url directly in the browser address bar, only get
  4. js sends ajax request can be get, post

Request method: GET request/POST request

get request: request parameters are passed through url, request parameter url: url?k1=v1&k2=v2&…

​ Unsafe, pass string, cannot pass file data

post request: request parameters are passed through the request body,

​ High security, pass string, pass file data

Servlet details

Annotate servlets

Annotation: special interface, use @ annotation name on method, class, property... (create object of annotation class)

Class type: class interface enum @interface

Annotation function: replace the configuration file

The @WebServlet annotation replaces the servlet configuration file

// name 忽略:
//@WebServlet(name="DServlet",urlPatterns="/DServlet")
//@WebServlet(urlPatterns="/DServlet")
//urlPatterns 与value 等同
//@WebServlet(value="/DServlet")
// 如果只给value赋值,  省略 value=
@WebServlet("/DServlet")

Are Servlets thread-safe?

Judgment basis: Whether multiple threads share a unified resource, not sharing: safe; sharing: unsafe

The Servlet class has only one object in Tomcat. When there are multiple requests, the same Servlet object is requested, and the same object is used by multiple threads. The Servlet object is shared, and the thread is not safe.

Because Servlet is thread-unsafe, do not provide attributes for storing data in Servlet

Servlet-url-pattern

url-pattern function: bind this servlet to access the url of this servlet

A Servlet can only bind one url?

Multiple urls can be bound, but in actual development generally only one is bound

How to write url-pattern

/string binds the determined url

Combine * (wildcard, any)

  • /user/* prefix matches, as long as it starts with /user, and anything behind it, such as /user/a /user/b
  • *.do suffix match, as long as the url ends with the suffix name, it matches, for example, a.do c.do

Note: /*. The suffix name is a typo

  • /* match all

The more precise the url match, the higher the priority, the url without asterisks has higher priority than those with asterisks

Modify the creation timing of Servlet

The default creation time of the Servlet is the first time the Servlet is requested to be created, which is also called the first penalty, and the first request takes a little more time

Modify the timing of Servlet creation: When the web server starts, create a Servlet

<servlet>
      <!--设置tomcat启动的时候,创建-->
      <load-on-startup>非负整数</load-on-startup>
  </servlet>
The smaller the number in load-on-startup`, the earlier it is created

ServletContext (context)

The ServletContext object has the same life as the sky (tomcat)

ServletContext is created when Tomcat starts and destroyed when Tomcat is closed. There is only one in the entire web project

Any user anywhere in the web project can access

Role: As a global shared data container, also known as the domain object ServletContext is called the application domain

Domain object function: access data, data is key/value structure key: String type value: Object type

There are four domains in javaWeb:

1. The application domain is valid for the entire project

  1. The session domain is usually valid within the same session
  2. The request field is usually valid within the same request
  3. The page field is valid on the same page/Servlet: equivalent to this
Domain objects have the following methods

void setAttribute(String name, Object object) Store data in the domain, if name does not exist, add it, if name exists, overwrite object

Object getAttribute(String name) Get data from domain, if name does not exist, return null

removeAttribute(String name) removes data from the field

Enumeration getAttributeNames() Get all the names in the domain

The page field is valid on the same page/Servlet: equivalent to this

Domain objects have the following methods

void setAttribute(String name, Object object) Store data in the domain, if name does not exist, add it, if name exists, overwrite object

Object getAttribute(String name) Get data from domain, if name does not exist, return null

removeAttribute(String name) removes data from the field

Enumeration getAttributeNames() Get all the names in the domain

Guess you like

Origin blog.csdn.net/m0_48895748/article/details/127686022