Skip to content

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

web-modules/jakarta-servlets-2/pom.xml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
<groupId>jakarta.servlet</groupId>
3232
<artifactId>jakarta.servlet-api</artifactId>
3333
<version>${jakarta.servlet-api.version}</version>
34+
<scope>provided</scope>
3435
</dependency>
3536
<dependency>
3637
<groupId>jakarta.servlet.jsp</groupId>
@@ -89,6 +90,14 @@
8990
</webApp>
9091
</configuration>
9192
</plugin>
93+
<plugin>
94+
<groupId>org.apache.maven.plugins</groupId>
95+
<artifactId>maven-compiler-plugin</artifactId>
96+
<configuration>
97+
<source>21</source>
98+
<target>21</target>
99+
</configuration>
100+
</plugin>
92101
</plugins>
93102
</build>
94103

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.baeldung.jakartaeetomcat;
2+
3+
import java.io.IOException;
4+
import java.io.PrintWriter;
5+
import java.time.LocalDateTime;
6+
7+
import jakarta.servlet.ServletException;
8+
import jakarta.servlet.annotation.WebServlet;
9+
import jakarta.servlet.http.HttpServlet;
10+
import jakarta.servlet.http.HttpServletRequest;
11+
import jakarta.servlet.http.HttpServletResponse;
12+
13+
@WebServlet(name = "CurrentDateAndTime", urlPatterns = { "/date-time" })
14+
public class CurrentDateAndTime extends HttpServlet {
15+
16+
LocalDateTime currentDate = LocalDateTime.now();
17+
18+
protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
19+
response.setContentType("text/html;charset=UTF-8");
20+
currentDate = LocalDateTime.now();
21+
22+
try (PrintWriter out = response.getWriter()) {
23+
out.printf("""
24+
<html>
25+
<head> <title> Current Date And Time </title> </head>
26+
<body> <h2> Servlet current date and time at %s </h2> <br/> Date and Time %s </body>
27+
</html>
28+
""", request.getContextPath(), currentDate);
29+
}
30+
}
31+
32+
@Override
33+
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
34+
processRequest(request, response);
35+
}
36+
37+
38+
39+
}
40+

0 commit comments

Comments
 (0)