We will introduce for beginners how to get the request from the front by HTTP communication using Java.
This time, when fetching a request by HTTP communication with Java, a Java program called Servlet is used.
A Servlet is a Java program that runs on a web server such as Tomcat. It plays a role in responding to requests from web browsers and processing sent data.
In this sample program, a request is made from a web browser, We will create a program that returns a simple HTML response to the browser.
After that, it will be explained in the following versions and environments.
IDE:eclipse Java version: 8 Tomcat:8
The folder structure of this sample program is as follows.
First, select "Dynamic Web Project" from Create New Project and proceed to the next.
Next, name the project "SampleHttpServer". Also, make sure the target runtime is "Tomcat 8". If there is no problem, click the Finish button.
First right click on the src directory and select New> Other.
Then, the following browser will appear. Select "Servlet" and proceed to the next.
Then, name the class "HttpServletTest". Java packages may or may not be listed. If not stated, the Servlet will be created in the default package.
After entering the class name, proceed to the next.
Uncheck the superclass constructor and inherited abstract methods.
Press Finish to complete the creation of the Servlet class.
This time, we will create a simple PGM that returns "Sucess!" To the browser when the URL http: // localhost: 8080 / SampleHttpServer / HttpServletTest
is executed in the browser.
First, rewrite the annotation part as follows.
name refers to the name of the Servlet and works without mentioning it, but it is often customarily written. urlPatterns is the URL (relative path) to access this Servlet.
It's OK to understand that "private static final long serialVersionUID = 1L" is like a magic trick when reading and writing data.
java
@WebServlet(name = "HttpServletTest", urlPatterns = { "/HttpServletTest" })
public class HttpServletTest extends HttpServlet {
private static final long serialVersionUID = 1L;
}
Next, I will write the source code that receives the request from the browser and returns the HTML response.
java
@WebServlet(name = "HttpServletTest", urlPatterns = { "/HttpServletTest" })
public class HttpServletTest extends HttpServlet {
private static final long serialVersionUID = 1L;
//The part to be added this time
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("Request receipt");
//HTTP response header content type
response.setContentType("text/html");
//HTTP body part
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<meta charset=\"UTF-8\">");
out.println("<title>Test</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Success!</h1>");
out.println("</body>");
out.println("</html>");
System.out.println("Return response");
}
First of all, the doget () method is described as above every time you make a GET request.
When returning a response, setContentType specifies the form in which the response is returned. This time, the response is returned in HTML, so "text / html" is specified.
Then, use the response.getWriter () method to get the "PrintWriter" class object for outputting characters to the client, and output the HTML text.
Also, in order to check that it was received on the server side, Sytem.out.println makes it possible to know the receipt of the request and the return of the response.
Right-click on the project and press Run on Server.
When the server starts, execute the following URL in your browser, and when "Success!" Is displayed as shown below, it is OK. Also, check that the log "Request reception / Response return" is output to the console.
http://localhost:8080/SampleHttpServer/HttpServletTest
I started my personal blog in 2020!
Based on the knowledge and experience gained as a freelance engineer, we plan to distribute content such as information about freelance engineers, IT technical information, industry information, and engineer life hacks.
The number of articles is still small, but it is updated weekly, so if you are interested, I would be grateful if you could take a look.
https://yacchi-engineer.com/
Recommended Posts