Wednesday, September 2, 2009

Write a program to input the new registration details to the Database. In addition, create a DSN to connect the JSP page to the database.

Write a program to input the new registration details to the Database. In addition, create a DSN to connect the JSP page to the database.
Solution:
The files used to run this application are:
1. index.jsp
2. details.jsp
3. Simple.jsp
4. SimpleResults.jsp
5. Signin.jsp
6. SigninFailed.jsp
7. ProcessSimpleAction.java
8. SuccessAction.java
9. SimpleActionForm.java
10.SigninActionForm.java
<html>
<head>
<title>Marko Bank</title>
</head>
<body>
<center><H1>Welcome to Marko Bank Home page</H1></center>
<br><br><br><br><a href="prepareSimple.do">Register New
user</a><br>
<a href="prepareSignin.do">Sign in user</a><br>
</body>
</html>

Enter the code in Notepad and save the file as index.jsp in %TOMCAT_HOME%/webapps/ marko.
<%@ page language="java" contentType="text/html; charset=utf-8" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<HTML>
<HEAD>
<TITLE> New User Registration form</TITLE>
</HEAD>
<CENTER><h1>New User Registration form</h1></CENTER>
<hr noshade="noshade"/>
<p>Enter information into the fields below. Your entries will be displayed when you Submit the form.
<br/>
This is just to demonstrate the Struts html tags. The information that you enter is discarded.</p>
<p>* = required field</p>
<hr noshade="noshade"/>
<html:form action="/processSimple">
<p>* Enter Username you wish to use? <br/><input type="text"
name="name" size="40" maxlength="50"/></p>
<p>* Enter Password:<br/><input type="password"
name="password" size="40" maxlength="50"/></p>
<p>Select the type of account you want to open?:<br/>
<select name="accType">
<option value="savings">Savings</option>
<option value="current">Current</option>
</select>
</p>
<p><input type="checkbox" name="referral"/>Do you have a
referral in this bank?</p>
<p>What banking facility inspired you to open account in this
bank?:<br />
<INPUT TYPE="radio" NAME="inspired" value="Phone
banking">Phone banking<br>
<INPUT TYPE="radio" NAME="inspired" value="ATM Card">ATM
Card.<br>
<INPUT TYPE="radio" NAME="inspired" value="Debit Card">Debit
Card<br>
<INPUT TYPE="radio" NAME="inspired" value="Online
Banking">Online Banking<br>
<INPUT TYPE="radio" NAME="inspired" value="All of the
choices">All of the above.
</p>
<p>Please enter below your professional details.:<br />
<textarea name="profDetails" cols="40" rows="6"/></textarea>
</p>
<hr noshade="noshade" />
<p>
<input type="submit" value="Submit" />
<input type="submit" value="Cancel"/>
</p>
</html:form>
</body>
</html>

Enter the code in Notepad and save the file as Simple.jsp in %TOMCAT_HOME%/webapps/ marko/simple/jsp.
<%@ page language="java" contentType="text/html; charset=utf-8" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>



<html>
<head>
<title>Marko Bank Registration Confirmation</title>
</head>
<body>
<CENTER><h1>Registration Results</h1></CENTER>
<hr noshade="noshade"/>
<p>Hello <bean:write name="simpleForm" property="name" />,</p>
<p><strong>Your Password is :</strong> <bean:write name="simpleForm" property="password" /></p>
<p><strong>Your account type is :</strong> <bean:write name="simpleForm" property="accType" /></p>
<p><strong>You have a referral this is:</strong> <bean:write name="simpleForm" property="referral" /></p>
<p><strong>In Marko bank you like the following:</strong>
<bean:write name="simpleForm" property="inspired" />
</p>
<p><strong>Your professional details are :</strong></p>
<p><bean:write name="simpleForm" property="profDetails" filter="false"/></p>

<p><%=request.getAttribute("message")%></p>
</body>
</html>

Enter the code in Notepad and save the file as SimpleResults.jsp in %TOMCAT_HOME%/webapps/ marko/simple/jsp.
package MARKO;

import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;

public class SimpleActionForm extends ActionForm
{
private String name = null;
private String password = null;
private String accType = null;
private boolean referral = false;
private String inspired = null;
private String profDetails = null;
public SimpleActionForm()
{
super();
}
public void reset(ActionMapping mapping, HttpServletRequest
request)
{
this.name = null;
this.password = null;
this.accType = null;
this.referral = false;
this.inspired = null;
this.profDetails = null;
}
public String getName()
{
return name;
}
public String getPassword()
{
return password;
}
public String getAccType()
{
return accType;
}
public boolean getReferral()
{
return referral;
}
public String getProfDetails()
{
return profDetails;
}
public String getInspired()
{
return inspired;
}
public void setName(String name)
{
this.name = name;
}
public void setPassword(String password)
{
this.password = password;
}
public void setAccType(String accType)
{
this.accType = accType;
}
public void setReferral(boolean Referral)
{
this.referral = Referral;
}
public void setInspired(String inspired)
{
this.inspired = inspired;
}
public void setProfDetails(String profDetails)
{
this.profDetails = profDetails;
}
}

Enter the Java code in Notepad and save the file as SimpleActionForm.java. Compile the file from the command prompt and copy the class file in %TOMCAT_HOME%/webapps/marko/ WEB-INF/classes/MARKO.
package MARKO;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

public class ProcessSimpleAction extends Action
{
public ProcessSimpleAction()
{
super();
}
public ActionForward execute(ActionMapping mapping,
ActionForm form, HttpServletRequest request,
HttpServletResponse response) throws Exception
{
if (isCancelled(request))
{
return mapping.findForward("home");
}
request.setAttribute("message",saveRecords(request));
return mapping.findForward("success");
}
public String saveRecords(HttpServletRequest request)
{
String name = request.getParameter("name");
String password = request.getParameter("password");
String accType = request.getParameter("accType");
String referral = request.getParameter("referral");
String inspired = request.getParameter("inspired");
String profDetails = request.getParameter("profDetails");

try
{
//javax.sql.DataSource dataSource;
//java.sql.Connection myConnection;
//dataSource = getDataSource(request);
//myConnection = dataSource.getConnection();
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
java.sql.Connection connection =
java.sql.DriverManager.getConnection("jdbc:odbc:userd");
java.sql.Statement statement =
connection.createStatement();
String query_car = "insert into userDB values('" + name +
"','" + password + "','" + accType + "','" + referral +
"','" + inspired + "','" + profDetails + "')";
statement.execute(query_car);
connection.close();
}
catch(Exception e)
{
e.printStackTrace();
return "<CENTER&gt<h3 STYLE='COLOR:RED'&gtError in saving the
records... Please try again</h3&gt</CENTER&gt";
}
return "<CENTER&gt<h3&gtRecords Saved Sucessfullly</h3&gt
<CENTER&gt";
}
}

Enter the Java code in Notepad and save the file as ProcessSimpleAction.java. Compile the file from the command prompt and copy the class file in %TOMCAT_HOME%/webapps/marko/ WEB-INF/classes/MARKO.

0 nhận xét: