[JAVA] About [servlet] [JSP] [tomcat]

Introduction

I'm currently studying servlet / JSP / tomcat at paiza. I will write Keta as part of the output.

What is a servlet?

It is a program written in JAVA that runs on a WEB server. My job is to respond to requests from web browsers and process them.

What is JSP

The servlet did the internal processing, but the JSP does the job of displaying characters on the screen. Embed JAVA in HTML and display WEB dynamically. It is an erb file in rails.

What is tomcat

It is a servlet container used when running servlets and JSPs. When an HTTP request comes in, I instruct the servlet to make it work.

Basic way to write servlet / JSP

Java file creation

Create a file like HelloWorld.java in tomcat / webapps / corresponding file name.

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class HelloWorld extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException {
        
         //Set to read the JSP file to be created later
        String view = "/WEB-INF/views/index.jsp";
        RequestDispatcher dispatcher = request.getRequestDispatcher(view);
        dispatcher.forward(request, response);
    }
}

Compile

Compile after writing.

> cd tomcat/webapps/Corresponding file name

> javac -classpath "../../lib/servlet-api.jar" -d WEB-INF/classes HelloWorld.java

No need to restart tomcat

Create mywork.xml in tomcat / conf / Catalina / localhost and write the following.


<Context
reloadable="true"/>

Create a jsp file

Create a views directory in tomcat / webapps / corresponding file name / WEB-INF. And create index.jsp in it.


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

Specify the routing.

Create web.xml in tomcat / webapps / corresponding file name / WEB-INF.


<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
        http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
        version="4.0" metadata-complete="true">

    <servlet>
        <servlet-name>Hello</servlet-name>
        <servlet-class>HelloWorld</servlet-class> //The class name of the java file created earlier
    </servlet>
    <servlet-mapping>
        <servlet-name>Hello</servlet-name>
        <url-pattern>/hello</url-pattern> //Describe the path
    </servlet-mapping>

</web-app>

Now when you access with the corresponding file / url called hello, Hello World will be displayed.

JSP tag

The types of tags used when embedding JAVA in JSP are as follows. This is the <%%> part of rails.

//Code execution
<% %> 
//Execute the code and output the result
<%= %>
//JSP declaration (index).Used at the beginning of jsp)
<%@ %>
//Declaration of methods and variables
<%! %>
//comment
<%- -%>

Data is sent from the servlet to the JSP and displayed.

to servlet

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class HelloWorld extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException {
       
       //request is the above HttpServletRequest.
       //Hello world is displayed when message is used in JSP. Note that it is a setAttribute.
       request.setAttribute("message", "Hello world");
    }
}

On the JSP side


<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Hello Template</title>
    </head>
    <body>
        
        //Extract the previous message from the request object. Note that this is getAttribute.
        //Cast to String type, assign to String type mesage, and display as.
        <% String message = (String)request.getAttribute("message");%>
        <p><%= message %></p>
    </body>
</html>

At the end

It was easy, but I summarized it!

Recommended Posts

About [servlet] [JSP] [tomcat]
About image upload of jsp (servlet)
JSP on tomcat
[Eclipse / Tomcat] Servlet + JSP in Maven webapp project
Java Servlet / JSP View drawing
Getting Started with JSP & Servlet
JSP error display from Servlet
Web application development memo with MVN, Tomcat, JSP / Servlet with VScode
How to use scope (JSP & Servlet)
Create a memo app with Tomcat + JSP + Servlet + MySQL using Eclipse
Servlet
Java Servlet / JSP Request Scope Part 2
Passing parameters from JSP with Servlet
I'm not sure about Java Servlet
Hello World with GlassFish 5.1 + Servlet + JSP
About =
Deploy Java Servlet app locally on Tomcat
[Introduction to JSP + Servlet] A little animation ♬