Servlet notes (continuous update)

Tomcat only implements Servlet/JSP, which is called a lightweight container, JBoss, WebLogic, etc. implement all 13 specifications of JavaEE, which is called a heavyweight container.

You can set the Tomcat port to 80 in server.xml, which is the default port of the Http protocol. As in the past, you can access the web service in Tomcat without entering the port number.

Several ways to deploy projects in Tomcat

  1. Put the project folder directly under the webapps directory, the access path of the project folder is the virtual directory

    => Simplified deployment: You can compress the project folder into war, put the war under webapps, the war package will be automatically decompressed when tomcat starts, and when tomcat is running, delete the war package, and tomcat will automatically delete the decompressed files Clip (hot deployment)

  2. Configurationconf/server.xml

    Configure in the Host tab <Context docBase="D:\Hello" path="/hehe" />

    • docBase: project storage path
    • path: project virtual directory (you need to add the virtual directory prefix when entering the url in the browser)
  3. In the conf/Catalina/localhostCreate a xml file directory, write in a file

    <Context docBase="D:\Hello" />

    The virtual directory of the project is the name of the xml file (when tomcat is running in IDEA, the project is deployed in this way)

Tomcat comes with two Servlets:

  • DefaultServlet -> Processing static resources (such as directly accessing an html file or an img picture in the browser address bar, the corresponding file on the server will be read through the DefaultServlet (IO operation), and then the content of the file will be streamed Write the form in the Response, Tomcat then extracts the data from the Response and returns it to the browser)
  • JspServlet -> Process the access to the jsp file (the Servlet will convert the jsp file into a Servlet, then call the Servlet to process the data, and assemble and splice the data with static HTML statements, and finally write the static HTML after splicing In Response, Tomcat extracts it from Response and returns it to the browser)

The two servlets that come with Tomcat, their url-pattern are respectively

  • DefaultServlet -> /
  • JspServlet -> *.jsp *.jspx

So when we develop our own Servlet,

The configured url-pattern cannot be written/* , which will short-circuit the two servlets that come with Tomcat and cannot handle static resources and JSP pages.

The configured url-pattern cannot be written either/ , which will short-circuit the DefaultServlet and cause the inability to process static resources

Do not place static resources (css/js/html, etc.) in the WEB-INF directory. The files in the WEB-INF directory cannot be accessed from outside ( /WEB-INF/index.htmlindex.html cannot be accessed by typing directly in the browser )

<!-- web.xml 中对servlet的配置-->
<!-- load-on-startup 配置为正数,则在tomcat启动时就加载Servlet,配置为负数,则在第一次访问Servlet时进行加载,数字为正数时,表示tomcat启动时加载servlet的顺序,数字越小越先加载 -->
<servlet>
        <servlet-name>yogurt</servlet-name>
        <servlet-class>com.yogurt.servlet.YogurtServlet</servlet-class>
        <load-on-startup>-1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>yogurt</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

Three elements of computer communication:

  1. IP
  2. Port
  3. Protocol

Guess you like

Origin blog.csdn.net/vcj1009784814/article/details/106016764