2.3.1 Publish static resources
Introduction to 2.3.1.1
Static Resource Case - Deployment and Access of Portal-like Sites
Requirements:
Enter an address in the browser to access the static HTML page.
Details:
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.
2.3.1.2 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" 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_3_1.xsd" version="3.1"> <!-- Modify Default Home Page--> <welcome-file-list> <welcome-file>/new/news.html</welcome-file> </welcome-file-list> </web-app>
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 2.3.2.1 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.
2.3.2.2 Case Introduction
Requirements:
Change the server of the JavaSE student management system to Tomcat.
Details:
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.
2.3.2.3 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.
2.3.2.4 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{ @Override public void init(ServletConfig servletConfig) throws ServletException { } @Override public ServletConfig getServletConfig() { return null; } /** * All client requests pass through the service method * @param servletRequest * @param servletResponse * @throws ServletException * @throws IOException */ @Override public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException { System.out.println("I was visited"); } @Override public String getServletInfo() { return null; } @Override 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.
<servlet> <servlet-name>myServlet</servlet-name> <servlet-class>com.symc.servlet.MyServlet</servlet-class> </servlet>
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--> <servlet-mapping> <servlet-name>myServlet</servlet-name> <url-pattern>/myServlet</url-pattern> </servlet-mapping> </web-app>
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.
2.3.2.5 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.