Tomcat Servlet and run the sample

Tomcat Servlet and run the sample

This article will write a servlet, and then deploy it to the whole process of Tomcat. Reference herein "thorough dismantling Tomcat_Jetty" content.

First, the development steps based web.xml

  1. Download, install Tomcat.
  2. Write a Java class inheritance of MyServlet HTTPServlet.
  3. The compiled Java class files into Class file.
  4. Establish Web application directory structure and configure web.xml.
  5. Deployment of Web applications.
  6. Start Tomcat.
  7. Browser access to verify the results.
  8. View Tomcat logs.

1 download, install Tomcat

To Tomcat's official website (https://tomcat.apache.org/download-90.cgi) Download the latest Tomcat (need to download the corresponding system version, select the actual situation, this paper MacOS system as an example). After the download is complete you can extract.

file

2 write MyServlet class

MyServlet write a class that inherits HttpServlet class, and its replication doGet () and doPost () method.

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;

/**
 * @Version V1.0
 * @Description: MyServlet 类
 */
public class MyServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("MyServlet 在处理 get()请求...");
        PrintWriter out = resp.getWriter();
        resp.setContentType("text/html;charset=utf-8");
        out.println("<strong>doGet My Servlet!</strong><br>");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("MyServlet 在处理 post()请求...");
        PrintWriter out = resp.getWriter();
        resp.setContentType("text/html;charset=utf-8");
        out.println("<strong>doPost My Servlet!</strong><br>");
    }
}

3. Compile MyServlet

Because here we do not use IDE way, so when compiling MyServlet.java class, we use the javac command. But we need to pay attention, because we inherited HttpServlet MyServlet in class, but not HttpServlet jdk own class, so it is necessary to bring the jar package HttpServlet where packaged together! Otherwise, the following error message:

Our first Tomcat extract the directory lib / servlet-api.jar under MyServlet.java copied to the same directory and execute the following command to compile the MyServlet:

javac -cp ./servlet-api.jar MyServlet.java

4 build Web applications directory

Create a folder, which is the project MyWebApp, its main catalog as follows:

MyWebApp/WEB-INF/web.xml
MyWebApp/WEB-INF/classes/MyServlet.class

Wherein the content web.xml as follows:

<?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"
  metadata-complete="true">
 
    <description> Servlet Example. </description>
    <display-name> MyServlet Example </display-name>
    <request-character-encoding>UTF-8</request-character-encoding>
 
    <servlet>
      <servlet-name>myServlet</servlet-name>
      <servlet-class>MyServlet</servlet-class>
    </servlet>
 
    <servlet-mapping>
      <servlet-name>myServlet</servlet-name>
      <url-pattern>/myservlet</url-pattern>
    </servlet-mapping>
 
</web-app>

5 application deployment MyWebApp

MyWebApp this directory are copied to directory under the webapps Tomcat installation directory:

Start Tomcat 6

Find Tomcat installation directory under the bin directory, if it is a Windows system, perform startup.bat; if a Linux system, perform startup.sh

7 browser access

Visit this URL in your browser: http://localhost:8080/MyWebApp/myservlet

Similarly, post invoke will get similar results!

8 View Tomcat log

We can look tomcat / catalina.out files under logs, use the following command to view the log:

tail -100f catalina.out

Second, the annotation-based development @WebServlet

Based on the above-described step web.xml substantially consistent with, the following points have different!

It is different from a 1: MyServlet class code

Add @WebServlet code on MyServlet, as follows:

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @Version V1.0
 * @Description: MyServlet 类
 */
@WebServlet("/myAnnotationServlet")
public class MyServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("MyServlet 在处理 get()请求...");
        PrintWriter out = resp.getWriter();
        resp.setContentType("text/html;charset=utf-8");
        out.println("<strong>doGet My Servlet!</strong><br>");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("MyServlet 在处理 post()请求...");
        PrintWriter out = resp.getWriter();
        resp.setContentType("text/html;charset=utf-8");
        out.println("<strong>doPost My Servlet!</strong><br>");
    }
}

2 differs from the 2: Configuration removed web.xml

Under MyWebApp project you do not need to configure web.xml, because the notes have been subjected to the relevant configuration.

33 difference: access address change

Because the configuration of the @WebServlet the path / myAnnotationServlet, therefore access path here would be:
http://localhost:8080/MyWebApp/myAnnotationServlet

The remaining steps are basically the same configuration on web.xml no longer dwell! Late this case will explain how the analysis is to load and run Tomcat Servlet is!

Micro-channel public number: Source Bay

We welcome the focus on micro-channel public number: Source Bay. This number will from time to time the public associated source code and share relevant technology development, common development and common progress ~


Blog:

  • Jane book: https://www.jianshu.com/u/91378a397ffe
  • CSDN: https://blog.csdn.net/ZhiyouWu
  • Open Source China: https://my.oschina.net/u/3204088
  • Nuggets: https://juejin.im/user/5b5979efe51d451949094265
  • Park blog: https://www.cnblogs.com/zhiyouwu/
  • Micro-channel public number: Source Bay
  • Micro letter: WZY1782357529 (welcome communication)

Guess you like

Origin www.cnblogs.com/zhiyouwu/p/11612523.html