servlets tutorial

Servlet Database Access

Servlet Database Access : Data that is shared between Web components and is persistent between invocations of a J2EE application is usually maintained by a database. Web components use the JDBC 2.0 API to access relational databases. The data for the bookstore application is maintained in a database and accessed through the helper class database.BookDB.

ReceiptServlet, for example, calls BookDB.When a user makes a purchase, the buyBooks method is used to update the book inventory. The buyBooks function calls buyBook for each book in the shopping basket. To ensure that the complete order is processed, the buy-Book calls are wrapped in a single JDBC transaction. The use of the shared database connection is synchronised using the [get|release]Connection methods.


Servlet Database Access

public void buyBooks(ShoppingCart cart) throws OrderException {
Collection items = cart.getItems();
Iterator i = items.iterator();
try {
getConnection();
con.setAutoCommit(false);
while (i.hasNext()) {
ShoppingCartItem sci = (ShoppingCartItem)i.next();
BookDetails bd = (BookDetails)sci.getItem();
String id = bd.getBookId();
int quantity = sci.getQuantity();
buyBook(id, quantity);
}
con.commit();
con.setAutoCommit(true);
releaseConnection();
} catch (Exception ex) {
try {
con.rollback();
releaseConnection();
throw new OrderException("Transaction failed: " +
ex.getMessage());
} catch (SQLException sqx) {
releaseConnection();
throw new OrderException("Rollback failed: " +
sqx.getMessage());
}
}
}

Initializing a Servlet

The Web container initialises the servlet after it loads and instantiates the servlet class and before it sends requests from clients. By overriding the init method of the Servlet interface, you can customise this procedure to allow the servlet to read permanent configuration data, initialise resources, and do any other one-time tasks. A servlet that cannot complete its initialization process should throw UnavailableException.

All of the servlets that interact with the bookstore database (BookStoreServlet, CatalogServlet, BookDetailsServlet, and ShowCartServlet) set a variable in their init function that points to the database helper object created by the Web context listener:

public class CatalogServlet extends HttpServlet {
private BookDB bookDB;
public void init() throws ServletException {
bookDB = (BookDB)getServletContext().
getAttribute("bookDB");
if (bookDB == null) throw new
UnavailableException("Couldn't get database.");
}
}

A servlet’s service is implemented in the service method of a GenericServlet, the doMethod methods of a HttpServlet (where Method can take the value Get, Delete, Options, Post, Put, Trace), or any other protocol-specific methods defined by a class that implements the Servlet interface.

Online Training Tutorials

  • servlets tutorialServlet Filter and Servlet ChainServlet Filter and Servlet Chain -We've looked at servlets that take requests from the server and provide results to the client. Servlets, on the other hand, were created as a generic […]
  • servlets tutorialServletRequest InterfaceServletRequest: When a servlet is asked to handle a request (ServletRequest), it often requires specific information about the request in order to process it effectively. The servlet will […]
  • servlets tutorialServlet Life CycleServlet Life Cycle, When a client requests a servlet, the server loads and runs the necessary Java classes. use those classes to build content, which the server then feeds back to the […]
  • servlets tutorialHttpServlet Class in ServletHttpServlet Class is a GenericServlet extension that adds methods for dealing with HTTP-specific data. To handle specific sorts of HTTP requests (GET, POST, and so on), HttpServlet […]
  • servlets tutorialServlets Interview Questions and Answerswe have list out latest and updated Servlets interview questions and their answers for fresher’s as well as experienced users.  These interview question covers latest version of Servlets. […]
  • servlets tutorialServlets Tutorial – Java – TechnosapServlets Tutorial provides the basic and advanced concepts of Servlets. Our Servlets Tutorial is designed for beginners and professionals to learn Servlets online; Java servlets are key […]
  • Payment Medium WorkbenchPayment Medium Workbench (PMW)Payment Medium Workbench (PMW) is used to create payment media. The user is provided with a generic payment medium program for all payment medium formats whose variants are to be entered […]
  • Logistics Invoice Verification in SAP MMWhat is Logistics Invoice Verification in SAP MM?Logistics invoice verification is the final step in procure to pay cycle. It begins with the vendor submitting the invoice for the goods sold. The Accounts Payable group, after receiving […]