编程知识 cdmana.com

[Java backend] Java backend development from scratch (3)

编写基础的Servlet应用程序

1 Servlet

使用Servlet需要在pom.xml中引入以下依赖:

    <!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
      <version>4.0.1</version>
      <scope>provided</scope>
    </dependency>

ServletThe interface defines the following5个方法

public void init(ServletConfig config) throws ServletException;

public ServletConfig getServletConfig();

public void service(ServletRequest req, ServletResponse res)
	throws ServletException, IOException;
	
public String getServletInfo();

public void destroy();

init、service和destroy方法属于servlet生命周期方法.servletThese three methods are called according to the following principles:

  • init:第一次请求Servlet时,Servlet容器就会调用这个方法.在后续的请求中,This method will no longer be called.You can use this method to write some application initialization related code.在调用这个方法时,Servlet容器会传递一个ServletConfig.
  • service:每次请求Servlet时,Servlet容器都会调用这个方法.Must be written hereServletCompleted corresponding code.第一次请求Servlet时,Servlet容器会调用init和service,之后每次请求,不会再调用init,只会调用service方法.
  • destroy:要销毁Servlet时,Servlet容器就会调用这个方法.It usually happens when an application is uninstalled,或者关闭Servlet容器的时候,一般来说,You can write some resource cleanup related code in this method.

ServletThe other two methods in are non-lifecycle methods:getServletInfo和getServletConfig.One thing that must be noted is thread safety.All users in an application will share oneServlet实例.

2 编写Servlet示例

确保在pom.xml文件中已经引入servlet-api依赖包.项目目录结构如下:
在这里插入图片描述

package com.servlet.demo;

import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(name = "MyServlet", urlPatterns = {
    "/my"})
public class MyServlet implements Servlet {
    

    private transient ServletConfig servletConfig;

    @Override
    public void init(ServletConfig config) throws ServletException {
    
        this.servletConfig = config;
    }

    @Override
    public ServletConfig getServletConfig() {
    
        return servletConfig;
    }

    @Override
    public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
    
        String servletName = servletConfig.getServletName();
        res.setContentType("text/html");
        PrintWriter writer = res.getWriter();
        writer.print("<html><head></head><body>Hello from " + servletName + "</body></html>");
    }

    @Override
    public String getServletInfo() {
    
        return "My Servlet";
    }

    @Override
    public void destroy() {
    

    }
}

上述使用了@WebServlet注解,所以需要在web.xmlThe configuration allows the use of annotations,web.xml 的顶层标签 中有一个属性:metadata-complete,该属性用于指定当前 web.xml 是否是完全的.若该属性设置为 true,则容器在部署时将只依赖 web.xml,忽略所有的注解.若不配置该属性,或者将其设置为 false,则Indicates that annotation support is enabled.配置如下所示:

<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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_4_0.xsd" version="4.0" metadata-complete="false">
    <!-- metadata-complete取值为true,Indicates that annotation support is disabled -->
    <!-- metadata-complete取值为false,Indicates that annotation support is enabled -->
    <display-name>Archetype Created Web Application</display-name>
</web-app>

现在,就可以运行Servlet项目了.

3 配置Servlet容器

  • 点击Add Configuration
    在这里插入图片描述
  • 点击 add new
    在这里插入图片描述
  • tomcat选择local
    在这里插入图片描述
  • 选择Application Server
    在这里插入图片描述
  • 选择本地的tomcat目录
    在这里插入图片描述
  • Change the following two port numbers(If it is not occupied, no need to change it)
    在这里插入图片描述
  • 配置Deployment
    在这里插入图片描述
    在这里插入图片描述
  • 更改一下application context
    在这里插入图片描述
    然后点击ok.

运行项目

Click Run in the order shown below:
在这里插入图片描述
运行成功后如下图所示,has a hook
在这里插入图片描述
Then enter the following in the browser:
在这里插入图片描述
【Java后台】从零开始的Java后台开发(二)
【Java后台】从零开始的Java后台开发(一)

版权声明
本文为[It-zhai male]所创,转载请带上原文链接,感谢
https://cdmana.com/2022/218/202208060725399753.html

Scroll to Top