3. Implement the middle layer
The following uses BookServlet as an example to describe how to implement the Servlet in the middle layer.
(1) initialize Servlet
Public class BookServlet extends HttpServlet {
Protected Connection dbConnection;
Protected PrepareStatement readQuery;
Protected PrepareStatement writeQuery;
Protected String dbName = "jdbc: odbc: BookDatabase ″;
Protected String bookName;
Protected String bookISBN;
Public void init (ServletConfig config) throws ServletExecption
{
Try {
Class. forName ("sun. jdbc. odbc. JdbcOdbcDriver ″);
DbConnection = DriverManager. getConnection (dbName ,″″,″″);
}
Catch (Exception e)
{
System. out. println ("Can not initialize database ″);
}
}
The Servlet init () function is called when the Servlet is activated for the first time. For BookServlet () we have created a Connection to the library database (of course we should have defined the BookDatabase in ODBC). Here we use the Connection object in the Java jdbc api.
(2) Implement service () Operations
When the client sends a request to the Servlet, the Servlet's service () function is called. In service (), we should implement all features of the middle layer.
Public void service (HttpServletRequest request, HttpServletResponse reponse)
Throws ServletException, IOException
{
BookName = request. getParameter ("BookName ″);
BookISBN = request. getParameter ("BookISBN ″);
If (bookName = null & bookISBN = null)
DoQueryBook (request, reponse );
Else doNewBook (request, reponse );
}
First, we use the HttpServletRequest parameter to obtain the input parameters of the client. These parameters are entered in the edit box on the HTML page. Then, we perform database query or database update operations based on your needs.
◆ Query database information and return the result page to the browser
To query database information, you must first construct an SQL statement based on the query conditions (in this example, only all records are returned), set the PrepaerStatement object, and run its executeQuery () request results from the backend database server. After the query result is obtained, the Servlet uses HTTPServletResponse to generate a result HTML page and return it to the browser.
Public void doQueryBook (HttpServletRequest request, HttpServletResponse reponse)
{
Try {
ReadQuery = dbConnection. prepareStatement (
"SELECT? From booktable ″);
String htmlHead = "
String htmlBody = "body>" +;
ResultSet readresultinto readquery.exe cuteQuery ();
While (readSet. next ())
{
String Name = readResult. getString ("BookName ″);
String ISBN = readResult. getString ("ISBN ″);
HtmlBody + = Name + "" + ISBN + ″″;
}
HtmlBody + = "</body>" +
PrintWriter output = new PrinterWrite (response. getOutputStream ());
Response. setContentType ("text/html ″);
Output. println (htmlHead + htmlBody + htmlTail );
}
Catch (Exception e)
{... ... ... }
}
◆ Write data to the database
The process of writing data to the database is similar to that of querying data. In this example, the SQL statement is:
String writeSql = "insert into BookTable (BookName, ISBN) Values (" + bookName + "," + bookISBN + ″)″;
Then call PrepareStatement: executeUpdate ();
4. Use Servlet to further discuss the Middle Layer
Through the example of the above book management, we can see that using Servlet technology to implement the middle layer is very convenient, and its operation process is similar to that written by CGI.