[Java] Beginner's understanding of Servlet-①

table of contents

--Introduction --What is Servlet? --Class declaration --Main methods - doGet(), doPost() - getWriter() - setContentType() --Exception

Introduction

This article is ・ "I'm going to do Java now" ・ "I'm doing Java, but I want to review it." It is a rough content for people. I am also studying hard, so I do not guarantee the accuracy of the content. .. .. I hope it will help you in your learning ~~ (notepad for your own learning) ~~.

What is Servlet?

In a nutshell, it's a server-side Java program </ b>.

・ Normal class file → File exchange in the local environment -Servlet class file → The Servlet performs processing according to the HTTP request and outputs it as a response. Image of.

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

public class HelloServlet extends HttpServlet {
    
    //Method to set the HTTP response to be returned for the HTTP request sent by GET
    public void doGet(HttpServletRequestrequest,HttpServletResponse response)
            throws IOException, ServletException {

        //Change the format of the file returned as an HTTP response to "html"
        //Set the character set to "UTF-Designated as "8"
        response.setContentType("text/html; charset=UTF-8");
        PrintWriter out = response.getWriter();
        out.println("<body>HelloServlet</body>");

    }
}

Class declaration

Servlet is created as an inherited class of HttpServlet class

public class HelloServlet extends HttpServlet{
}

Main methods

●doGet(), doPost();

public void doGet[doPost](HttpServletRequestrequest,HttpServletResponse){
}

A method that returns an HTTP response to a Get / Post request. The first argument corresponds to the HTTP request and the second argument corresponds to the HTTP response.

●setContentType()

response.setContentType("text/html; charset=UTF-8");

A method that specifies the "format" and "character code" of the file to be output as an HTTP response. The set value is reflected in Content-Type of html header

●getWriter()

PrintWriter out = response.getWriter();

Method to get stream for output By creating the PrintWriter class as an out object, you can use PrintWriter class methods such as println ().

  • The contents of println () here are written as an html file.

exception

The following two exceptions occur

  • IOException
  • ServletException

Since the Servlet class is not operated by itself and is often operated by calling from another main class (?), Exceptions are thrown to the calling class.

public void doGet(HttpServletRequestrequest,HttpServletResponse response)
        throws IOException, ServletException {
}

in conclusion

Difficult sentences / difficult to understand ... I want to be able to write more sentences. I'll do my best to post so that I can improve gradually ~~

Then.

Recommended Posts

[Java] Beginner's understanding of Servlet-②
[Java] Beginner's understanding of Servlet-①
[For beginners] Summary of java constructor
Step-by-step understanding of Java exception handling
Summary of Docker understanding by beginners ② ~ docker-compose ~
[Java] Overview of Java
A collection of simple questions for Java beginners
Summary of Docker understanding by beginners ① ~ docker run -p ~
Java Servlet should be aware of multithreaded environment
[Introduction to Java] Basics of java arithmetic (for beginners)
Expired collection of java
Predicted Features of Java
[Java] Significance of serialVersionUID
NIO.2 review of java
Review of java Shilber
RxJava Beginner's Recommendation of RxJava
java --Unification of comments
List of beginners (List) memo
History of Java annotation
java (merits of polymorphism)
Java Servlet LifeCycle Example
NIO review of java
[Java] Three features of Java
Summary of Java support 2018
[For beginners] Quickly understand the basics of Java 8 Lambda
[Java] When writing the source ... A memorandum of understanding ①
Introduction to Java for beginners Basic knowledge of Java language ①
List of frequently used Java instructions (for beginners and beginners)
"No.1 understanding of this kind of thing" tomcat restart [Java]
[For beginners] Explanation of classes, instances, and statics in Java
About an instance of java
[Java] Mirage-Basic usage of SQL
[Java] Practice of exception handling [Exception]
[Java11] Stream Summary -Advantages of Stream-
Basics of character operation (java)
Java debug execution [for Java beginners]
[Java] Basic statement for beginners
[Java] Creation of original annotation
4th day of java learning
Java end of month plusMonths
[Java] Summary of regular expressions
[Java] Summary of operators (operator)
[Java] Implementation of Faistel Network
[Java] Comparator of Collection class
Object-oriented summary by beginners (Java)
Java beginners briefly summarized the behavior of Array and ArrayList
"Understanding this kind of thing No.2" Volume of comparison operators [Java]
Java Servlet / JSP View drawing
Recommendation of set operation by Java (and understanding of equals and hashCode)
Summary of Java language basics
Summary of Java Math class
Java for beginners, data hiding
Enumeration of all combinations Java
Memorandum of understanding about LOD.
Understanding Java Concurrent Processing (Introduction)
java (inheritance of is-a principle)
Advantages and disadvantages of Java
Java Servlet 4.0 web.xml schema definition
Java beginners read Hello World
Benefits of Java static method
Reintroduction to Java for Humanities 0: Understanding the Act of Programming