3 Publish Web Project

2.3.1 Publish static resources

Introduction to

Static Resource Case - Deployment and Access of Portal-like Sites

Enter an address in the browser to access the static HTML page.

Add pages from HTML and CSS lessons to a Java Web project, deploy the project in Tomcat, start the Tomcat server, and use a browser to access them. Implementation Steps

Step 1: Create a project and choose the Tomcat version to use

Step 2: Copy the resource to the project's web directory
Paste our written web resources, html, css, pictures, etc. into the web directory

Step 3: Configure the default home page in web.xml
The default home page we just started with is index.jsp, but we don't use this one

This requires us to configure it ourselves. There is a web.xml file in the web directory that we open to configure.

You need to add a tag to this page and add the path of the home page to the tag. For example, if you want news.html to be your home page, you can modify the default home page.

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
<!--    Modify Default Home Page-->

Step 4: Deploy the project to the Tomcat server
Confirm if the project is published, click Run, edit configurations
First go to deployment, find the application context, and confirm this path. Usually, I have multiple modules, so I specify specific modules here. If there is a single project or only one module, he usually only has one ** "/"**, so don't move.

Then go to the server and change the browser you want to use. Note that the path behind the url column must be the same as the application context above. My port 80 here corresponds to the HTTP port below and can be modified by myself without displaying the address bar of 80. Then updateResource selection can automatically update resource s, this is recommended. Click ok.

Step 5: Test browser access
Once configured above, you can start the service, find the corresponding tomcat, and click Run.

Finally, 404 happens easily here. It can't find the location of resources, indicating that your path may have been written incorrectly. Check the correction.

2.3.2 Publishing dynamic resources

Introduction to Servlet

Before you can learn to publish dynamic resources, you need to understand the Servlet.
Servlet translation into Chinese is a service-side script, which is a set of specifications introduced by SUN company called Servlet Specification. Servlet Specification is part of JavaEE Specification. We can learn the basic concepts of Servlet by consulting the API of JavaEE Specification. You can see the introduction of Servlet by clicking on the official JavaEE8 document.
Java EE API Local chm File

Servlet s are Java server-side programs that receive and respond to requests from clients based on the HTTP protocol.
If you want to implement the functionality of a Servlet, you can either implement the javax.servlet.Servlet interface or inherit its implementation class.
Core method: service(), through which any client request passes. Case Introduction

Change the server of the JavaSE student management system to Tomcat.

Copy the HTML and styles and picture files involved in the Java Project Student Management System into the Java Web project, deploy the project in Tomcat, start the Tomcat server, and use a browser to access them. Implementation Steps

Step 1: Create a project
Step 2: Copy Resources
Step 3: Configure the default home page
Step 4: Deploy the project
Add your own project path to the resource access path here.

The steps are essentially the same as those for the static resources above, and will not be repeated here. Creating a dynamic resource in a case-Servlet

Write a Servlet step by step:
Step 1: Write a common class to implement the Servlet interface or inherit the GenericServlet class or inherit the HttpServlet
First create a package in the src directory, then create a class of MyServlet that implements the Servlet interface and all its methods.
Step 2: Rewrite the service method and output a sentence

package com.symc.servlet;

import javax.servlet.*;
import java.io.IOException;

 * @Author: Class 2019 Medical Information Engineering 0213, Fengwen Shenyang Medical College
 * @CreateTime: 2021/10/06 15:05
 * @Description: This is a program to get started with Servlet
public class MyServlet implements Servlet{
    public void init(ServletConfig servletConfig) throws ServletException {


    public ServletConfig getServletConfig() {
        return null;

     * All client requests pass through the service method
     * @param servletRequest
     * @param servletResponse
     * @throws ServletException
     * @throws IOException
    public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
        System.out.println("I was visited");

    public String getServletInfo() {
        return null;

    public void destroy() {


Step 3: Configure Servlet in web.xml
You need to find the web.xml file in the web, WEB-INF directory;
You need to configure the Servlet declaration here, and I'll name myServlet as you like, and the location of the class is the full package name.


You also need to configure the Servlet mapping, still putting the name in the declaration in the name here, followed by the url plus / myServlet, so that it first finds the name in the mapping, and then finds the location corresponding to the name in the declaration by that name is the package name above.

<!--    Servlet mapping-->

Step 4: Start the tomcat server test
Open the server and add a name to the browser address bar.

The browser page is blank and unchanged, but when you return to the console, you will see traces of access and the console will record every time the browser refreshes.

This means that as long as there are clients accessing the server, the service method will be executed. Execution Process

First the / myServlet we access from the address bar finds the content of the url-pattern tag in the mapping configuration, then finds the name in the name tag from this content, then finds the name in the name tag in the declaration configuration, then finds the implementation class in the declaration configuration by this name, and finally finds the class and it executes the service automaticallyMethod.

Tags: Web Development JavaEE Tomcat servlet

Posted on Wed, 06 Oct 2021 15:26:17 -0400 by moonshaden