Servlet: 서버에서 웹페이지 등을 동적으로 생성하거나 데이터 처리를 수행하기 위해 자바로 작성된 프로그램
new dynamic web project > configure > convert to Maven project
package > create Servlet(inherited abstract methods, doGet)
클라이언트에서 GET 방식의 요청이 왔을 때 WAS(Web Application Server, Tomcat)가 호출하는 메서드.
파라미터 request: 클라이언트가 보낸 요청에 대한 정보를 가지고 있는 객체.
파라미터 response: WAS가 클라이언트로 보낼 응답을 작성하기 위해 필요한 기능들을 가지고 있는 객체.
<form action="form-result.jsp" method="post">
<input type="text" name="username" placeholder="이름 입력" required autofocus />
<input type="submit" value="전송" />
</form>
<jsp:forward></jsp:forward>
<jsp:include></jsp:include>
<jsp:useBean></jsp:useBean>
<jsp:getProperty></jsp:getProperty>
<jsp:setProperty></jsp:setProperty>
JSP에서 상태 정보들을 유지하기 위해서 사용하는 객체(변수 이름)들:
EL에서 상태 정보들을 유지하기 위해서 사용하는 객체(변수 이름)들:
EL에서 상태 변수를 찾는 순서: ${ var }
${ pageScope.var }
==> ${ requestScope.var }
==> ${ sessionScope.var }
==> ${ applicationScope.var }
<c:forEach items="${ contacts }" var="c">
<c:set var="colorValue2" value="black"></c:set>
<c:if test="${ param.color == 'r' }"></c:if>
<c:choose>
<c:when test="${ param.username == 'admin' }">
<h2>관리자 페이지</h2>
</c:when>
<c:otherwise>
<h2>일반 사용자 페이지</h2>
</c:otherwise>
</c:choose>
<c:url var="reqURL" value="form2-result.jsp">
<c:param name="username" value="adm&in"></c:param>
<c:param name="color" value="g"></c:param>
</c:url>
<c:url>에서 "/" 요청주소는 context root까지.
<c:url var="mainPage" value="/"></c:url>
<a href="${ mainPage }">메인 페이지</a>
java.sql.Date, java.sql.Timestamp 타입의 객체들을 원하는 형식으로 포맷팅.
java.time.LocalDate, java.time.LocalDateTime 객체들은 포맷팅을 못함.
<c:set var="now" value="<%= Timestamp.valueOf(LocalDateTime.now()) %>"></c:set>
<h2>${ now }</h2>
<h2>date:
<fmt:formatDate value="${ now }" type="date" />
</h2>
<h2>time:
<fmt:formatDate value="${ now }" type="time" />
</h2>
<h2>date & time:
<fmt:formatDate value="${ now }" type="both" />
</h2>
<h2>
<fmt:formatDate value="${ now }" type="both" dateStyle="full" timeStyle="full" />
</h2>
<h2>
<fmt:formatDate value="${ now }" type="both" dateStyle="short" timeStyle="short" />
</h2>
<h2>
<fmt:formatDate value="${ now }" pattern="yyyy/MM/dd HH:mm:ss" />
</h2>
pom.xml
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.itwill</groupId>
<artifactId>post</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<description>JSP MVC example</description>
<dependencies>
<!-- Tomcat 10.1 WAS에서 JSTL을 사용하기 위해서. -->
<dependency>
<groupId>jakarta.servlet.jsp.jstl</groupId>
<artifactId>jakarta.servlet.jsp.jstl-api</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>org.glassfish.web</groupId>
<artifactId>jakarta.servlet.jsp.jstl</artifactId>
<version>3.0.1</version>
</dependency>
<!-- JUnit Test -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.9.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.9.3</version>
<scope>test</scope>
</dependency>
<!-- Oracle JDBC library -->
<dependency>
<groupId>com.oracle.database.jdbc</groupId>
<artifactId>ojdbc11</artifactId>
<version>23.2.0.0</version>
</dependency>
<!-- HikariCP - Connection Pool -->
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
<version>5.0.1</version>
</dependency>
<!-- Log4j - 로그 출력 라이브러리 -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.20.0</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.20.0</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<version>2.20.0</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.36</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<release>17</release>
</configuration>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.3</version>
</plugin>
</plugins>
</build>
</project>