package com.example.spring.bank.web; import java.io.IOException; import java.math.BigDecimal; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.web.context.WebApplicationContext; import org.springframework.web.context.support.WebApplicationContextUtils; import com.example.spring.bank.AccountManager; /** * Servlet implementation class BankGateway */ public class BankGateway extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public BankGateway() { super(); } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { getServletContext().getRequestDispatcher("/deposit.jsp").forward(request, response); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(getServletContext()); AccountManager am = ctx.getBean("accountManager", AccountManager.class); int accountNumber = Integer.parseInt(request.getParameter("accountNumber")); BigDecimal amount = new BigDecimal(request.getParameter("amount")); // How to use a session scoped bean (Example only) // DepositForm form = (DepositForm) ctx.getBean("depositForm"); // form.setAccountNumber(accountNumber); // form.setAmount(amount); // End of example am.deposit(accountNumber, amount); // We can dispatch to a success page if we prefer! getServletContext().getRequestDispatcher("/deposit.jsp").forward(request, response); } }