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

@ -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

@ -7,8 +7,8 @@
<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>