Skip to content

Commit e62f8fa

Browse files
buddhini81adamd1985
authored andcommitted
Add javaEE annotation sample project (eugenp#1481)
* Delete AccountServlet.java * Delete BankAppServletContextListener.java * Delete LogInFilter.java * Delete UploadCustomerDocumentsServlet.java * Delete index.jsp * Delete login.jsp * Delete upload.jsp * Delete web.xml * Create javaeeannotations * Delete javaeeannotations * commit javaEE annotations project
1 parent 9b48f77 commit e62f8fa

10 files changed

Lines changed: 183 additions & 109 deletions

File tree

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<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">
2+
<modelVersion>4.0.0</modelVersion>
3+
<groupId>com.baeldung.javaeeannotations</groupId>
4+
<artifactId>JavaEEAnnotationsSample</artifactId>
5+
<version>0.0.1-SNAPSHOT</version>
6+
<packaging>war</packaging>
7+
<name>JavaEEAnnotationsSample</name>
8+
<description>JavaEEAnnotationsSample</description>
9+
10+
<dependencies>
11+
12+
<dependency>
13+
<groupId>javax.annotation</groupId>
14+
<artifactId>javax.annotation-api</artifactId>
15+
<version>1.3</version>
16+
</dependency>
17+
18+
<dependency>
19+
<groupId>javax.servlet</groupId>
20+
<artifactId>javax.servlet-api</artifactId>
21+
<version>3.1.0</version>
22+
</dependency>
23+
24+
<dependency>
25+
<groupId>javax.servlet.jsp</groupId>
26+
<artifactId>jsp-api</artifactId>
27+
<version>2.1</version>
28+
</dependency>
29+
30+
</dependencies>
31+
<build>
32+
<pluginManagement>
33+
<plugins>
34+
<plugin>
35+
<groupId>org.apache.maven.plugins</groupId>
36+
<artifactId>maven-compiler-plugin</artifactId>
37+
<version>3.2</version>
38+
<configuration>
39+
<source>1.7</source>
40+
<target>1.7</target>
41+
</configuration>
42+
</plugin>
43+
<plugin>
44+
<groupId>org.apache.maven.plugins</groupId>
45+
<artifactId>maven-war-plugin</artifactId>
46+
<version>2.4</version>
47+
<configuration>
48+
<warSourceDirectory>src/main/webapp</warSourceDirectory>
49+
<warName>SpringFieldConstructorInjection</warName>
50+
<failOnMissingWebXml>false</failOnMissingWebXml>
51+
</configuration>
52+
</plugin>
53+
</plugins>
54+
</pluginManagement>
55+
<finalName>JavaEEAnnotationsSample</finalName>
56+
</build>
57+
</project>

jee7/src/main/java/com/baeldung/javaeeannotations/AccountServlet.java renamed to jee7/src/main/java/com/baeldung/javaeeannotations/JavaEEAnnotationsSample/src/main/java/com/baeldung/javaeeannotations/AccountServlet.java

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,6 @@
55

66
import javax.servlet.ServletConfig;
77
import javax.servlet.ServletException;
8-
import javax.servlet.annotation.HttpConstraint;
9-
import javax.servlet.annotation.HttpMethodConstraint;
10-
import javax.servlet.annotation.ServletSecurity;
118
import javax.servlet.annotation.WebInitParam;
129
import javax.servlet.annotation.WebServlet;
1310
import javax.servlet.http.HttpServletRequest;
@@ -19,20 +16,20 @@
1916
urlPatterns = {"/account", "/bankAccount" },
2017
initParams = { @WebInitParam(name = "type", value = "savings") }
2118
)
22-
@ServletSecurity(
19+
/*@ServletSecurity(
2320
value = @HttpConstraint(rolesAllowed = {"admin"}),
2421
httpMethodConstraints = {@HttpMethodConstraint(value = "POST", rolesAllowed = {"admin"})}
25-
)
22+
)*/
2623
public class AccountServlet extends javax.servlet.http.HttpServlet {
2724

2825
String accountType = null;
2926

30-
@Override
3127
public void init(ServletConfig config) throws ServletException {
3228
accountType = config.getInitParameter("type");
3329
}
3430

3531
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
32+
3633
PrintWriter writer = response.getWriter();
3734
writer.println("<html>Hello, I am an AccountServlet!</html>");
3835
writer.flush();
@@ -49,8 +46,8 @@ public void doPost(HttpServletRequest request, HttpServletResponse response) thr
4946

5047
PrintWriter writer = response.getWriter();
5148
writer.println("<html> Balance of " + accountType + " account is: " +
52-
accountBalance + "<br> This account bares an interest rate of " + interestRate +
53-
" % </html>");
49+
accountBalance + "<br> This account bares an interest rate of " + interestRate +
50+
" % </html>");
5451
writer.flush();
5552

5653
}

jee7/src/main/java/com/baeldung/javaeeannotations/BankAppServletContextListener.java renamed to jee7/src/main/java/com/baeldung/javaeeannotations/JavaEEAnnotationsSample/src/main/java/com/baeldung/javaeeannotations/BankAppServletContextListener.java

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1-
package com.baeldung.javaeeannotations;
2-
3-
import javax.servlet.ServletContextEvent;
4-
import javax.servlet.ServletContextListener;
5-
import javax.servlet.annotation.WebListener;
6-
7-
@WebListener
8-
public class BankAppServletContextListener implements ServletContextListener {
9-
10-
public void contextInitialized(ServletContextEvent sce) {
11-
sce.getServletContext().setAttribute("ATTR_DEFAULT_LANGUAGE", "english");
12-
}
13-
14-
public void contextDestroyed(ServletContextEvent sce) {
15-
System.out.println("CONTEXT DESTROYED");
16-
}
17-
}
1+
package com.baeldung.javaeeannotations;
2+
3+
import javax.servlet.ServletContextEvent;
4+
import javax.servlet.ServletContextListener;
5+
import javax.servlet.annotation.WebListener;
6+
7+
@WebListener
8+
public class BankAppServletContextListener implements ServletContextListener {
9+
10+
public void contextInitialized(ServletContextEvent sce) {
11+
sce.getServletContext().setAttribute("ATTR_DEFAULT_LANGUAGE", "english");
12+
}
13+
14+
public void contextDestroyed(ServletContextEvent sce) {
15+
System.out.println("CONTEXT DESTROYED");
16+
}
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.baeldung.javaeeannotations;
2+
3+
import javax.servlet.ServletRequestEvent;
4+
import javax.servlet.ServletRequestListener;
5+
import javax.servlet.annotation.WebListener;
6+
import javax.servlet.http.HttpServletRequest;
7+
8+
@WebListener
9+
public class DepositRequestListener implements ServletRequestListener {
10+
11+
public void requestDestroyed(ServletRequestEvent event) {
12+
13+
}
14+
15+
public void requestInitialized(ServletRequestEvent evet) {
16+
HttpServletRequest req = (HttpServletRequest)evet.getServletRequest();
17+
req.setAttribute("interest", new Double(10));
18+
}
19+
20+
}

jee7/src/main/java/com/baeldung/javaeeannotations/LogInFilter.java renamed to jee7/src/main/java/com/baeldung/javaeeannotations/JavaEEAnnotationsSample/src/main/java/com/baeldung/javaeeannotations/LogInFilter.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@
1111
import javax.servlet.http.HttpServletRequest;
1212
import javax.servlet.http.HttpServletResponse;
1313

14-
@WebFilter(
14+
/*@WebFilter(
1515
urlPatterns = "/bankAccount/*",
1616
filterName = "LogInFilter",
1717
description = "Filter all account transaction URLs"
18-
)
18+
)*/
1919
public class LogInFilter implements javax.servlet.Filter {
20-
@Override
20+
2121
public void init(FilterConfig filterConfig) throws ServletException {
2222
}
2323

@@ -29,8 +29,8 @@ public void doFilter(ServletRequest request, ServletResponse response, FilterCha
2929
chain.doFilter(request, response);
3030
}
3131

32-
@Override
3332
public void destroy() {
33+
3434
}
3535

3636
}

jee7/src/main/java/com/baeldung/javaeeannotations/UploadCustomerDocumentsServlet.java renamed to jee7/src/main/java/com/baeldung/javaeeannotations/JavaEEAnnotationsSample/src/main/java/com/baeldung/javaeeannotations/UploadCustomerDocumentsServlet.java

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,29 @@
1-
package com.baeldung.javaeeannotations;
2-
3-
import java.io.IOException;
4-
5-
import javax.servlet.ServletException;
6-
import javax.servlet.annotation.MultipartConfig;
7-
import javax.servlet.annotation.WebServlet;
8-
import javax.servlet.http.HttpServlet;
9-
import javax.servlet.http.HttpServletRequest;
10-
import javax.servlet.http.HttpServletResponse;
11-
import javax.servlet.http.Part;
12-
13-
@WebServlet(urlPatterns = { "/uploadCustDocs" })
14-
@MultipartConfig(
15-
fileSizeThreshold = 1024 * 1024 * 20,
16-
maxFileSize = 1024 * 1024 * 20,
17-
maxRequestSize = 1024 * 1024 * 25,
18-
location = "D:/custDocs"
19-
)
20-
public class UploadCustomerDocumentsServlet extends HttpServlet {
21-
22-
protected void doPost(
23-
HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
24-
for (Part part : request.getParts()) {
25-
part.write("myFile");
26-
}
27-
}
28-
29-
}
1+
package com.baeldung.javaeeannotations;
2+
3+
import java.io.IOException;
4+
5+
import javax.servlet.ServletException;
6+
import javax.servlet.annotation.MultipartConfig;
7+
import javax.servlet.annotation.WebServlet;
8+
import javax.servlet.http.HttpServlet;
9+
import javax.servlet.http.HttpServletRequest;
10+
import javax.servlet.http.HttpServletResponse;
11+
import javax.servlet.http.Part;
12+
13+
@WebServlet(urlPatterns = { "/uploadCustDocs" })
14+
@MultipartConfig(
15+
fileSizeThreshold = 1024 * 1024 * 20,
16+
maxFileSize = 1024 * 1024 * 20,
17+
maxRequestSize = 1024 * 1024 * 25,
18+
location = "D:/custDocs"
19+
)
20+
public class UploadCustomerDocumentsServlet extends HttpServlet {
21+
22+
protected void doPost(
23+
HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
24+
for (Part part : request.getParts()) {
25+
part.write("myFile");
26+
}
27+
}
28+
29+
}

jee7/src/main/webapp/index.jsp renamed to jee7/src/main/java/com/baeldung/javaeeannotations/JavaEEAnnotationsSample/src/main/webapp/index.jsp

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
2-
pageEncoding="ISO-8859-1"%>
3-
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
4-
<html>
5-
<head>
6-
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
7-
<title>My Account</title>
8-
</head>
9-
<body>
10-
<form action="account" method="post">
11-
Width: <input type="text" size="5" name="dep"/>
12-
&nbsp;&nbsp;
13-
<input type="submit" value="Deposit" />
14-
</form>
15-
</body>
1+
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
2+
pageEncoding="ISO-8859-1"%>
3+
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
4+
<html>
5+
<head>
6+
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
7+
<title>My Account</title>
8+
</head>
9+
<body>
10+
<form action="bankAccount" method="post">
11+
Amount: <input type="text" size="5" name="dep"/>
12+
&nbsp;&nbsp;
13+
<input type="submit" value="Deposit" />
14+
</form>
15+
</body>
1616
</html>

jee7/src/main/webapp/login.jsp renamed to jee7/src/main/java/com/baeldung/javaeeannotations/JavaEEAnnotationsSample/src/main/webapp/login.jsp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
2-
pageEncoding="ISO-8859-1"%>
3-
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
4-
<html>
5-
<head>
6-
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
7-
<title>Login</title>
8-
</head>
9-
<body>
10-
Login Here...
11-
</body>
1+
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
2+
pageEncoding="ISO-8859-1"%>
3+
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
4+
<html>
5+
<head>
6+
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
7+
<title>Login</title>
8+
</head>
9+
<body>
10+
Login Here...
11+
</body>
1212
</html>

jee7/src/main/webapp/upload.jsp renamed to jee7/src/main/java/com/baeldung/javaeeannotations/JavaEEAnnotationsSample/src/main/webapp/upload.jsp

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
2-
pageEncoding="ISO-8859-1"%>
3-
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
4-
<html>
5-
<head>
6-
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
7-
<title>Insert title here</title>
8-
</head>
9-
<body>
10-
<form action="uploadCustDocs" method="post" enctype="multipart/form-data">
11-
<input type="file" name="file" size="50" />
12-
<br />
13-
<input type="submit" value="Upload File" />
14-
</form>
15-
</body>
1+
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
2+
pageEncoding="ISO-8859-1"%>
3+
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
4+
<html>
5+
<head>
6+
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
7+
<title>Insert title here</title>
8+
</head>
9+
<body>
10+
<form action="uploadCustDocs" method="post" enctype="multipart/form-data">
11+
<input type="file" name="file" size="50" />
12+
<br />
13+
<input type="submit" value="Upload File" />
14+
</form>
15+
</body>
1616
</html>

jee7/src/main/webapp/WEB-INF/web.xml renamed to jee7/src/main/java/com/baeldung/javaeeannotations/JavaEEAnnotationsSample/src/main/webapp/web.xml

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
2-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3-
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
4-
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">
5-
6-
<login-config>
7-
<auth-method>BASIC</auth-method>
8-
<realm-name>default</realm-name>
9-
</login-config>
10-
1+
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
2+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
4+
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">
5+
6+
<login-config>
7+
<auth-method>BASIC</auth-method>
8+
<realm-name>default</realm-name>
9+
</login-config>
10+
1111
</web-app>

0 commit comments

Comments
 (0)