Servlet that handles a post request from a web form

Web application in which, a web form allows the end-user to perform online transfer of funds from Savings Account to Current Account.

Directory Structure

fundstransferapp
transfer.html
==> WEB-INF
====> web.xml
====> classes
======> MoneyTransferServlet.class
====> lib
======> Classesl2.jar

Web form URL

http://localhost:8080/fundstransferapp/transfer.html

Source code of transfer.html

<HTML>

<BODY>

<CENTER>

<H2> ON LINE FUNDS TRANSFER INTERFACE </H2>

<FORM ACTION ="./transfer" METHOD="POST">

Savings A/C NO<INPUT TYPE=text NAME="sbno"><BR>

Current A/C NO<INPUTTYPE=text NAME="sbno"><BR>

TransferAmount<INPUTTYPE=text NAME="amount"><BR>

<INPUT TYPE-submit VALUE=Transfer the fund >

</FORM>

</CENTER>

</BODY>

</HTML>

Source code of web.xml

< ?xml version = "1.0" enco ding = "ISO-8859-1"? >

<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http ://java.sun. com/dtd/web-app_2_3. dtd">

<web-app>

<servlet>

<servlet-name>moneytransfer</servlet-name>

<servlet-class>MoneyTransferServlet</servlet-class>

<init-param >

<param-name>driver</param-name>

<param-value>oracle.jdbc.driver.OracleDriver</param-value >

</init-param >

<init-param >

<param-name>url</param-name>

<param-value>jdbc: oracle: thin :@localhost:l 521 : orcl</param-value>

</init-param >

<init-param>

<param-name > user</param-name >

<param-value>scott</param-value>

</init-param >

<init-param >

<param-name>pass</param-name>

<param-value>tiger</param-value>

</init-param>

</servlet>

<servlet-mapping >

<servlet-name>moneytransfer</servlet-name>

<url-pattern >/transfer </url-pattern >

</servlet-mapping >

</web-app>

Source code of the Servlet

import Java.io. *;

import java.sql. *;

import javax.servlet. *;

import javax.servlet. http. *;

public class MoneyTransferServlet extends HttpServlet

{

Connection con;

public void init(ServletConfig config)throws ServletException

{

try

{

String driver- con fig. getInitParameter("driver");

String url-config.getInitParameter("url");

String user= config.getInitParameter("user");

String pwd= config.getInitParameter("pass");

Class.forName(driver);

con=DriverManager.getConnection(url,user,pwd);

}

catch(Exception e)

{

e.printStackTrace();

}

}//init

public void doPost(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException

{

Statement st—null;

int sbaccno -Integer.parselnt(req. getParameter("sbno "));

int caccno=Integer.parseInt(req.getParameter("cano"));

float amount=Float.parseFloat(req.getParameter("amount"));

res.setContentType("text/html"); PrintWriter out-res.getWriterQ;

out.println("<HTML>");

out.println("<BODY BGCOLOR = wheat> ");

try

{

st=con.createStatement();

String sql1 = "UPDATE SAVINGS SET BALANCE-BALANCE -"+amount+

"WHERE ACCNO="+sbaccno;

String sql2 = "UPDATE CURRANT SET BALANCE-^ BALANCE +" +amount+ "WHERE ACCNO="+caccno;

st.executeUpdate(sql1);

st.executeUpdate(sql2);

out.println("<H2>FUNDS TRANSFERED SUCCESS FULLY</H2>");

}//try

catch (Exception e){e.printStackTrace();

}

finally

{

try

{

if(st !=null)

st.close();

}

catch(Exception e)

{

e.printStackTrace();

}

}//finally

outprintln("</BODY >");

out.println("</HTML > ");

out.close();

}//doPost()

public void destroy()

{

try

{

if (con !=null)

con.close();

}

catch(Exception e)

{

e.printStackTrace();                                                      .

}

}

}//class

In the init method of the MoneyTransferServlet class we retrieved the initialization parameters. Database driver class, connection URL, user name and password are supplied from the web.xml declaratively. Whenever we want to change the connection related things, we need not change the source code of the Servlet. Only few entries in the web.xml are to be modified.