Adding Code for BAEL-394 (#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
This commit is contained in:
buddhini81 2017-02-20 05:24:42 +05:30 committed by adamd1985
parent e5ecd86f27
commit 7c337eb6c0
13 changed files with 265 additions and 0 deletions

View File

@ -0,0 +1,57 @@
package com.baeldung.javaeeannotations;
import java.io.IOException;
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;
import javax.servlet.http.HttpServletResponse;
@WebServlet(
name = "BankAccountServlet",
description = "Represents a Bank Account and it's transactions",
urlPatterns = {"/account", "/bankAccount" },
initParams = { @WebInitParam(name = "type", value = "savings") }
)
@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();
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
double accountBalance = 1000d;
double interestRate = Double.parseDouble(request.getAttribute("interest").toString());
String paramDepositAmt = request.getParameter("dep");
double depositAmt = Double.parseDouble(paramDepositAmt);
accountBalance = accountBalance + depositAmt;
PrintWriter writer = response.getWriter();
writer.println("<html> Balance of " + accountType + " account is: " +
accountBalance + "<br> This account bares an interest rate of " + interestRate +
" % </html>");
writer.flush();
}
}

View File

@ -0,0 +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");
}
}

View File

@ -0,0 +1,36 @@
package com.baeldung.javaeeannotations;
import java.io.IOException;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebFilter(
urlPatterns = "/bankAccount/*",
filterName = "LoggingFilter",
description = "Filter all account transaction URLs"
)
public class LoggingFilter implements javax.servlet.Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
HttpServletRequest req = (HttpServletRequest) request;
HttpServletResponse res = (HttpServletResponse) response;
res.sendRedirect(req.getContextPath() + "/login.jsp");
chain.doFilter(request, response);
}
@Override
public void destroy() {
}
}

View File

@ -0,0 +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");
}
}
}

View File

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

View File

@ -0,0 +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"/>
&nbsp;&nbsp;
<input type="submit" value="Deposit" />
</form>
</body>
</html>

View File

@ -0,0 +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>
</html>

View File

@ -0,0 +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>
</html>

View File

@ -0,0 +1,20 @@
package com.baeldung;
public class Ebook {
private int bookId;
private String bookTitle;
public int getBookId() {
return bookId;
}
public void setBookId(int bookId) {
this.bookId = bookId;
}
public String getBookTitle() {
return bookTitle;
}
public void setBookTitle(String bookTitle) {
this.bookTitle = bookTitle;
}
}

View File

@ -0,0 +1,5 @@
package com.baeldung;
public interface EbookRepository {
String titleById(int id);
}

View File

@ -0,0 +1,12 @@
package com.baeldung;
import org.springframework.beans.factory.annotation.Autowired;
public class LibraryUtils {
@Autowired
private EbookRepository eBookRepository;
public String findBook(int id) {
return eBookRepository.titleById(id);
}
}

View File

@ -0,0 +1,20 @@
package com.baeldung;
public class Member {
private int memberId;
private String memberName;
public int getMemberId() {
return memberId;
}
public void setMemberId(int memberId) {
this.memberId = memberId;
}
public String getMemberName() {
return memberName;
}
public void setMemberName(String memberName) {
this.memberName = memberName;
}
}

View File

@ -0,0 +1,14 @@
package com.baeldung;
import org.springframework.beans.factory.annotation.Autowired;
public class Reservation {
private Member member;
private Ebook eBook;
@Autowired
public Reservation(Member member, Ebook eBook) {
this.member = member;
this.eBook = eBook;
}
}