Monday, April 27, 2009

Write a program to add a link requesting for a bank chequebook

Write a program to add a link requesting for a bank chequebook. Extend example 3 to add a link to the welcome page. A page should be created to display to the user that the request for the chequebook has been placed.

Solution:
The files used to run the application are:
1. home.jsp
2. chq.jsp
3. acclosure.jsp
4. MainServlet.java

<html>
<head>
<title> Home </title>
</head>
<body>
<%
String userName = (String)session.getAttribute("UserName");
%>
<h3 align ='center'>Welcome <%=userName%> </h3>
While you are at the home page of MARKO Bank, please select
any of the options given below<br>
<br><br>
<a href="redirecterServlet?action=Withdrawal">Withdrawal<a>
<br>
<a href="redirecterServlet?action=deposit">Deposit<a><br>
<a href="redirecterServlet?action=chq">Request for cheque
book</a>
<br>
</body>
</html>

Update the home.jsp page with hyperlinks for requesting the chequebook and account closure. Save the file in %TOMCAT_HOME%/webapps/Application.

<html>
<head>
<title> checque </title>
</head>
<body>
<%
String userName = (String)session.getAttribute("UserName");
java.util.Date date = new java.util.Date();
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
java.sql.Connection connection =
java.sql.DriverManager.getConnection("jdbc:odbc:userdb
");
java.sql.Statement statement =
connection.createStatement();
String query_car = "update userdetails set ChqStatus =
'Requested for checque on" + date.toString() + "'
where UserName = '" + userName + "'" ;
statement.execute(query_car);
}
catch(Exception e)
{
e.printStackTrace();
}
%>
<h1 align='center'>Your Request is Recorded on <%=date.toString()%>...</h1>
<br><br>
<a href="redirecterServlet?action=home">Marko Home</a><br>
<a href="redirecterServlet?action=Withdrawal">Withdrawal<a>
<br>
<a href="redirecterServlet?action=deposit">Deposit<a>
<br>
</body>
</html>

Enter the code in Notepad and save the file as chq.jsp in %TOMCAT_HOME%/webapps/ Application.

package MARKO;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class MainServlet extends HttpServlet
{
public void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException,
IOException {doGet(request,response);
}

public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
PrintWriter out = response.getWriter();
response.setContentType("text/html");
String action;
HttpSession ses = request.getSession(true);
action = request.getParameter("action");
if (action == null)
{
return;
}
else if (action.equals("home"))
{
RequestDispatcher dis =
request.getRequestDispatcher("/home.jsp");
dis.include(request, response);
}
else if (action.equals("chq"))
{
RequestDispatcher dis =
request.getRequestDispatcher("/chq.jsp");
dis.include(request, response);
}
else if (action.equals("Withdrawal"))
{
RequestDispatcher dis =
request.getRequestDispatcher("/Withdrawal.jsp");
dis.include(request, response);
}
else if (action.equals("deposit"))
{
RequestDispatcher dis =
request.getRequestDispatcher("/deposit.jsp");
dis.include(request, response);
}
else
{
out.println("Error in Accessing the Site");
}
out.close();
}
}

Update the MainServlet.java page to redirect the request to the new chequebook page. Save the file in %TOMCAT_HOME%/webapps/Application.




0 nhận xét: