[Java] Inheritance and structure of HttpServlet class

table of contents

1. Introduction 2. What is Servlet? 3. Inheritance of HttpServlet class 4. Structure of HttpServlet class 5. Sample 6. Finally

1.First of all

This article was created for the following people. ・ "Let's use Java for the first time" ・ "I want to make a web application using Java" ・ "The word Servlet came out ..." I hope it helps you in your learning.

It is described with reference to the following. Citation site: [Java] Servlet creation (basic coding)

2. What is Servlet?

Servlet is a program written in Java to dynamically process and generate web pages and web applications on the server. It is also a function of Java EE.

3. Inheritance of HttpServlet class

Generally, when creating a Servlet, inherit the javax.servlet.http.HttpServlet class.

servlet.java


import javax.servlet.http.HttpServlet;

public class SampleServlet extends HttpServlet {

}

Since the Java library provides a class that implements the functions required for Servlets (functions specialized for HTTP communication), developers inherit HttpServlet and override their methods to create their own Servlets. You can create it.

4. Structure of HttpServlet class

The structure of HttpServlet class is expressed as follows. HttpServletクラスの構造図

-Main methods of HttpServlet class

init () method A method that initializes the Servlet. Get initialization parameters. First called when instantiating a Servlet.

service () method A method that accepts a request and returns a response. Executed on every request from the client. The service () method of the HttpServlet class makes the following branches according to the type of client request. ・ Http GET method → ​​doGet () method is executed ・ Http POST method → ​​doPost () method is executed

doGet () method A method that accepts a request with the http GET method and returns a response. It is executed by branching from the service () method.

doPost () method A method that accepts a request with the http POST method and returns a response. It is executed by branching from the service () method.

destroy () method A method that describes the processing required at the end of the Servlet. Called when an instance of the Servlet is destroyed.

5. Sample

This is a sample using the doGet method. Create a class that inherits from HttpServlet and override the doGet () method.

doGetSample.java


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

public class HelloServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
        //Fill in the process
        //Example) Analyze and use the contents of request
        //・ ・ ・
        //Example) Enter the content you want to return in response
    }
}

Feel free to code and use the above processing entry.

6. Finally

This article is a brief introduction to just the beginning of Servlet. If you look closely, you will find deeper knowledge and implementation methods, so please try it. The articles that I have referred to in creating this article are listed below. We hope that this will be of some help to you.

Reference article: Creating a Servlet (basic coding)

Recommended Posts

[Java] Inheritance and structure of HttpServlet class
[Java] Class inheritance
[Java] Set structure of collection class (about HashSet and TreeSet)
Java programming (class structure)
Java and Swift comparison (3) Class implementation / Class inheritance / Class design
[Java] Comparator of Collection class
Java class definition and instantiation
Summary of Java Math class
[Java] What is class inheritance?
java (inheritance of is-a principle)
Advantages and disadvantages of Java
Use of Abstract Class and Interface properly in Java
[Ruby] Class nesting, inheritance, and the basics of self
[Java] Handling of character strings (String class and StringBuilder class)
Various methods of Java String class
About fastqc of Biocontainers and Java
StringBuffer and StringBuilder Class in Java
[Java] Judgment of identity and equivalence
Basic structure of Java source code
Outputs a list of CDK descriptor, class name, and 3D structure descriptor.
[Java] Introductory structure Class definition Relationship between class and instance Method definition format
[Java] Inheritance
Java inheritance
Mechanism and characteristics of Collection implementation class often used in Java
Java inheritance
Read the first 4 bytes of the Java class file and output CAFEBABE
java (inheritance)
After 3 months of Java and Spring training
[Java] Differences between instance variables and class variables
[Java / Swift] Comparison of Java Interface and Swift Protocol
Proper use of interface and abstract class
Java programming (static clauses and "class variables")
Summary of Java Math.random and import (Calendar)
[Java] Contents of Collection interface and List interface
Basics of java basics ② ~ if statement and switch statement ~
Discrimination of Enums in Java 7 and above
Java beginner escape boot camp Part 1 Java class structure and how to write
Display Japanese calendar and days of the week using java8 standard class
Java Basic Learning Content 6 (Inheritance / Abstract Class / Interface)
Java class methods
Class and model
[Java] How to use FileReader class and BufferedReader class
[ev3 × Java] Interface, implementation and inheritance (event handling)
Java HashMap class
About class inheritance.
[Java] The confusing part of String and StringBuilder
java (abstract class)
I compared the characteristics of Java and .NET
[Java] Nested class
Java anonymous class
JAVA: Realizes generation and scanning of various barcodes
[Java] Basic structure
Basics of threads and Callable in Java [Beginner]
About Java class
[Java] Classification memo of compilation error and run-time error
About next () and nextLine () of the Scanner class
Java and JavaScript
A quick review of Java learned in class
XXE and Java
About java inheritance
[java] abstract class