1. Trang chủ
  2. » Công Nghệ Thông Tin

Dynamic Web Pages using JSP - Lab Deliverable 11 pdf

11 364 1

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 11
Dung lượng 448,21 KB

Nội dung

Use Models and ActionForms Ver 1.0 © 2005 Aptech Limited 1 Lab Deliverable 11 Using Models and ActionForms Part II 1. 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/> 2 Ver 1.0 © 2005 Aptech Limited JSP and Struts 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" %> Use Models and ActionForms Ver 1.0 © 2005 Aptech Limited 3 <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() 4 Ver 1.0 © 2005 Aptech Limited JSP and Struts { 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) Use Models and ActionForms Ver 1.0 © 2005 Aptech Limited 5 { 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"); 6 Ver 1.0 © 2005 Aptech Limited JSP and Struts 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><h3 STYLE='COLOR:RED'>Error in saving the records Please try again</h3></CENTER>"; } return "<CENTER><h3>Records Saved Sucessfullly</h3> <CENTER>"; } } 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. Use Models and ActionForms Ver 1.0 © 2005 Aptech Limited 7 Do It Yourself 1. Consider Example 1 where the user details are stored in the database. Use the Sign in link to verify whether the user details are stored in the database. In addition, a page is displayed with username and password fields, when the user clicks the Sign in hyper link. Solution: The files used to run this application are: 1. Signin.jsp 2. SigninFailed.jsp 3. SigninActionForm.java 4. ProcessSigninAction.java <%@ page language="java" contentType="text/html; charset=utf-8" %> <%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %> <HTML> <HEAD> <TITLE> Sign in form</TITLE> </HEAD> <CENTER><h1>Sign in form </h1></CENTER> <hr noshade="noshade" /> <html:form action="/processSignin"> <p>* Enter your Username<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> <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 Signin.jsp in %TOMCAT_HOME%/webapps/ marko/simple/jsp. <html> <body> <h1><center><font color="red">Error Signing in Please go back and retry</font></center></h1> </body> </html> Enter the code in Notepad and save the file as SigninFailed.jsp in %TOMCAT_HOME%/webapps/ marko/simple/jsp. 8 Ver 1.0 © 2005 Aptech Limited JSP and Struts 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 SigninActionForm extends ActionForm { private String name = null; private String password = null; public SigninActionForm() { super(); } public void reset(ActionMapping mapping, HttpServletRequest request) { this.name = null; this.password = null; } public String getName() { return name; } public String getPassword() { return password; } public void setName(String name) { this.name = name; } public void setPassword(String password) { this.password = password; } } Enter the Java code in Notepad and save the file as SigninActionForm.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; Use Models and ActionForms Ver 1.0 © 2005 Aptech Limited 9 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 ProcessSigninAction extends Action { public ProcessSigninAction() { super(); } public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { if (isCancelled(request)) { return mapping.findForward("home"); } if(validateUser(request)) { return mapping.findForward("success"); } else { return mapping.findForward("faliure"); } } public boolean validateUser(HttpServletRequest request) { String name = request.getParameter("name"); String password = request.getParameter("password"); 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 = "select * from userDB where name ='" + name + "' and password = '" + password + "'"; java.sql.ResultSet res = statement.executeQuery(query_car); if (res.next()) { 10 Ver 1.0 © 2005 Aptech Limited JSP and Struts connection.close(); return true; } else { connection.close(); return false; } } catch(Exception e) { e.printStackTrace(); return false; } } } Enter the Java code in Notepad and save the file as ProcessSigninAction.java. Compile the file from the command prompt and copy the class file in %TOMCAT_HOME%/webapps/marko/ WEB-INF/classes/MARKO. <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd"> <struts-config> <! == Data Source Configuration == > <data-sources> <! configuration for commons BasicDataSource > <! <data-source> <set-property property="driverClassName" value="sun.jdbc.odbc.JdbcOdbcDriver" /> <set-property property="url" value="jdbc:odbc:userd" /> <set-property property="username" value="" /> <set-property property="password" value="" /> <set-property property="maxActive" value="10" /> <set-property property="maxWait" value="5000" /> <set-property property="defaultAutoCommit" value="false" /> <set-property property="defaultReadOnly" value="false" /> <set-property property="validationQuery" value="SELECT COUNT(*) FROM market" /> </data-source> > </data-sources> <! == Form Bean Definitions == > <form-beans> [...]... path=" /jsp/ simple/Signin .jsp" /> //created in Exercise 1 LG 18 Update the struts-config.xml... //created in Exercise 1 LG 18 ... path="/prepareSimple" type="MARKO.SuccessAction"> //created in Exercise 1 LG 18 //created in Exercise 1 LG 18 Update the struts-config.xml file used in the Web application The output of the program is as shown in Figure 18.1 Use Models and ActionForms Ver 1.0 © 2005 Aptech Limited 11 . 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". as index .jsp in %TOMCAT_HOME%/webapps/ marko. <%@ page language="java" contentType="text/html; charset=utf-8" %> <%@ taglib uri=" /WEB- INF/struts-html.tld". 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

Ngày đăng: 14/08/2014, 05:20

TỪ KHÓA LIÊN QUAN