Skip to content

Commit 7c337eb

Browse files
buddhini81adamd1985
authored andcommitted
Adding Code for BAEL-394 (eugenp#1163)
* Code for evaluation article Article : Field vs. Constructor Injection * Correct typo in attribute name * Delete EbookRepositiry.java * Add corrected class * Update LibraryUtils.java * Update Member.java * Update Reservation.java * Adding new file AccountServlet * Adding new files for BAEL-394 * Add new files for BAEL-394 * Add new file for BAEL-394 * Indentation of annotations fixed * Indentation of annotations fixed * Indentation of annotations fixed * Removing this class since it is not relevant * New example added for @weblistener
1 parent e5ecd86 commit 7c337eb

13 files changed

Lines changed: 265 additions & 0 deletions

File tree

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.baeldung.javaeeannotations;
2+
3+
import java.io.IOException;
4+
import java.io.PrintWriter;
5+
6+
import javax.servlet.ServletConfig;
7+
import javax.servlet.ServletException;
8+
import javax.servlet.annotation.HttpConstraint;
9+
import javax.servlet.annotation.HttpMethodConstraint;
10+
import javax.servlet.annotation.ServletSecurity;
11+
import javax.servlet.annotation.WebInitParam;
12+
import javax.servlet.annotation.WebServlet;
13+
import javax.servlet.http.HttpServletRequest;
14+
import javax.servlet.http.HttpServletResponse;
15+
16+
@WebServlet(
17+
name = "BankAccountServlet",
18+
description = "Represents a Bank Account and it's transactions",
19+
urlPatterns = {"/account", "/bankAccount" },
20+
initParams = { @WebInitParam(name = "type", value = "savings") }
21+
)
22+
@ServletSecurity(
23+
value = @HttpConstraint(rolesAllowed = {"admin"}),
24+
httpMethodConstraints = {@HttpMethodConstraint(value = "POST", rolesAllowed = {"admin"})}
25+
)
26+
public class AccountServlet extends javax.servlet.http.HttpServlet {
27+
28+
String accountType = null;
29+
30+
@Override
31+
public void init(ServletConfig config) throws ServletException {
32+
accountType = config.getInitParameter("type");
33+
}
34+
35+
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
36+
PrintWriter writer = response.getWriter();
37+
writer.println("<html>Hello, I am an AccountServlet!</html>");
38+
writer.flush();
39+
}
40+
41+
public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
42+
double accountBalance = 1000d;
43+
double interestRate = Double.parseDouble(request.getAttribute("interest").toString());
44+
45+
String paramDepositAmt = request.getParameter("dep");
46+
double depositAmt = Double.parseDouble(paramDepositAmt);
47+
48+
accountBalance = accountBalance + depositAmt;
49+
50+
PrintWriter writer = response.getWriter();
51+
writer.println("<html> Balance of " + accountType + " account is: " +
52+
accountBalance + "<br> This account bares an interest rate of " + interestRate +
53+
" % </html>");
54+
writer.flush();
55+
56+
}
57+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +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+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.baeldung.javaeeannotations;
2+
3+
import java.io.IOException;
4+
5+
import javax.servlet.FilterChain;
6+
import javax.servlet.FilterConfig;
7+
import javax.servlet.ServletException;
8+
import javax.servlet.ServletRequest;
9+
import javax.servlet.ServletResponse;
10+
import javax.servlet.annotation.WebFilter;
11+
import javax.servlet.http.HttpServletRequest;
12+
import javax.servlet.http.HttpServletResponse;
13+
14+
@WebFilter(
15+
urlPatterns = "/bankAccount/*",
16+
filterName = "LoggingFilter",
17+
description = "Filter all account transaction URLs"
18+
)
19+
public class LoggingFilter implements javax.servlet.Filter {
20+
@Override
21+
public void init(FilterConfig filterConfig) throws ServletException {
22+
}
23+
24+
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
25+
HttpServletRequest req = (HttpServletRequest) request;
26+
HttpServletResponse res = (HttpServletResponse) response;
27+
28+
res.sendRedirect(req.getContextPath() + "/login.jsp");
29+
chain.doFilter(request, response);
30+
}
31+
32+
@Override
33+
public void destroy() {
34+
}
35+
36+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +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+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +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+
11+
</web-app>

‎jee7/src/main/webapp/index.jsp‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +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>
16+
</html>

‎jee7/src/main/webapp/login.jsp‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +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>
12+
</html>

‎jee7/src/main/webapp/upload.jsp‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +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>
16+
</html>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.baeldung;
2+
3+
public class Ebook {
4+
5+
private int bookId;
6+
private String bookTitle;
7+
8+
public int getBookId() {
9+
return bookId;
10+
}
11+
public void setBookId(int bookId) {
12+
this.bookId = bookId;
13+
}
14+
public String getBookTitle() {
15+
return bookTitle;
16+
}
17+
public void setBookTitle(String bookTitle) {
18+
this.bookTitle = bookTitle;
19+
}
20+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.baeldung;
2+
3+
public interface EbookRepository {
4+
String titleById(int id);
5+
}

0 commit comments

Comments
 (0)