[Java] About [servlet][JSP][tomcat]

2 minute read

Introduction

I am currently studying about servlet/JSP/tomcat at paiza. I will write Keta as a part of the output.

What is a servlet

A program written in JAVA that runs on a WEB server. My job is to perform processing in response to a request from a web browser.

What is JSP

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

What is tomcat

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

Basic way of writing servlet/JSP

Create Java file

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 be able to read the JSP file that will be created later
        String view = "/WEB-INF/views/index.jsp";
        RequestDispatcher dispatcher = request.getRequestDispatcher(view);
        dispatcher.forward(request, response);
    }
}

Compile

Compile when you are done.

> 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/Catarina/localhost and write the following.


<Context
reloadable="true"/>

Create jsp file

Create 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> // 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>

With this, when you access with the url called the relevant file /hello, Hello World is displayed.

JSP tags

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

// execute code
<% %>
// execute the code and output the result
<%= %>
// JSP declaration (used at the beginning of index.jsp)
<%@ %>
// method or variable declaration
<%! %>
// comment
<%- -%>

Send data from the servlet to JSP and display it.

into a 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 HttpServletRequest above.
       // Hello world is displayed when using message in JSP. Be careful because 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>
        
        // Get the message from the request object. Note that this is getAttribute.
        // Cast to String type, assign to String type mesage, and display it.
        <% String message = (String)request.getAttribute("message");%>
        <p><%= message %></p>
    </body>
</html>

At the end

It was easy, but I’ve put together!