当前位置:网站首页>Servlet learning notes

Servlet learning notes

2022-04-23 20:13:00 Jiugui wine!!!

Catalog

One 、Servlet What is it? ?

Two 、 Development Servlet The of the program only needs two steps

( One ) To write servlet Class implementation Servlet Interface and its hierarchical relationship

( Two ) Write servlet Class deployment to web The server

3、 ... and 、Servlet Operating mechanism 、 working principle


One 、Servlet What is it? ?

Servlet It is a kind of operation in Web Server side Java Applications , Can produce dynamic web page , It's a kind of API Interface . meanwhile Servlet Belong to JSP The bottom of the ,Servlet It's a Java class , function JSP when , The bottom layer of the server will JSP It will compile into a Java class , This class is Servlet.


Two 、 Development Servlet The of the program only needs two steps

To achieve Servlet Interface Java The program is called Servlet

The user uses the browser to request Servlet Of url How to write ???

localhost:8080/ Project name /<url-pattern>

( One ) To write servlet Class implementation Servlet Interface and its hierarchical relationship

1、 self-written servlet Class to inherit HttpServlet class

2、 rewrite HttpServlet Of doGet() perhaps doPost() Method

( Two ) Write servlet Class deployment to web The server

1、 To configure web.xml file : First, register servlet、 The second is the setting servlet The request path for

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
         xmlns="http://java.sun.com/xml/ns/j2ee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
        http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<!--    register servlet    -->
  <servlet>
    <servlet-name>hello</servlet-name>
    <servlet-class>com.dai.servlet.HelloServlet</servlet-class>
  </servlet>
<!--    Set up servlet The request path for     -->
  <servlet-mapping>
    <servlet-name>hello</servlet-name>
    <url-pattern>/url</url-pattern>
  </servlet-mapping>
</web-app>
<!--    servlet working principle  ==>
url-pattern: It is equivalent to specifying a request path ,
 When the user requests to enter Tomcat Server's webapps The project name inside servlet1 after ( Here is http://localhost:8080/servlet1)

    Next, add... After the path /url(http://localhost:8080/servlet1/url), You can visit the servlet 了 ,
 Will be in web.xml Inside <servlet-mapping> seek  <url-pattern> yes /url Of 

    When you find it, you know what to do /url Of servlet The name is called hello,
 Then go to <servlet> Who does it match servlet Name is hello, After matching successfully ,
 It's time to do it servlet Inside <servlet-class> Class   Corresponding doGet  doPost  Method 
-->

<servlet></servlet> Used to declare a servlet The data of , There are mainly the following sub elements :

  • <servlet-name></servlet-name> Appoint servlet The name of
  • <servlet-class></servlet-class> Appoint servlet Class name of
  • <servlet-mapping></servlet-mapping> Used to define servlet The corresponding URL mapping , It contains two sub elements
  • <servlet-name></servlet-name> Appoint servlet The name of
  • <url-pattern></url-pattern> Appoint servlet The corresponding URL

3、 ... and 、Servlet Operating mechanism 、 working principle

Operating mechanism :

servlet In a life cycle , Only one... Will be instantiated servlet object , In the 1 Create... On access , The first 2 Only run... On this visit doGet and doPost Method .

servlet It adopts multithreading mechanism , Every request , The system will allocate a thread to run doGet function

working principle :

When Web After the server receives the request from the browser , Do the following work :

版权声明
本文为[Jiugui wine!!!]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204210556007147.html

随机推荐