1. Introduction 2. What is Servlet? 3. Inheritance of HttpServlet class 4. Structure of HttpServlet class 5. Sample 6. Finally
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)
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.
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.
The structure of HttpServlet class is expressed as follows.
-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.
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.
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