Use IDEA to create and run servlet objects

Use IDEA to create and run servlet objects

1. Create a JAVA project:
New->Project->JAVA Module...
insert image description here
2. Enter the project name (take ServletDemo as an example)
insert image description here
3. Configure Tomcat:
insert image description here
②Click "+"->Tomcat Servlet->Local
insert image description here

③Modify
the Name and view the URL address of Tomcat:
insert image description here
④Click "deploment"->"+"->"Artifact..."->"OK"
insert image description here
4. Click "File"->"Project Structure"->"Module"-> "Source" -> Click "Web" -> Right click "WEB-INF"
insert image description here
5. "New Folder" is as follows:
insert image description here
6. Click "Path" -> Select "Use Module Compile Output Path" -> Set the output path and test output All paths are selected to the place where the classes just created
insert image description here
7. Click "Dependency" -> click "+" -> select the first "JARs or directories..." -> find the location of the lib you just built -> "OK":
insert image description here
8 .Select "Jar Directory" -> "OK":
insert image description here
9. Click "OK":
insert image description here
10. Create a new Servlet:
create a package under src (eg com.servlet) -> right-click to create a new class (MyFirstServlet)
insert image description here

Sample code:

package com.servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
public class MyFirstServlet extends HttpServlet {
    
    
        private static final long serialVersionUID=1L;
        public void init(ServletConfig config)throws ServletException{
    
    
            super.init(config);
        }
        public void service(HttpServletRequest request, HttpServletResponse response)throws IOException{
    
    
            response.setContentType("text/html;charset=utf-8");
            PrintWriter out=response.getWriter();
            out.println("<html><body>");
            out.println("第一个Servlet类");
            out.println("</body></html>");
        }
    }

11. Configure web.xml under WEB-INT:
insert image description here
enter the code:

<?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_4_0.xsd"
         version="4.0">
    <servlet>
        <servlet-name>MyFirstServlet</servlet-name>
        <servlet-class>com.servlet.MyFirstServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>MyFirstServlet</servlet-name>
        <url-pattern>/MyFirstServlet</url-pattern>
    </servlet-mapping>
</web-app>

12. Start Tomcat, you can enter Tomcat's URL + "MyFirstServlet" in the browser to run the Servlet! ! !

insert image description here
(URL viewing method: click Tomcat)
insert image description here

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324228907&siteId=291194637