Last Modified 2024.3.31

Jakarta EE 9

테스트 환경: JDK 11 LTS, Jetty 11

ee9exam
├── pom.xml
├── src
│   └── main
│       └── webapp
│           ├── WEB-INF
│           │   └── web.xml
│           └── index.jsp
pom.xml
<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
    http://maven.apache.org/xsd/maven-4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>

<groupId>net.java_school</groupId>
<artifactId>ee9exam</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>

<name>ee9exam Maven Webapp</name>
<url>http://localhost:8080</url>

<properties>
	<project.build.sourceEncoding>utf-8</project.build.sourceEncoding>
	<project.reporting.outputEncoding>utf-8</project.reporting.outputEncoding>
</properties>

<dependencies>
	<dependency>
		<groupId>junit</groupId>
		<artifactId>junit</artifactId>
		<version>4.11</version>
		<scope>test</scope>
	</dependency>
	<!-- https://mvnrepository.com/artifact/jakarta.servlet/jakarta.servlet-api -->
	<dependency>
		<groupId>jakarta.servlet</groupId>
		<artifactId>jakarta.servlet-api</artifactId>
		<version>5.0.0</version>
		<scope>provided</scope>
	</dependency>
</dependencies>

<build>
	<finalName>ee9exam</finalName>
	<pluginManagement>
		<plugins>
			<plugin>
				<artifactId>maven-clean-plugin</artifactId>
				<version>3.1.0</version>
			</plugin>
			<plugin>
				<artifactId>maven-resources-plugin</artifactId>
				<version>3.0.2</version>
			</plugin>
			<plugin>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.8.0</version>
			</plugin>
			<plugin>
				<artifactId>maven-surefire-plugin</artifactId>
				<version>2.22.1</version>
			</plugin>
			<plugin>
				<artifactId>maven-war-plugin</artifactId>
				<version>3.2.2</version>
			</plugin>
			<plugin>
				<artifactId>maven-install-plugin</artifactId>
				<version>2.5.2</version>
			</plugin>
			<plugin>
				<artifactId>maven-deploy-plugin</artifactId>
				<version>2.8.2</version>
			</plugin>
			<!-- https://mvnrepository.com/artifact/org.eclipse.jetty/jetty-maven-plugin -->
			<plugin>
				<groupId>org.eclipse.jetty</groupId>
				<artifactId>jetty-maven-plugin</artifactId>
				<version>11.0.12</version>
			</plugin>
		</plugins>
	</pluginManagement>
</build>
</project>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="5.0"
    xmlns="https://jakarta.ee/xml/ns/jakartaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee
    https://jakarta.ee/xml/ns/jakartaee/web-app_5_0.xsd">

</web-app>

src/main/webapp/

index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>jakartaEE 9 Examples</title>
</head>
<body>
<c:choose>
    <c:when test="${empty param.name }">
        <h4>Hello, World!</h4>
    </c:when>
    <c:otherwise>
        <h4>Hello, ${param.name }!</h4>
    </c:otherwise>
</c:choose>
</table>
</body>
</html>
mvn jetty:run 

Hello, World!

Hello, John!

mkdir -p src/main/java/example

vi src/main/java/example/InitParamServlet.java

Jakarta EE 9에선 패키지 이름으로 javax.* 대신 jakarta.* 를 사용한다.

InitParamServlet.java
package example;

import java.io.IOException;
import java.io.PrintWriter;

import jakarta.servlet.ServletConfig;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

public class InitParamServlet extends HttpServlet {

	private String url;
	private String user;
	private String passwd;
	private String driver;

	@Override
	public void init() throws ServletException {
		url = this.getInitParameter("url");
		user = this.getInitParameter("user");
		passwd = this.getInitParameter("passwd");
		driver = this.getInitParameter("driver");
	}

	@Override
	public void doGet(HttpServletRequest req, HttpServletResponse resp) 
		throws IOException, ServletException {

		resp.setContentType("text/html; charset=UTF-8");
		PrintWriter out = resp.getWriter();

		out.println("url: " + url + "<br />");
		out.println("user: " + user + "<br />");
		out.println("passwd: " + passwd + "<br />");
		out.println("driver: " + driver);

	}

}
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="5.0"
    xmlns="https://jakarta.ee/xml/ns/jakartaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee
    https://jakarta.ee/xml/ns/jakartaee/web-app_5_0.xsd">

<servlet>
	<servlet-name>InitParamServlet</servlet-name>
	<servlet-class>example.InitParamServlet</servlet-class>
	<init-param>
		<param-name>driver</param-name>
		<param-value>oracle.jdbc.driver.OracleDriver</param-value>
	</init-param>
	<init-param>
		<param-name>url</param-name>
		<param-value>jdbc:oracle:thin:@127.0.0.1:1521:XE</param-value>
	</init-param>
	<init-param>
		<param-name>user</param-name>
		<param-value>scott</param-value>
	</init-param>
	<init-param>
		<param-name>passwd</param-name>
		<param-value>tiger</param-value>
	</init-param>
</servlet>

<servlet-mapping>
	<servlet-name>InitParamServlet</servlet-name>
	<url-pattern>/initParam</url-pattern>
</servlet-mapping>

</web-app>
mvn jetty:run

http://localhost:8080/initParam

InitParam Servlet Test

스프링 5.3.x 버전대는 Java EE 7-8 스펙을 따른다.
스프링 6 버전은 Jakarta EE 9+ 스펙을 따른다.
Spring 6은 최소 JDK 17, Apache Tomcat 10 또는 Jetty 11이 필요하다.

책 예제를 Jakarta EE 9로 수정

책 예제를 Spring 6을 사용하도록 수정했다.
Spring 6은 최소 JDK 17, Apache Tomcat 10 또는 Jetty 11이 필요하다.
Test: JDK 17, Jetty 11, Spring 6, Spring-Security 6, Servlet 3 File Upload, DBCP2, MyBatis 3.5.x, Hibernate Validator 8

최종 소스: https://github.com/kimjonghoon/JavaWebProgramming2

실행 방법
src/main/resources/log4j2.xml 파일에서 로그 경로를 자신의 자신의 시스템에 맞게 수정한다.
src/main/java/net/java_school/commons/WebContants.java 에서 UPLOAD_PATH를 자신의 시스템에 맞게 수정한다.
톰캣이 정지시킨 후 아래 명령으로 jetty를 실행한다.
mvn jetty:run

참조