Add javaEE annotation sample project (#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
This commit is contained in:
buddhini81 2017-03-26 00:20:46 +05:30 committed by adamd1985
parent 9b48f77c8d
commit e62f8fa3d8
10 changed files with 183 additions and 109 deletions

View File

@ -0,0 +1,57 @@
<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>com.baeldung.javaeeannotations</groupId>
<artifactId>JavaEEAnnotationsSample</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>JavaEEAnnotationsSample</name>
<description>JavaEEAnnotationsSample</description>
<dependencies>
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>javax.annotation-api</artifactId>
<version>1.3</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.1</version>
</dependency>
</dependencies>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.2</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.4</version>
<configuration>
<warSourceDirectory>src/main/webapp</warSourceDirectory>
<warName>SpringFieldConstructorInjection</warName>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>
</pluginManagement>
<finalName>JavaEEAnnotationsSample</finalName>
</build>
</project>

View File

@ -5,9 +5,6 @@ import java.io.PrintWriter;
import javax.servlet.ServletConfig; import javax.servlet.ServletConfig;
import javax.servlet.ServletException; import javax.servlet.ServletException;
import javax.servlet.annotation.HttpConstraint;
import javax.servlet.annotation.HttpMethodConstraint;
import javax.servlet.annotation.ServletSecurity;
import javax.servlet.annotation.WebInitParam; import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet; import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
@ -19,20 +16,20 @@ import javax.servlet.http.HttpServletResponse;
urlPatterns = {"/account", "/bankAccount" }, urlPatterns = {"/account", "/bankAccount" },
initParams = { @WebInitParam(name = "type", value = "savings") } initParams = { @WebInitParam(name = "type", value = "savings") }
) )
@ServletSecurity( /*@ServletSecurity(
value = @HttpConstraint(rolesAllowed = {"admin"}), value = @HttpConstraint(rolesAllowed = {"admin"}),
httpMethodConstraints = {@HttpMethodConstraint(value = "POST", rolesAllowed = {"admin"})} httpMethodConstraints = {@HttpMethodConstraint(value = "POST", rolesAllowed = {"admin"})}
) )*/
public class AccountServlet extends javax.servlet.http.HttpServlet { public class AccountServlet extends javax.servlet.http.HttpServlet {
String accountType = null; String accountType = null;
@Override
public void init(ServletConfig config) throws ServletException { public void init(ServletConfig config) throws ServletException {
accountType = config.getInitParameter("type"); accountType = config.getInitParameter("type");
} }
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException { public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
PrintWriter writer = response.getWriter(); PrintWriter writer = response.getWriter();
writer.println("<html>Hello, I am an AccountServlet!</html>"); writer.println("<html>Hello, I am an AccountServlet!</html>");
writer.flush(); writer.flush();
@ -49,8 +46,8 @@ public class AccountServlet extends javax.servlet.http.HttpServlet {
PrintWriter writer = response.getWriter(); PrintWriter writer = response.getWriter();
writer.println("<html> Balance of " + accountType + " account is: " + writer.println("<html> Balance of " + accountType + " account is: " +
accountBalance + "<br> This account bares an interest rate of " + interestRate + accountBalance + "<br> This account bares an interest rate of " + interestRate +
" % </html>"); " % </html>");
writer.flush(); writer.flush();
} }

View File

@ -1,17 +1,17 @@
package com.baeldung.javaeeannotations; package com.baeldung.javaeeannotations;
import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener; import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener; import javax.servlet.annotation.WebListener;
@WebListener @WebListener
public class BankAppServletContextListener implements ServletContextListener { public class BankAppServletContextListener implements ServletContextListener {
public void contextInitialized(ServletContextEvent sce) { public void contextInitialized(ServletContextEvent sce) {
sce.getServletContext().setAttribute("ATTR_DEFAULT_LANGUAGE", "english"); sce.getServletContext().setAttribute("ATTR_DEFAULT_LANGUAGE", "english");
} }
public void contextDestroyed(ServletContextEvent sce) { public void contextDestroyed(ServletContextEvent sce) {
System.out.println("CONTEXT DESTROYED"); System.out.println("CONTEXT DESTROYED");
} }
} }

View File

@ -0,0 +1,20 @@
package com.baeldung.javaeeannotations;
import javax.servlet.ServletRequestEvent;
import javax.servlet.ServletRequestListener;
import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpServletRequest;
@WebListener
public class DepositRequestListener implements ServletRequestListener {
public void requestDestroyed(ServletRequestEvent event) {
}
public void requestInitialized(ServletRequestEvent evet) {
HttpServletRequest req = (HttpServletRequest)evet.getServletRequest();
req.setAttribute("interest", new Double(10));
}
}

View File

@ -11,13 +11,13 @@ import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
@WebFilter( /*@WebFilter(
urlPatterns = "/bankAccount/*", urlPatterns = "/bankAccount/*",
filterName = "LogInFilter", filterName = "LogInFilter",
description = "Filter all account transaction URLs" description = "Filter all account transaction URLs"
) )*/
public class LogInFilter implements javax.servlet.Filter { public class LogInFilter implements javax.servlet.Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException { public void init(FilterConfig filterConfig) throws ServletException {
} }
@ -29,8 +29,8 @@ public class LogInFilter implements javax.servlet.Filter {
chain.doFilter(request, response); chain.doFilter(request, response);
} }
@Override
public void destroy() { public void destroy() {
} }
} }

View File

@ -1,29 +1,29 @@
package com.baeldung.javaeeannotations; package com.baeldung.javaeeannotations;
import java.io.IOException; import java.io.IOException;
import javax.servlet.ServletException; import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig; import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet; import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part; import javax.servlet.http.Part;
@WebServlet(urlPatterns = { "/uploadCustDocs" }) @WebServlet(urlPatterns = { "/uploadCustDocs" })
@MultipartConfig( @MultipartConfig(
fileSizeThreshold = 1024 * 1024 * 20, fileSizeThreshold = 1024 * 1024 * 20,
maxFileSize = 1024 * 1024 * 20, maxFileSize = 1024 * 1024 * 20,
maxRequestSize = 1024 * 1024 * 25, maxRequestSize = 1024 * 1024 * 25,
location = "D:/custDocs" location = "D:/custDocs"
) )
public class UploadCustomerDocumentsServlet extends HttpServlet { public class UploadCustomerDocumentsServlet extends HttpServlet {
protected void doPost( protected void doPost(
HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
for (Part part : request.getParts()) { for (Part part : request.getParts()) {
part.write("myFile"); part.write("myFile");
} }
} }
} }

View File

@ -1,16 +1,16 @@
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%> pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html> <html>
<head> <head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>My Account</title> <title>My Account</title>
</head> </head>
<body> <body>
<form action="account" method="post"> <form action="bankAccount" method="post">
Width: <input type="text" size="5" name="dep"/> Amount: <input type="text" size="5" name="dep"/>
&nbsp;&nbsp; &nbsp;&nbsp;
<input type="submit" value="Deposit" /> <input type="submit" value="Deposit" />
</form> </form>
</body> </body>
</html> </html>

View File

@ -1,12 +1,12 @@
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%> pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html> <html>
<head> <head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Login</title> <title>Login</title>
</head> </head>
<body> <body>
Login Here... Login Here...
</body> </body>
</html> </html>

View File

@ -1,16 +1,16 @@
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%> pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html> <html>
<head> <head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title> <title>Insert title here</title>
</head> </head>
<body> <body>
<form action="uploadCustDocs" method="post" enctype="multipart/form-data"> <form action="uploadCustDocs" method="post" enctype="multipart/form-data">
<input type="file" name="file" size="50" /> <input type="file" name="file" size="50" />
<br /> <br />
<input type="submit" value="Upload File" /> <input type="submit" value="Upload File" />
</form> </form>
</body> </body>
</html> </html>

View File

@ -1,11 +1,11 @@
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"> http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">
<login-config> <login-config>
<auth-method>BASIC</auth-method> <auth-method>BASIC</auth-method>
<realm-name>default</realm-name> <realm-name>default</realm-name>
</login-config> </login-config>
</web-app> </web-app>