[JAVA] Create a Servlet program in Eclipse

Introduction

As a prerequisite, it is assumed that Eclipse has been installed. Eclipse Download This time, I aim to output Hello World using Servlet and JSP in Eclipse.

What is Servlet in the first place?

A Servlet is a program that runs on a web server, that is, on the back end, and is a program written in Java to realize a dynamic web page.

** What is a dynamic web page **? A page that changes the appearance of the page according to the person who accessed it even if the same URL is requested. For example, on an EC site (a site where you can shop online), pages are configured in consideration of your browsing history and information added to your favorites, and pages are provided according to the tastes and behavioral tendencies of each person who visits. .. Servlets are available as one of these methods.

Learning Servlet

Learning Servlets is good for Java beginners

――A technology that is still very popular today. --Since the web framework hides the Servlet, I rarely write a plain Servlet. Knowledge of Servlet is required when problems occur. --Learn the basic mechanism of web applications

Run Servlet

Execution environment

Servlet project creation

Create a Servlet. Go through Eclipse's file newother web folderdynamic web project. Enter a suitable project name Select the Java version you have for the target runtime (You can check the java version that can be used from the settings) → Done

Creating a Servlet class

Create a Servlet that runs on a web server. File NewOther Web folderServlet Suitable package name, Class name (servlet, SampleTest this time) →Done Then the following Java file is created.

SampleTest.java



package servlet;

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

/**
 * Servlet implementation class SampleTest
 */
@WebServlet("/SampleTest")
public class SampleTest extends HttpServlet {

    /**
     * @see HttpServlet#HttpServlet()
     */
    public SampleTest() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.getWriter().append("Served at: ").append(request.getContextPath());
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}
}

Output Hello World

Servlet class to forward

This time we will edit the doGet method.

SampleTest.java


  protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
    request.getRequestDispatcher("/WEB-INF/view/sample.jsp").forward(request, response);
  }

Forwarding is done by the forward () method of the RequestDispatcher instance.

** Forward syntax **

RequestDispatcher dispatcher =
  request.getRequestDispatcher("Forward destination");
dispatcher.foward(request,response);

As the forward destination, not only the JSP file but also the Servlet class can be specified.

--For JSP files -Path from / Web-Content --For Servlet class -/ URL pattern

Use JSP file

The controller is handled by a Servlet class suitable for receiving requests from users and performing overall control. The output view is handled by a JSP file that specializes in HTML output.

Creating a JSP file

Right-click on WebContent / WEB-INF`` → folder enter the folder namedone`

Prohibition of direct requests to JSP files

** Place under WEB-INF ** When you create a WEB application, the request from the browser is basically the Servlet class. Since the JSP file is created on the assumption that it will be forwarded from the Servlet class and will work, errors and problems may occur when it is called from the browser, so make sure that you cannot make a direct request. Browsers cannot directly request files located under WEB-INF.

JSP file to display

sample.jsp


<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>Hello World</h1>
</body>
</html>

This time I want to output Hello World, so enter it in the JSP file.

Start the server

Now that you're ready, let's start the server. Right-click on SampleTest.java`` Execute Execute on server Select the server type Tomcat v9.0 Server HelloWorld出力

Success if the above screen is output!

Finally

This time I used Sevlet to output the basic Hello World Next time, we will use the doPost method to get the parameters.

Recommended Posts

Create a Servlet program in Eclipse
Create a simple batch processing framework in Eclipse.
Null-safe program in Java (Eclipse)
Create a Jetty project using Eclipse
Create a tomcat project using Eclipse
Create a base for your batch processing project in Eclipse.
Create a database in a production environment
Create a new app in Rails
Create a Java project using Eclipse
Create a tomcat project using Eclipse Pleiades All in One
Create a memo app with Tomcat + JSP + Servlet + MySQL using Eclipse
Building a Lambda development environment in Eclipse
Create a TODO app in Java 7 Create Header
Creating a Servlet in the Liberty environment
MVC in Eclipse.
I made a primality test program in Java
Create a CSR with extended information in Java
Let's create a custom tab view in SwiftUI 2.0
Let's create a super-simple web framework in Java
[Eclipse / Tomcat] Servlet + JSP in Maven webapp project
How to create a theme in Liferay 7 / DXP
Create a tool for name identification in Salesforce
How to easily create a pull-down in Rails
I wrote a prime factorization program in Java
Create a native extension of Ruby in Rust
How to automatically generate a constructor in Eclipse
How to create a Java environment in just 3 seconds
[Java] Create a filter
[CentOS, Eclipse] Load a library file in a C project
Created a menu program
I tried to create a Clova skill in Java
To debug in eclipse
How to launch another command in a Ruby program
Create JSON in Java
[Programming complete] §5 Create a review management app in Ruby
Call a program written in Swift from Processing (Java)
What is a Servlet?
Create a Java Servlet and JSP WAR file to deploy to Apache Tomcat 9 in Gradle
Create a frameless non-rectangular window in JavaFX without a taskbar
Create a MySQL test environment (+ millions of test data) in 5 minutes
Create a jar file that can be executed in Gradle
[Beginner] I made a program to sell cakes in Java
How to create a placeholder part to use in the IN clause
Create a MySQL container for application testing in Ansible AWX
Create a SlackBot with AWS lambda & API Gateway in Java
Create a method to return the tax rate in Java
Until you create a Web application with Servlet / JSP (Part 1)
I want to create a Parquet file even in Ruby
A program that counts the number of words in a List
How to create a service builder portlet in Liferay 7 / DXP
[Enum_help] Use enum_help to create a select box displayed in Japanese!
Create hyperlinks in Java PowerPoint
Create a java method [Memo] [java11]
Install the plugin in Eclipse
[Java] Create a temporary file
Multiplication in a Ruby array
Create a VS Code Plugin.
Find a subset in Java
Create a playground with Xcode 12
Create Azure Functions in Java
To write a user-oriented program (1)