I'm currently studying servlet / JSP / tomcat at paiza. I will write Keta as part of the output.
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.
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.
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.
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 after writing.
> cd tomcat/webapps/Corresponding file name
> javac -classpath "../../lib/servlet-api.jar" -d WEB-INF/classes HelloWorld.java
Create mywork.xml in tomcat / conf / Catalina / localhost and write the following.
<Context
reloadable="true"/>
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>
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.
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
<%- -%>
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>
It was easy, but I summarized it!
Recommended Posts