2018-09-30 19:31:56 +05:30
|
|
|
package com.baeldung.javaeeannotations;
|
2017-02-20 05:24:42 +05:30
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.PrintWriter;
|
|
|
|
|
|
|
|
import javax.servlet.ServletConfig;
|
|
|
|
import javax.servlet.ServletException;
|
|
|
|
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") }
|
|
|
|
)
|
2017-03-26 00:20:46 +05:30
|
|
|
/*@ServletSecurity(
|
2017-04-12 13:26:08 +05:30
|
|
|
value = @HttpConstraint(rolesAllowed = {"Member"}),
|
|
|
|
httpMethodConstraints = {@HttpMethodConstraint(value = "POST", rolesAllowed = {"Admin"})}
|
2017-03-26 00:20:46 +05:30
|
|
|
)*/
|
2017-02-20 05:24:42 +05:30
|
|
|
public class AccountServlet extends javax.servlet.http.HttpServlet {
|
|
|
|
|
|
|
|
String accountType = null;
|
|
|
|
|
|
|
|
public void init(ServletConfig config) throws ServletException {
|
|
|
|
accountType = config.getInitParameter("type");
|
|
|
|
}
|
|
|
|
|
|
|
|
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
|
2017-03-26 00:20:46 +05:30
|
|
|
|
2017-02-20 05:24:42 +05:30
|
|
|
PrintWriter writer = response.getWriter();
|
|
|
|
writer.println("<html>Hello, I am an AccountServlet!</html>");
|
|
|
|
writer.flush();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
|
2017-04-03 02:04:46 +05:30
|
|
|
|
2017-02-20 05:24:42 +05:30
|
|
|
double accountBalance = 1000d;
|
|
|
|
String paramDepositAmt = request.getParameter("dep");
|
|
|
|
double depositAmt = Double.parseDouble(paramDepositAmt);
|
|
|
|
|
|
|
|
accountBalance = accountBalance + depositAmt;
|
2017-04-03 02:04:46 +05:30
|
|
|
|
2017-02-20 05:24:42 +05:30
|
|
|
PrintWriter writer = response.getWriter();
|
2017-04-03 02:04:46 +05:30
|
|
|
writer.println("<html> Balance of " + accountType + " account is: " + accountBalance + "</html>");
|
2017-02-20 05:24:42 +05:30
|
|
|
writer.flush();
|
|
|
|
}
|
|
|
|
}
|