buddhini81 7c337eb6c0 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
2017-02-20 00:54:42 +01:00

58 lines
2.1 KiB
Java

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();
}
}