Dynamic web technology: Servlet in detail

1 Introduction to Servlet

  • Servlet is a technology developed by sun company to develop dynamic web
  • Sun provides an interface in these APIs called: Servlet.
  • To develop a Servlet program, there are only two steps: write a class, implement the Servlet interface, and deploy the developed java class to the web server.

The java program that implements the Servlet interface is called Servlet.

2 HelloServlet

There are two default classes that implement the Servlet interface: HttpServlet and GenericServlet.

Generally, directly inherit HttpServlet and override the doGet() and doPost() methods.

3 steps

1. Maven dependency

There are mainly two packages:

  • servlet-api
  • jsp-api
        <!-- 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>
        <!-- https://mvnrepository.com/artifact/javax.servlet.jsp/jsp-api -->
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.2</version>
            <scope>provided</scope>
        </dependency>

2. Write a class that inherits HttpServlet

  • Override the doGet() and doPost() methods.
  • Because get or post is only different in the way of request, the business logic is the same, and they can call each other.
public class MyServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取前端传送参数
        String method =req.getParameter("method");
        if("add".equals(method)){
            req.getSession().setAttribute("msg", "执行一个add方法~");
        }
        if("delete".equals(method)){
            req.getSession().setAttribute("msg", "执行一个delete方法~~");
        }
        //进行业务处理
        //转发或重定向到视图,此处为转发到指定页面
        req.getRequestDispatcher("/WEB-INF/jsp/home.jsp").forward(req, resp);
    }
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}

3. Write the page home.jsp to be forwarded

  • After the servlet is processed, it is forwarded to the /WEB-INF/jsp/home.jsp page.
  • The body of the page displays ${msg}, which represents the content of the msg field returned by the servlet.
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
 <head>
    <title>zuka</title>
 </head>
 <body>
  ${msg}
  </body>
</html>

4. Write Servlet mapping

Why do I need to map:

  • The Servlet implementation class is a JAVA program, which is accessed through a browser, and the browser needs to connect to a web server, so we need to register the Servlet we wrote in web.xml.
  • You also need to provide a path that the browser can access.

[Note]: In /hi, the slash / must not be less, otherwise IDEA will report an error when starting Tomcat.

<?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>
        <servlet-name>hi</servlet-name>
        <servlet-class>servlet.MyServlet</servlet-class>
    </servlet>
    <!--servlet请求路径,斜杠/不能少,否则启动Tomcat会报错-->
    <servlet-mapping>
        <servlet-name>hi</servlet-name>
        <url-pattern>/hi</url-pattern>
    </servlet-mapping>
</web-app>

5. Tomcat configuration

  • First, make sure that the project to be tested has at least one artifact. If there is none, click + Add.

  • Added a local Tomcat test instance.

 

 

6. Start the test

  • First enter http://localhost:8080/servlet_war/ to display the default page index.jsp.

  • Add the servlet access path /hi, and send the parameter?method=add, enter the page http://localhost:8080/servlet_war/hi?method=add. The mapped servlet returns the msg content according to the parameter method=add sent by the front-end "Execute an add method~".

What does the MVC framework do

  • Map URL to java class or method of java class.
  • Encapsulate the data submitted by the user.
  • Process the request-call related business processing-encapsulate the response data.
  • Render the corresponding data, jsp/html and other presentation layer data.

Original link: https://www.cnblogs.com/letsdaydayup/p/14431529.html

If you think this article is helpful to you, you can forward it and follow it to support it

 

Guess you like

Origin blog.csdn.net/weixin_48182198/article/details/113991385