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:
parent
9b48f77c8d
commit
e62f8fa3d8
@ -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>
|
@ -5,9 +5,6 @@ import java.io.PrintWriter;
|
||||
|
||||
import javax.servlet.ServletConfig;
|
||||
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.WebServlet;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
@ -19,20 +16,20 @@ import javax.servlet.http.HttpServletResponse;
|
||||
urlPatterns = {"/account", "/bankAccount" },
|
||||
initParams = { @WebInitParam(name = "type", value = "savings") }
|
||||
)
|
||||
@ServletSecurity(
|
||||
/*@ServletSecurity(
|
||||
value = @HttpConstraint(rolesAllowed = {"admin"}),
|
||||
httpMethodConstraints = {@HttpMethodConstraint(value = "POST", rolesAllowed = {"admin"})}
|
||||
)
|
||||
)*/
|
||||
public class AccountServlet extends javax.servlet.http.HttpServlet {
|
||||
|
||||
String accountType = null;
|
||||
|
||||
@Override
|
||||
public void init(ServletConfig config) throws ServletException {
|
||||
accountType = config.getInitParameter("type");
|
||||
}
|
||||
|
||||
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
|
||||
|
||||
PrintWriter writer = response.getWriter();
|
||||
writer.println("<html>Hello, I am an AccountServlet!</html>");
|
||||
writer.flush();
|
||||
@ -49,8 +46,8 @@ public class AccountServlet extends javax.servlet.http.HttpServlet {
|
||||
|
||||
PrintWriter writer = response.getWriter();
|
||||
writer.println("<html> Balance of " + accountType + " account is: " +
|
||||
accountBalance + "<br> This account bares an interest rate of " + interestRate +
|
||||
" % </html>");
|
||||
accountBalance + "<br> This account bares an interest rate of " + interestRate +
|
||||
" % </html>");
|
||||
writer.flush();
|
||||
|
||||
}
|
@ -1,17 +1,17 @@
|
||||
package com.baeldung.javaeeannotations;
|
||||
|
||||
import javax.servlet.ServletContextEvent;
|
||||
import javax.servlet.ServletContextListener;
|
||||
import javax.servlet.annotation.WebListener;
|
||||
|
||||
@WebListener
|
||||
public class BankAppServletContextListener implements ServletContextListener {
|
||||
|
||||
public void contextInitialized(ServletContextEvent sce) {
|
||||
sce.getServletContext().setAttribute("ATTR_DEFAULT_LANGUAGE", "english");
|
||||
}
|
||||
|
||||
public void contextDestroyed(ServletContextEvent sce) {
|
||||
System.out.println("CONTEXT DESTROYED");
|
||||
}
|
||||
}
|
||||
package com.baeldung.javaeeannotations;
|
||||
|
||||
import javax.servlet.ServletContextEvent;
|
||||
import javax.servlet.ServletContextListener;
|
||||
import javax.servlet.annotation.WebListener;
|
||||
|
||||
@WebListener
|
||||
public class BankAppServletContextListener implements ServletContextListener {
|
||||
|
||||
public void contextInitialized(ServletContextEvent sce) {
|
||||
sce.getServletContext().setAttribute("ATTR_DEFAULT_LANGUAGE", "english");
|
||||
}
|
||||
|
||||
public void contextDestroyed(ServletContextEvent sce) {
|
||||
System.out.println("CONTEXT DESTROYED");
|
||||
}
|
||||
}
|
@ -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));
|
||||
}
|
||||
|
||||
}
|
@ -11,13 +11,13 @@ import javax.servlet.annotation.WebFilter;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
@WebFilter(
|
||||
/*@WebFilter(
|
||||
urlPatterns = "/bankAccount/*",
|
||||
filterName = "LogInFilter",
|
||||
description = "Filter all account transaction URLs"
|
||||
)
|
||||
)*/
|
||||
public class LogInFilter implements javax.servlet.Filter {
|
||||
@Override
|
||||
|
||||
public void init(FilterConfig filterConfig) throws ServletException {
|
||||
}
|
||||
|
||||
@ -29,8 +29,8 @@ public class LogInFilter implements javax.servlet.Filter {
|
||||
chain.doFilter(request, response);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void destroy() {
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -1,29 +1,29 @@
|
||||
package com.baeldung.javaeeannotations;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.annotation.MultipartConfig;
|
||||
import javax.servlet.annotation.WebServlet;
|
||||
import javax.servlet.http.HttpServlet;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import javax.servlet.http.Part;
|
||||
|
||||
@WebServlet(urlPatterns = { "/uploadCustDocs" })
|
||||
@MultipartConfig(
|
||||
fileSizeThreshold = 1024 * 1024 * 20,
|
||||
maxFileSize = 1024 * 1024 * 20,
|
||||
maxRequestSize = 1024 * 1024 * 25,
|
||||
location = "D:/custDocs"
|
||||
)
|
||||
public class UploadCustomerDocumentsServlet extends HttpServlet {
|
||||
|
||||
protected void doPost(
|
||||
HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
|
||||
for (Part part : request.getParts()) {
|
||||
part.write("myFile");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
package com.baeldung.javaeeannotations;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.annotation.MultipartConfig;
|
||||
import javax.servlet.annotation.WebServlet;
|
||||
import javax.servlet.http.HttpServlet;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import javax.servlet.http.Part;
|
||||
|
||||
@WebServlet(urlPatterns = { "/uploadCustDocs" })
|
||||
@MultipartConfig(
|
||||
fileSizeThreshold = 1024 * 1024 * 20,
|
||||
maxFileSize = 1024 * 1024 * 20,
|
||||
maxRequestSize = 1024 * 1024 * 25,
|
||||
location = "D:/custDocs"
|
||||
)
|
||||
public class UploadCustomerDocumentsServlet extends HttpServlet {
|
||||
|
||||
protected void doPost(
|
||||
HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
|
||||
for (Part part : request.getParts()) {
|
||||
part.write("myFile");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -1,16 +1,16 @@
|
||||
<%@ page language="java" contentType="text/html; charset=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">
|
||||
<html>
|
||||
<head>
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
|
||||
<title>My Account</title>
|
||||
</head>
|
||||
<body>
|
||||
<form action="account" method="post">
|
||||
Width: <input type="text" size="5" name="dep"/>
|
||||
|
||||
<input type="submit" value="Deposit" />
|
||||
</form>
|
||||
</body>
|
||||
<%@ page language="java" contentType="text/html; charset=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">
|
||||
<html>
|
||||
<head>
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
|
||||
<title>My Account</title>
|
||||
</head>
|
||||
<body>
|
||||
<form action="bankAccount" method="post">
|
||||
Amount: <input type="text" size="5" name="dep"/>
|
||||
|
||||
<input type="submit" value="Deposit" />
|
||||
</form>
|
||||
</body>
|
||||
</html>
|
@ -1,12 +1,12 @@
|
||||
<%@ page language="java" contentType="text/html; charset=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">
|
||||
<html>
|
||||
<head>
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
|
||||
<title>Login</title>
|
||||
</head>
|
||||
<body>
|
||||
Login Here...
|
||||
</body>
|
||||
<%@ page language="java" contentType="text/html; charset=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">
|
||||
<html>
|
||||
<head>
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
|
||||
<title>Login</title>
|
||||
</head>
|
||||
<body>
|
||||
Login Here...
|
||||
</body>
|
||||
</html>
|
@ -1,16 +1,16 @@
|
||||
<%@ page language="java" contentType="text/html; charset=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">
|
||||
<html>
|
||||
<head>
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
|
||||
<title>Insert title here</title>
|
||||
</head>
|
||||
<body>
|
||||
<form action="uploadCustDocs" method="post" enctype="multipart/form-data">
|
||||
<input type="file" name="file" size="50" />
|
||||
<br />
|
||||
<input type="submit" value="Upload File" />
|
||||
</form>
|
||||
</body>
|
||||
<%@ page language="java" contentType="text/html; charset=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">
|
||||
<html>
|
||||
<head>
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
|
||||
<title>Insert title here</title>
|
||||
</head>
|
||||
<body>
|
||||
<form action="uploadCustDocs" method="post" enctype="multipart/form-data">
|
||||
<input type="file" name="file" size="50" />
|
||||
<br />
|
||||
<input type="submit" value="Upload File" />
|
||||
</form>
|
||||
</body>
|
||||
</html>
|
@ -1,11 +1,11 @@
|
||||
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
|
||||
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">
|
||||
|
||||
<login-config>
|
||||
<auth-method>BASIC</auth-method>
|
||||
<realm-name>default</realm-name>
|
||||
</login-config>
|
||||
|
||||
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
|
||||
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">
|
||||
|
||||
<login-config>
|
||||
<auth-method>BASIC</auth-method>
|
||||
<realm-name>default</realm-name>
|
||||
</login-config>
|
||||
|
||||
</web-app>
|
Loading…
x
Reference in New Issue
Block a user