tag:blogger.com,1999:blog-33196375622683506462024-03-13T09:00:02.177-07:00Learning JSP by ExampleThanks for visiting my blog!Have a good timeadminhttp://www.blogger.com/profile/10028997688633545873noreply@blogger.comBlogger37125tag:blogger.com,1999:blog-3319637562268350646.post-88064906842371321922012-02-13T05:51:00.000-08:002012-03-25T07:45:21.019-07:00Share all exercise learn JSP step by stepHi all,<br />
I have a books very good about JSP. It's include all Lab step by step. I <span class="short_text" id="result_box" lang="en"><span class="hps">hope it is </span></span><span class="short_text" id="result_box" lang="en"><span class="hps">useful for you<br />Please, Download by link below<br /><br />Lab 1. </span></span><a href="http://adf.ly/6dWTM" target="_blank">Developing Web Applications-An Overview</a><br />
Lab 2. <a href="http://adf.ly/6dWaC" target="_blank">Using Java Server Pages (JSP) Tags</a><br />
Lab 3. <a href="http://adf.ly/6dWgX" target="_blank">Using JSP ScriptlUsing JSP Scriptlet</a><br />
Lab 4. <a href="http://adf.ly/6dWjc" target="_blank">Using JavaBeans in Java Server Pages </a><br />
Lab 5. <a href="http://adf.ly/6dWmx" target="_blank">JSP Application Models</a> <br />
Lab 6. <a href="http://adf.ly/6dWq7" target="_blank">Session Management</a><br />
Lab 7.<a href="http://adf.ly/6dWrm" target="_blank"> Java Database Connectivity</a><br />
Lab 8. <a href="http://adf.ly/6dWtO" target="_blank">Creating Web Applications</a><br />
Lab 9. <a href="http://adf.ly/6dWx2" target="_blank">Introduction to Struts</a><br />
Lab 10. <a href="http://www.mediafire.com/download.php?73qfvacd9p8hk4b" target="_blank">Working with User Input</a><br />
Lab 11.<a href="http://adf.ly/6dX2g" target="_blank"> Using Models and ActionForms</a><br />
Lab 12. <a href="http://adf.ly/6dX4v" target="_blank">Using Actions</a><br />
Lab 13. <a href="http://adf.ly/6dX7k" target="_blank">Struts Tags</a><br />
Lab 14. <a href="http://adf.ly/6dXAq" target="_blank">Lab Deliverable 14</a><br />
Lab 15. <a href="http://adf.ly/6dXE0" target="_blank">Creating Custom Tags</a><br />
Lab 16. <a href="http://adf.ly/6dXFN" target="_blank">Struts Validator Framework</a><br />
Lab 17. <a href="http://adf.ly/6dXIN" target="_blank">Tiles Framework</a><br />
Lab 18. <a href="http://adf.ly/6dXL5" target="_blank">Using Eclipse-1</a><br />
Lab 19. <a href="http://adf.ly/6dXP8" target="_blank">Using Eclipse-2</a><br />
<br />
Good luck!Mr.Funnyhttp://www.blogger.com/profile/04168800068928572028noreply@blogger.com0tag:blogger.com,1999:blog-3319637562268350646.post-53344456783821519732011-02-07T04:58:00.000-08:002011-02-07T05:48:53.287-08:00Share books JSP for everyoneI have some book JSP and Java. I want share for everyone. You can download link:<br /><a href="http://downloadvnn.blogspot.com/search/label/Ebook%20Java">Download here</a><br />Note: I will update when new book.<br />Thank all!Mr.Funnyhttp://www.blogger.com/profile/04168800068928572028noreply@blogger.com4tag:blogger.com,1999:blog-3319637562268350646.post-45507150062949195172009-09-02T05:55:00.000-07:002011-05-31T10:40:10.365-07:00Write 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.<br />Solution:<br />The files used to run this application are:<br /> 1. index.jsp<br /> 2. details.jsp<br /> 3. Simple.jsp <br /> 4. SimpleResults.jsp<br /> 5. Signin.jsp<br /> 6. SigninFailed.jsp<br /> 7. ProcessSimpleAction.java<br /> 8. SuccessAction.java<br /> 9. SimpleActionForm.java<br />10.SigninActionForm.java<br /><pre name="code" class="cpp"><html><br /><head><br /><title>Marko Bank</title><br /></head><br /><body><br /><center><H1>Welcome to Marko Bank Home page</H1></center><br /><br><br><br><br><a href="prepareSimple.do">Register New<br />user</a><br><br /><a href="prepareSignin.do">Sign in user</a><br><br /></body><br /></html><br /></pre><br />Enter the code in Notepad and save the file as index.jsp in %TOMCAT_HOME%/webapps/ marko.<br /><pre name="code" class="cpp"><%@ page language="java" contentType="text/html; charset=utf-8" %><br /><%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %><br /><HTML><br /><HEAD><br /><TITLE> New User Registration form</TITLE><br /></HEAD><br /><CENTER><h1>New User Registration form</h1></CENTER><br /><hr noshade="noshade"/><br /><p>Enter information into the fields below. Your entries will be displayed when you Submit the form.<br /><br/><br />This is just to demonstrate the Struts html tags. The information that you enter is discarded.</p><br /><p>* = required field</p><br /><hr noshade="noshade"/> <br /><html:form action="/processSimple"><br /><p>* Enter Username you wish to use? <br/><input type="text"<br />name="name" size="40" maxlength="50"/></p><br /><p>* Enter Password:<br/><input type="password"<br />name="password" size="40" maxlength="50"/></p><br /><p>Select the type of account you want to open?:<br/><br /><select name="accType"><br /><option value="savings">Savings</option><br /><option value="current">Current</option><br /></select><br /></p><br /><p><input type="checkbox" name="referral"/>Do you have a<br />referral in this bank?</p><br /><p>What banking facility inspired you to open account in this<br />bank?:<br /><br /><INPUT TYPE="radio" NAME="inspired" value="Phone<br />banking">Phone banking<br><br /><INPUT TYPE="radio" NAME="inspired" value="ATM Card">ATM<br />Card.<br><br /><INPUT TYPE="radio" NAME="inspired" value="Debit Card">Debit<br />Card<br><br /><INPUT TYPE="radio" NAME="inspired" value="Online<br />Banking">Online Banking<br><br /><INPUT TYPE="radio" NAME="inspired" value="All of the<br />choices">All of the above.<br /></p> <br /><p>Please enter below your professional details.:<br /><br /><textarea name="profDetails" cols="40" rows="6"/></textarea><br /></p><br /><hr noshade="noshade" /><br /><p><br /><input type="submit" value="Submit" /><br /><input type="submit" value="Cancel"/><br /></p><br /></html:form><br /></body><br /></html><br /></pre><br />Enter the code in Notepad and save the file as Simple.jsp in %TOMCAT_HOME%/webapps/ marko/simple/jsp.<br /><pre name="code" class="cpp"><%@ page language="java" contentType="text/html; charset=utf-8" %><br /><%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %><br /><br /><br /><br /><html><br /><head><br /><title>Marko Bank Registration Confirmation</title><br /></head><br /><body><br /><CENTER><h1>Registration Results</h1></CENTER><br /><hr noshade="noshade"/><br /><p>Hello <bean:write name="simpleForm" property="name" />,</p><br /><p><strong>Your Password is :</strong> <bean:write name="simpleForm" property="password" /></p><br /><p><strong>Your account type is :</strong> <bean:write name="simpleForm" property="accType" /></p><br /><p><strong>You have a referral this is:</strong> <bean:write name="simpleForm" property="referral" /></p><br /><p><strong>In Marko bank you like the following:</strong><br /> <bean:write name="simpleForm" property="inspired" /><br /></p><br /><p><strong>Your professional details are :</strong></p><br /><p><bean:write name="simpleForm" property="profDetails" filter="false"/></p><br /><br /><p><%=request.getAttribute("message")%></p><br /></body><br /></html><br /></pre><br />Enter the code in Notepad and save the file as SimpleResults.jsp in %TOMCAT_HOME%/webapps/ marko/simple/jsp.<br /><pre name="code" class="cpp">package MARKO;<br /><br />import javax.servlet.http.HttpServletRequest;<br />import org.apache.struts.action.ActionError;<br />import org.apache.struts.action.ActionErrors;<br />import org.apache.struts.action.ActionForm;<br />import org.apache.struts.action.ActionMapping;<br /><br />public class SimpleActionForm extends ActionForm<br />{<br />private String name = null;<br />private String password = null;<br />private String accType = null;<br />private boolean referral = false;<br />private String inspired = null;<br />private String profDetails = null;<br />public SimpleActionForm()<br />{<br />super();<br />}<br />public void reset(ActionMapping mapping, HttpServletRequest<br />request)<br />{<br />this.name = null;<br />this.password = null;<br />this.accType = null;<br />this.referral = false;<br />this.inspired = null;<br />this.profDetails = null;<br />} <br />public String getName()<br />{<br />return name;<br />}<br />public String getPassword()<br />{<br />return password;<br />}<br />public String getAccType()<br />{<br />return accType;<br />}<br />public boolean getReferral()<br />{<br />return referral;<br />}<br />public String getProfDetails()<br />{<br />return profDetails;<br />}<br />public String getInspired()<br />{<br />return inspired;<br />}<br />public void setName(String name)<br />{<br />this.name = name;<br />}<br />public void setPassword(String password)<br />{<br />this.password = password;<br />}<br />public void setAccType(String accType)<br />{<br />this.accType = accType;<br />}<br />public void setReferral(boolean Referral)<br />{<br />this.referral = Referral;<br />} <br />public void setInspired(String inspired)<br />{<br />this.inspired = inspired;<br />}<br />public void setProfDetails(String profDetails)<br />{<br />this.profDetails = profDetails;<br />}<br />}<br /></pre><br />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.<br /><pre name="code" class="cpp">package MARKO;<br /><br />import javax.servlet.http.HttpServletRequest;<br />import javax.servlet.http.HttpServletResponse;<br />import org.apache.struts.action.Action;<br />import org.apache.struts.action.ActionForm;<br />import org.apache.struts.action.ActionForward;<br />import org.apache.struts.action.ActionMapping;<br /><br />public class ProcessSimpleAction extends Action<br />{<br />public ProcessSimpleAction()<br />{<br />super();<br />}<br />public ActionForward execute(ActionMapping mapping,<br />ActionForm form, HttpServletRequest request,<br />HttpServletResponse response) throws Exception<br />{<br />if (isCancelled(request))<br />{<br />return mapping.findForward("home");<br />}<br />request.setAttribute("message",saveRecords(request));<br />return mapping.findForward("success");<br />}<br />public String saveRecords(HttpServletRequest request)<br />{ <br />String name = request.getParameter("name");<br />String password = request.getParameter("password");<br />String accType = request.getParameter("accType");<br />String referral = request.getParameter("referral");<br />String inspired = request.getParameter("inspired");<br />String profDetails = request.getParameter("profDetails");<br /><br />try<br />{<br />//javax.sql.DataSource dataSource;<br />//java.sql.Connection myConnection;<br />//dataSource = getDataSource(request);<br />//myConnection = dataSource.getConnection(); <br />Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");<br />java.sql.Connection connection =<br />java.sql.DriverManager.getConnection("jdbc:odbc:userd");<br />java.sql.Statement statement =<br />connection.createStatement();<br />String query_car = "insert into userDB values('" + name +<br />"','" + password + "','" + accType + "','" + referral +<br />"','" + inspired + "','" + profDetails + "')";<br />statement.execute(query_car);<br />connection.close();<br />}<br />catch(Exception e)<br />{<br />e.printStackTrace();<br />return "<CENTER&gt<h3 STYLE='COLOR:RED'&gtError in saving the<br />records... Please try again</h3&gt</CENTER&gt";<br />}<br />return "<CENTER&gt<h3&gtRecords Saved Sucessfullly</h3&gt<br /><CENTER&gt";<br />}<br />}<br /></pre><br />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.Mr.Funnyhttp://www.blogger.com/profile/04168800068928572028noreply@blogger.com0tag:blogger.com,1999:blog-3319637562268350646.post-14724189696649520912009-06-26T02:40:00.000-07:002011-02-07T04:22:53.338-08:00Create a user details page<span style="color: rgb(204, 0, 0);">Create a user details page. The page should have First Name, Last Name, and Email address fields. On clicking the submit button, a new Web page should display the details entered by the user.<br />Hint: Use getAttribute to display the user details.<br /><br />Solution:<br /><br />The files used in this exercise are:<br /><br />1. index.jsp<br />2. displayname.jsp<br />3. NameForm.java<br />4. NameAction.java<br /><pre name="code" class="cpp"><br /><%@ page language="java" %><br /><%@ page language="java" %><br /><%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %><br /><br /><html><br /><head><br /><title>Sample Struts Application</title><br /></head><br /><body><br /><h3> User Details</h3><br /><html:form action="Name" name="nameForm" type="example.NameForm" ><br /><table width="80%" border="0"><br /><br /><tr><br /><td><b>First Name:</b><br /><html:text property="name" /><br /></td><br /></tr><br /><br /><tr><br /><td><b>Last Name:</b><br /><html:text property="last" /><br /></td><br /></tr><br /><br /><tr><br /><td><br /><b>Email: </b><br /><html:text property="email" /><br /></td><br /></tr><br /><br /><tr><br /><td><html:submit /><br /></td><br /></tr><br /></table><br /><br /></html:form><br /></body><br /></html><br /></pre><br />Enter the code in Notepad and save the file as index.jsp in %TOMCAT_HOME%/webapps/ details.<br /><pre name="code" class="cpp"><br /><html><br /><head><br /><title>Sample Struts Display Name</title><br /></head><br /><body><br /><h2> Confirm the Details You Entered</h2><br /><table width="80%" border="0"><br /><br /><tr><br /><td><br /><b>First Name:</b><%= request.getAttribute("NAME") %><br /></td><br /></tr><br /><br /><tr><br /><td><br /><b>Last Name:</b><%= request.getAttribute("LAST") %><br /></td><br /></tr><br /><br /><tr><br /><td><br /><b>Email:</b><%= request.getAttribute("EMAIL") %></td><br /></tr><br /><br /></table><br /></body><br /></html><br /></pre><br />Enter the code in Notepad and save the file as displayname.jsp in %TOMCAT_HOME%/ webapps/details.<br /><pre name="code" class="cpp"><br />package example;<br />import javax.servlet.http.HttpServletRequest;<br />import org.apache.struts.action.ActionForm;<br />import org.apache.struts.action.ActionMapping;<br /><br />public class NameForm extends ActionForm<br />{<br />private String name = null;<br />private String last = null;<br />private String email = null;<br /><br />public String getName()<br />{<br />return (name);<br />}<br />public String getLast()<br />{<br />return (last);<br />}<br />public String getEmail()<br />{<br />return (email);<br />}<br />public void setName(String name)<br />{<br />this.name = name;<br />}<br />public void setLast(String last)<br />{<br />this.last = last;<br />}<br />public void setEmail(String email)<br />{<br />this.email = email;<br />}<br />public void reset(ActionMapping mapping, HttpServletRequest request)<br />{<br />this.name = null;<br />this.last= null;<br />this.email=null;<br />}<br />}<br /></pre><br />Enter the Java code in Notepad and save the file as NameForm.java. Compile the file from the command prompt and copy the class file in %TOMCAT_HOME%/webapps/details/WEB-INF/classes/example.<br /><pre name="code" class="cpp"><br />package example;<br />import java.io.IOException;<br />import javax.servlet.ServletException;<br />import javax.servlet.http.HttpServletRequest;<br />import javax.servlet.http.HttpServletResponse;<br />import org.apache.struts.action.Action;<br />import org.apache.struts.action.ActionForm;<br />import org.apache.struts.action.ActionForward;<br />import org.apache.struts.action.ActionMapping;<br /><br />public class NameAction extends Action<br />{<br />public ActionForward execute(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response)<br />throws IOException, ServletException<br />{<br />String target = new String("success");<br />String name=null;<br />String last=null;<br />String email=null;<br /><br />if ( form != null )<br />{<br />NameForm nameForm = (NameForm)form;<br />NameForm lastForm = (NameForm)form;<br />NameForm emailForm = (NameForm)form;<br /><br />name = nameForm.getName();<br />last = lastForm.getLast();<br />email = emailForm.getEmail();<br />}<br /><br />if ( name == null )<br />{<br />target = new String("failure");<br />}<br />else<br />{<br />request.setAttribute("NAME", name);<br />request.setAttribute("LAST", last);<br />request.setAttribute("EMAIL", email);<br />}<br />return (mapping.findForward(target));<br />}<br />}<br /></pre><br />Enter the Java code in Notepad and save the file as NameAction.java. Compile the file from the command prompt and copy the class file in %TOMCAT_HOME%/webapps/details/WEB-INF/classes/example.<br /><pre name="code" class="cpp"><br /><?xml version="1.0" encoding="ISO-8859-1" ?><br /><!DOCTYPE struts-config PUBLIC<br />"-//Apache Software Foundation//DTD Struts Configuration 1.1//EN"<br />"http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd"><br /><br /><struts-config><br /><br /><!-- Form Bean Definitions --><br /><br /><form-beans><br /><br /><form-bean name="nameForm" type="example.NameForm"/><br /><br /></form-beans><br /><br /><!-- Action Mapping Definitions --><br /><br /><action-mappings><br /><br /><action path="/Name" type="example.NameAction" name="nameForm" input="/index.jsp"><br /><br /><forward name="success" path="/displayname.jsp"/><br /><br /><forward name="failure" path="/index.jsp"/><br /><br /></action><br /><br /></action-mappings><br /></struts-config><br /></pre><br /></span><span id="fullpost"><span style="color: rgb(204, 0, 0);"></span><br />Update the struts-config.xml file used in the Web application.<br /><br />The output of the program is as shown in Figure 17.1.<br /><br /><br /><div style="text-align: center;"><a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhOooPDEJBud_OXTaAp8_RCtwhwkOpCbm1sWKXSdM6ddwrfZSpRfNFZg2bOaA1gWUaVfSuszGP0sz4OlMGSGECDJOiuWnDbVsr4MzIi3GkF69NSllsuyEwd46c8DI9aoAqucwQSrzmTB50/s1600-h/5.JPG"><img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 400px; height: 230px;" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhOooPDEJBud_OXTaAp8_RCtwhwkOpCbm1sWKXSdM6ddwrfZSpRfNFZg2bOaA1gWUaVfSuszGP0sz4OlMGSGECDJOiuWnDbVsr4MzIi3GkF69NSllsuyEwd46c8DI9aoAqucwQSrzmTB50/s400/5.JPG" alt="" id="BLOGGER_PHOTO_ID_5351570812096620050" border="0" /></a>Figure 17.1: Accepting User Details<br /></div><br />The output of the program that displays user details is as shown in Figure 17.2.<br /><br /><div style="text-align: center;"><a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi0pwqn8oVgL0V-PZp4-yjqbi93YKUYe49jqUJPuYWnVYWKdxPVFdVE-THCcSE_37ExWDvnWRht8i10jSrMxbq-s4x6f77c5Sas0SZrBTelDW8qe7y-HyDit4fxy5J8lwLmMRo_m-CNiP4/s1600-h/6.JPG"><img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 400px; height: 231px;" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi0pwqn8oVgL0V-PZp4-yjqbi93YKUYe49jqUJPuYWnVYWKdxPVFdVE-THCcSE_37ExWDvnWRht8i10jSrMxbq-s4x6f77c5Sas0SZrBTelDW8qe7y-HyDit4fxy5J8lwLmMRo_m-CNiP4/s400/6.JPG" alt="" id="BLOGGER_PHOTO_ID_5351570806343618370" border="0" /></a>Figure 17.2: Details Page<br /></div><br /></span>adminhttp://www.blogger.com/profile/10028997688633545873noreply@blogger.com3tag:blogger.com,1999:blog-3319637562268350646.post-47483018834591166842009-06-20T17:16:00.001-07:002011-02-07T04:32:42.663-08:00Create a Login form using struts<span style="color: rgb(153, 0, 0);">Create a Login form using struts. Create two text fields in the Web page, and name the fields as User and Password. The page includes a Submit button. After the user clicks the Submit button, a page should appear indicating that the user is a valid user.<br /><br />The files used in this exercise are:<br /><br />1. cust.jsp<br />2. valid.jsp<br />3. CustForm.java<br />4. CustAction.java<br /><pre name="code" class="cpp"><br /><%@ taglib uri = "/WEB-INF/struts-html.tld" prefix="html" %><br /><html><br /><head> <title>Testing struts</title> </head><br /><body><br /><html:form action = "cust.do" > <center><br /><b>User:</b> <html:text property = "first" /><br /><b>Password:</b><html:password property = "pwd" /><br /><html:submit /> </center><br /></html:form><br /></body><br /></html><br /></pre><br />Enter the code in Notepad and save the file as cust.jsp in %TOMCAT_HOME%/webapps/ struts-test.<br /><pre name="code" class="cpp"><br /><html><br /><head><title>Valid User</title><br /></head><br /><body><br /><center><h2><b>VALID USER</b></h2></center><br /></body><br /></html><br /></pre><br />Valid Use<br />Enter the code in Notepad and save the file as valid.jsp in %TOMCAT_HOME%/webapps/ struts-test.<br /><pre name="code" class="cpp"><br />package common.test;<br />import javax.servlet.http.*;<br />import org.apache.struts.action.*;<br /><br />public class CustForm extends ActionForm<br />{<br />private String mFirst = null;<br />private String mPwd = null;<br /><br />public String getFirst() { return mFirst; }<br />public void setFirst(String aFirst) { mFirst = aFirst; }<br /><br />public String getPwd() { return mPwd; }<br />public void setPwd(String aPwd) { mPwd = aPwd; }<br />public void reset(ActionMapping aMapping, HttpServletRequest aRequest)<br />{<br />mFirst = null;<br />mPwd = null;<br />}<br />}<br /></pre><br />Enter the Java code in Notepad and save the file as CustForm.java. Compile the file from the command prompt and copy the class file in %TOMCAT_HOME%/webapps/struts-test/WEB-INF/classes/common/test.<br /><pre name="code" class="cpp"><br />package common.test;<br />import javax.servlet.*;<br />import javax.servlet.http.*;<br />import org.apache.struts.action.*;<br /><br />public class CustAction extends Action<br />{<br /><br />public ActionForward perform(ActionMapping aMapping, ActionForm aForm,HttpServletRequest aRequest, HttpServletResponse aResponse)throws ServletException<br />{<br />CustForm f = (CustForm) aForm;<br />String first = f.getFirst();<br />String last = f.getPwd();<br />return aMapping.findForward("saved");<br />}<br />}<br /></pre><br />Enter the Java code in Notepad and save the file as CustAction.java. Compile the file from the command prompt and copy the class file in %TOMCAT_HOME%/webapps/struts-test/WEB-INF/classes/common/test.<br /><pre name="code" class="cpp"><br /><?xml version="1.0" encoding="ISO-8859-1" ?><br /><!DOCTYPE struts-config PUBLIC<br />"-//Apache Software Foundation//DTD Struts Configuration 1.0//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_0.dtd"><br /><struts-config><br /><!-- Form Bean Definitions --><br /><form-beans><br /><form-bean name = "custForm" type = "common.test.CustForm"/><br /></form-beans><br /><!-- Global Forward Definitions --><br /><global-forwards><br /><forward name = "start" path = "/index.jsp"/><br /></global-forwards><br /><!-- Action Mapping Definitions --><br /><action-mappings><br /><action path = "/cust" type = "common.test.CustAction" name = "custForm"><br /><forward name = "saved" path = "/valid.jsp"/><br /></action><br /></action-mappings><br /></struts-config><br /></pre></span><span id="fullpost"><span style="color: rgb(204, 0, 0);"></span><br /><br />Update the struts-config.xml file used in the Web application<br /><br />The output of the program is as shown in Figure 16.3.<br /><br /><a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgMoe-txCCc75wDGDhkgWMH9TjbUw6QhTI6H4Ym312C73klphI6vH2DnL8ZtDGO5HkYsT5_9spLIg2CfLe9HWtJeZo_RIeJlqljGkVet3ZYFIWfvNHLUbLOLe9T1PkJOAfs5MH5m1YCGkk/s1600-h/3.JPG"><img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 400px; height: 231px;" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgMoe-txCCc75wDGDhkgWMH9TjbUw6QhTI6H4Ym312C73klphI6vH2DnL8ZtDGO5HkYsT5_9spLIg2CfLe9HWtJeZo_RIeJlqljGkVet3ZYFIWfvNHLUbLOLe9T1PkJOAfs5MH5m1YCGkk/s400/3.JPG" alt="" id="BLOGGER_PHOTO_ID_5349569430837633730" border="0" /></a><br /><div style="text-align: center;">Figure 16.3: Login page<br /></div><br />After the user clicks the Submit button, the output of the program is as shown in Figure 16.2.<br /><br /><div style="text-align: center;"><a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgpDo4Y3iIEY_qwddvrQryXCBqZ9ZqywcmwpIQNvzpbynQLEEJ04GWBnPCNU6bPsrLyDEE5knV8PZaEWhYXd96pETxnSOKSqNHy3pifqKZH9dSVftoKU6iDJGmWtfXbCzuXhBR9yMTKG-k/s1600-h/4.JPG"><img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 400px; height: 231px;" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgpDo4Y3iIEY_qwddvrQryXCBqZ9ZqywcmwpIQNvzpbynQLEEJ04GWBnPCNU6bPsrLyDEE5knV8PZaEWhYXd96pETxnSOKSqNHy3pifqKZH9dSVftoKU6iDJGmWtfXbCzuXhBR9yMTKG-k/s400/4.JPG" alt="" id="BLOGGER_PHOTO_ID_5349569424813128850" border="0" /></a>Figure 16.4: Valid user page<br /></div></span>adminhttp://www.blogger.com/profile/10028997688633545873noreply@blogger.com0tag:blogger.com,1999:blog-3319637562268350646.post-38604922225067814392009-06-20T16:56:00.000-07:002011-02-07T04:34:36.283-08:00Create a struts-blank.war blank application1. Create a struts-blank.war blank application. Use the blank application to create a Web page that displays the title and author of a book. The page includes a Submit button. After the user clicks the Submit button, a page should appear indicating that the request is being processed. The example requires two JSP pages and two JavaBeans. Update struts-config.xml file to associate the Web pages with the JavaBeans.<br /><br />Solution:<br /><br />The files used in this exercise are:<br /><br />1. test.jsp<br />2. searching.jsp<br />3. BookForm.java<br />4. BookAction.java<br /><pre name="code" class="cpp"><br /><%@ taglib uri = "/WEB-INF/struts-html.tld" prefix="html" %><br /><html><br /><head> <title>Struts</title><br /></head><br /><body><br /><html:form action="book.do" ><center><br /><b>TITLE:<b> <html:text property ="title" /><p><br /><b>AUTHOR:<b> <html:text property ="author" /><br> <p><br /><html:submit /></center><br /></html:form><br /></body><br /></html><br /></pre><br />Enter the code in Notepad and save the file as test.jsp in %TOMCAT_HOME%/webapps/ struts-test.<br /><pre name="code" class="cpp"><br /><html><br /><head><title>Searching</title><br /></head><br /><body><br /><h3><b>Searching.....<b></h3><br /></body><br /></html><br /></pre><br />Enter the code in Notepad and save the file as searching.jsp in %TOMCAT_HOME%/webapps/ struts-test.<br /><pre name="code" class="cpp"><br />package com;<br />import javax.servlet.http.*;<br />import org.apache.struts.action.*;<br /><br />public class BookForm extends ActionForm<br />{<br />private String nTitle = null;<br />private String nAuthor = null;<br /><br />public String getTitle() { return nTitle; }<br />public void setTitle(String aTitle) { nTitle = aTitle; }<br /><br />public String getAuthor() { return nAuthor; }<br />public void setAuthor(String aAuthor) { nAuthor = aAuthor; }<br />public void reset(ActionMapping aMapping, HttpServletRequest aRequest)<br />{<br />nTitle = null;<br />nAuthor = null;<br />}<br />}<br /></pre><br />Enter the Java code in Notepad and save the file as BookForm.java. Compile the file from the command prompt and copy the class file in %TOMCAT_HOME%/webapps/struts-test/WEB-INF/classes/com.<br /><pre name="code" class="cpp"><br />package com;<br />import javax.servlet.*;<br />import javax.servlet.http.*;<br />import org.apache.struts.action.*;<br /><br />public class BookAction extends Action<br />{<br />public ActionForward perform(ActionMapping aMapping, ActionForm aForm,HttpServletRequest aRequest, HttpServletResponse aResponse)throws ServletException<br />{<br />BookForm f = (BookForm) aForm;<br />String title = f.getTitle();<br />String author = f.getAuthor();<br />return aMapping.findForward("saved");<br />}<br />}<br /></pre><br />Enter the Java code in Notepad and save the file as BookForm.java. Compile the file from the command prompt and copy the class file in %TOMCAT_HOME%/webapps/struts-test/WEB-INF/classes/com.<br /><br /><br />Enter the Java code in Notepad and save the file as BookAction.java. Compile the file from the command prompt and copy the class file in %TOMCAT_HOME%/webapps/struts-test/WEB-INF/classes/com.<br /><pre name="code" class="cpp"><br /><?xml version="1.0" encoding="ISO-8859-1" ?><br /><!DOCTYPE struts-config PUBLIC<br />"-//Apache Software Foundation//DTD Struts Configuration 1.0//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_0.dtd"><br /><struts-config><br /><!-- Form Bean Definitions --><br /><form-beans><br /><form-bean name = "bookForm" type = "com.bookForm"/><br /></form-beans><br /><!-- Global Forward Definitions --><br /><global-forwards><br /><forward name = "start" path = "/index.jsp"/><br /></global-forwards><br /><!-- Action Mapping Definitions --><br /><action-mappings><br /><action path = "/book" type = "com.bookAction" name = "bookForm"><br /><forward name = "saved" path = "/greeting.jsp"/><br /></action><br /></action-mappings><br /></struts-config><br /></pre><span id="fullpost"><span style="color: rgb(204, 0, 0);"></span><br /><br />Update the struts-config.xml file used in the Web application.<br /><br />The output of the program is as shown in Figure 16.1.<br /><br /><a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi6PLlomOHhY2kmmNv88UOSh1MX1D_xpN9j4zv-J4YPgJBnsC3nWHIg8rCFR2BhksPYy8Te9tyWOmzp7w4zeX0M2ghNwErisCs8aGMhq52T9xtO_MPcZYkUJ0xjwQJYPGpRJb8Ic9rfaYI/s1600-h/1.JPG"><img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 400px; height: 231px;" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi6PLlomOHhY2kmmNv88UOSh1MX1D_xpN9j4zv-J4YPgJBnsC3nWHIg8rCFR2BhksPYy8Te9tyWOmzp7w4zeX0M2ghNwErisCs8aGMhq52T9xtO_MPcZYkUJ0xjwQJYPGpRJb8Ic9rfaYI/s400/1.JPG" alt="" id="BLOGGER_PHOTO_ID_5349566924885827314" border="0" /></a><br /><div style="text-align: center;">Figure 16.1: Input page<br />After the user clicks the Submit button, the output of the program is as shown in Figure 16.2.<br /></div><div style="text-align: center;"><a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg-TmN20DIbtVeUCh__mVMoo7ahP6JiNGO0olsOZX4S9xwgOmLjDud7NWKxx8bqHKTaGwyWSGQ-wt-LsRBqpek8P0zVupasOKxEsrjw4Segy1EPCtEegFp4bybhpENHyYEeBJiMUEPhj_o/s1600-h/2.JPG"><img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 400px; height: 231px;" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg-TmN20DIbtVeUCh__mVMoo7ahP6JiNGO0olsOZX4S9xwgOmLjDud7NWKxx8bqHKTaGwyWSGQ-wt-LsRBqpek8P0zVupasOKxEsrjw4Segy1EPCtEegFp4bybhpENHyYEeBJiMUEPhj_o/s400/2.JPG" alt="" id="BLOGGER_PHOTO_ID_5349566926912900530" border="0" />Figure 16.2: Saved page</a><br /></div></span>adminhttp://www.blogger.com/profile/10028997688633545873noreply@blogger.com0tag:blogger.com,1999:blog-3319637562268350646.post-31766762344362802182009-05-06T09:09:00.000-07:002011-02-07T04:36:40.481-08:00User Login in JSPEvery website and software in the world is having login facility. Login gives access rights to user and defines their role in website and application. Nobody can access website if they failure in proving their identity on website or application.<br />Registration is first step by login to website. We will keep focus on only user login in JSP.<br /><br />User login contain two fields, first one important User ID. This is unique ID provided by site owner or software application itself or most of provide facility to choose user id themselves on their website of web application.<br /><br />Second is password, it is secret field and user have to keep remember without sharing with anybody. This field gives authentication to user to login on the website. User ID and password keep isolate one user to other users.<br /><br />We have three forms of JSP pages.<br /><br />login.jsp take input from user, mainly user id and password then submitted to server for further processing. This process handles with database. Database has a SQL table name usermaster. Usermaster table is having number of fields which are not using in login process. We need user id, password, user type, user level, first name, last name.<br /><br />User type field in database explain user type as e.g. admin role, power user role, moderator role, end user role. User levels field explain about permission defined to user. Read, write, update, view are permission on user can work accordingly to these permission. This certainly is not using in current login facility. This can be useful after user login successfully and work in application.<br /><br /><br />SQL usermaster Table<br /><pre name="code" class="cpp"><br /> CREATE TABLE `usermaster` (<br /> `sUserID` varchar(45) NOT NULL,<br /> `sEmail` varchar(250) NOT NULL,<br /> `sFirstName` varchar(45) NOT NULL,<br /> `sLastName` varchar(45) NOT NULL,<br /> `iDOB` datetime NOT NULL,<br /> `cGender` varchar(45) NOT NULL,<br /> `iCountryID` int(10) unsigned NOT NULL,<br /> `iCityID` varchar(45) NOT NULL,<br /> `iUserType` varchar(45) DEFAULT NULL,<br /> `iUserLevel` varchar(45) DEFAULT NULL,<br /> `sPassword` varchar(45) NOT NULL,<br /> `sForgetPassword` varchar(45) DEFAULT NULL,<br /> `sContact` bigint(20) unsigned NOT NULL,<br /> `sCreatedBy` varchar(45) DEFAULT NULL,<br /> `dCreatedDate` datetime DEFAULT NULL,<br /> `sModifiedBy` varchar(45) DEFAULT NULL,<br /> `sModifiedDate` datetime DEFAULT NULL,<br /> `sStatus` varchar(45) NOT NULL,<br /> PRIMARY KEY (`sUserID`),<br /> UNIQUE KEY `sEmail` (`sEmail`)<br /> );<br /></pre><br />login.jsp<br /><pre name="code" class="cpp"><br /> <%@ page contentType="text/html; charset=iso-8859-1" language="java" %><br /> <%<br /> String error=request.getParameter("error");<br /> if(error==null || error=="null"){<br /> error="";<br /> }<br /> %><br /> <html><br /> <head><br /> <title>User Login JSP</title><br /> <script><br /> function trim(s)<br /> {<br /> return s.replace( /^\s*/, "" ).replace( /\s*$/, "" );<br /> }<br /><br /> function validate()<br /> {<br /> if(trim(document.frmLogin.sUserName.value)=="")<br /> {<br /> alert("Login empty");<br /> document.frmLogin.sUserName.focus();<br /> return false;<br /> }<br /> else if(trim(document.frmLogin.sPwd.value)=="")<br /> {<br /> alert("password empty");<br /> document.frmLogin.sPwd.focus();<br /> return false;<br /> }<br /> }<br /> </script><br /> </head><br /><br /> <body><br /> <div><%=error%></div><br /> <form name="frmLogin" onSubmit="return validate();" action="doLogin.jsp" method="post"><br /> User Name <input type="text" name="sUserName" /><br /><br /> Password <input type="password" name="sPwd" /><br /><br /> <input type="submit" name="sSubmit" value="Submit" /><br /> </form><br /> </body><br /> </html><br /></pre><br />doLogin.jsp mainly deals with database to check user id and password is matched with user trying to provide to get access from the server.<br /><br />Our password field is encrypted with mysql password function. To decrypt password we have to use mysql password function again. If you are using Oracle or other database password function come with different name. Only user knows exact password and anybody can find out real password of the user. This increases the security of the system and reduces the hacking.<br /><br />doLogin.jsp<br /><pre name="code" class="cpp"><br /> <%@ page language="java" import="java.sql.*" errorPage="" %><br /> <%<br /> Connection conn = null;<br /> Class.forName("com.mysql.jdbc.Driver").newInstance();<br /> conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/database","root", "");<br /><br /> ResultSet rsdoLogin = null;<br /> PreparedStatement psdoLogin=null;<br /><br /> String sUserID=request.getParameter("sUserName");<br /> String sPassword=request.getParameter("sPwd");<br /> String message="User login successfully ";<br /><br /> try{<br /> String sqlOption="SELECT * FROM usermaster where"<br /> +" sUserID=? and sPassword=password(?) and sStatus=’A'";<br /><br /> psdoLogin=conn.prepareStatement(sqlOption);<br /> psdoLogin.setString(1,sUserID);<br /> psdoLogin.setString(2,sPassword);<br /><br /> rsdoLogin=psdoLogin.executeQuery();<br /><br /> if(rsdoLogin.next())<br /> {<br /> String sUserName=rsdoLogin.getString("sFirstName")+" "+rsdoLogin.getString("sLastName");<br /><br /> session.setAttribute("sUserID",rsdoLogin.getString("sUserID"));<br /> session.setAttribute("iUserType",rsdoLogin.getString("iUserType"));<br /> session.setAttribute("iUserLevel",rsdoLogin.getString("iUserLevel"));<br /> session.setAttribute("sUserName",sUserName);<br /><br /> response.sendRedirect("success.jsp?error="+message);<br /> }<br /> else<br /> {<br /> message="No user or password matched" ;<br /> response.sendRedirect("login.jsp?error="+message);<br /> }<br /> }<br /> catch(Exception e)<br /> {<br /> e.printStackTrace();<br /> }<br /><br /><br /> /// close object and connection<br /> try{<br /> if(psdoLogin!=null){<br /> psdoLogin.close();<br /> }<br /> if(rsdoLogin!=null){<br /> rsdoLogin.close();<br /> }<br /><br /> if(conn!=null){<br /> conn.close();<br /> }<br /> }<br /> catch(Exception e)<br /> {<br /> e.printStackTrace();<br /> }<br /><br /> %><br /></pre><br />doLogin.jsp match user id and password with database record. If record is matched with user field and password. It will set user id, user type, user level, first name, last name in session. This can access from session in further in application. It will finish processing and return to success.jsp page.<br /><br />success.jsp<br /><pre name="code" class="cpp"><br /><%@ page contentType="text/html; charset=iso-8859-1" language="java"%><br /><br /> <html><br /> <head><br /> <title>Successfully Login by JSP</title><br /> </head><br /> <body><br /> Successfully login by JSP<br /><br /> Session Value<br /><br /> <%<br /> out.print("UserName :"+session.getAttribute("sUserID")+"<br>");<br /> out.print("First & Last Name :"+session.getAttribute("sUserName"));<br /> %><br /> </body><br /> </html><br /></pre><br />If user id and password is not matched, it will return back to login.jsp page and print error message to user, user id and password is not matched.<br /><br />The example of login is given with source code, login.jsp, doLogin.jsp and success.jsp.adminhttp://www.blogger.com/profile/10028997688633545873noreply@blogger.com1tag:blogger.com,1999:blog-3319637562268350646.post-43317980627897640192009-05-04T06:52:00.000-07:002011-02-07T04:39:09.833-08:00JSP CheckBox<blockquote style="color: rgb(153, 0, 0);"><pre name="code" class="cpp"><br /><html><br /><h2>Select Languages:</h2><br /><form ACTION="jspCheckBox.jsp"><br /><input type="checkbox" name="id" value="Java"> Java<BR><br /><input type="checkbox" name="id" value=".NET"> .NET<BR><br /><input type="checkbox" name="id" value="PHP"> PHP<BR><br /><input type="checkbox" name="id" value="C/C++"> C/C++<BR><br /><input type="checkbox" name="id" value="PERL"> PERL <BR><br /><input type="submit" value="Submit"><br /></form><br /><%<br />String select[] = request.getParameterValues("id");<br />if (select != null && select.length != 0) {<br />out.println("You have selected: ");<br />for (int i = 0; i < select.length; i++) {<br />out.println(select[i]);<br />}<br />}<br />%><br /></html><br /></pre><br /></blockquote>Output will be displayed as:<br /><br /><a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://www.roseindia.net/jsp/checkbox1.gif"><img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 283px; height: 178px;" src="http://www.roseindia.net/jsp/checkbox1.gif" alt="" border="0" /></a><br />After selecting the languages, when you submit the button, the message will be displayed with selected languages:<br /><br /><a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://www.roseindia.net/jsp/checkbox2.gif"><img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 283px; height: 212px;" src="http://www.roseindia.net/jsp/checkbox2.gif" alt="" border="0" /></a>adminhttp://www.blogger.com/profile/10028997688633545873noreply@blogger.com1tag:blogger.com,1999:blog-3319637562268350646.post-23882996947885389712009-04-28T08:53:00.000-07:002011-02-07T04:40:55.545-08:00JSP Sessions ExampleSessions are started automatically in JSP pages.<br />You can access current session by prebuilt session object.<br /><br />Create sessions.jsp file on your Tomcat's webapps/ROOT directory :<br /><pre name="code" class="cpp"><br /><%@page import="java.util.Date"%><br /><%<br />out.print("Session Creation Time:" + new Date(session.getCreationTime()));<br />out.print("<BR>");<br />out.print("Last accessed Time:" + new Date(session.getLastAccessedTime()));<br />out.print("<BR>");<br />out.print("Session ID:"+session.getId());<br />%><br /></pre><br />and launch it (for example http://localhost:8080/sessions.jsp)<br /><br />it prints:<br /><br />Session Creation Time:Sat Jun 14 19:28:06 EEST 2008<br />Last accessed Time:Sat Jun 14 19:33:34 EEST 2008<br />Session ID:365A3F239D1E54FD43EA0F7CBA1931EF<br /><br />Now we are able to access the session. We can store objects in session objects.<br />This can help share objects between JSP pages.<br /><br />Sessions are widely used for authentication purposes.<br /><br />For example , let this be index.jsp as default page :<br /><pre name="code" class="cpp"><br /><%<br />String userName = request.getParameter("userName");<br />String password = request.getParameter("password");<br />if ("admin".equals(userName) && "adminpass".equals(password) ) { //Session start<br />session.setAttribute("userName",userName);<br />session.setAttribute("role","admin");<br />response.sendRedirect("/main.jsp");<br />}<br />else out.print("Logon failed");<br />%><br /><form action="index.jsp" method="post"><br />Enter Username: <input name="userName"> <br><br />Enter Password: <input name="password"> <br><br /><input type="submit"><br /></form><br /></pre><br />The sample code above get username and password from user. If username and password are ok , username and role objects are stored in session and redirects to main.jsp otherwise sends an http error.<br /><br />And in main.jsp we authenticate user based on username and role :<br /><pre name="code" class="cpp"><br /><%<br />String userName = session.getAttribute("userName");<br />String role = session.getAttribute("role");<br />if ( userName == null || ! "admin".equals(role) ) {<br />response.sendError(403,"You are not authorized to view this page");<br />return;<br />}<br />%><br /></pre><br />This authentication part can be a seperate jsp and included in every page that needs authentication. Ideally , username , password and role lookup should be from database or from a directory service like LDAP or Active Directory.<span id="fullpost"><br /></span>adminhttp://www.blogger.com/profile/10028997688633545873noreply@blogger.com0tag:blogger.com,1999:blog-3319637562268350646.post-89929434018424992262009-04-28T08:50:00.000-07:002011-02-07T04:42:09.794-08:00Simple JSP TutorialThis jsp accepts a number from an html form and prints squareroot of that number on the same page. Paste this code to file "first.jsp" and move it to tomcat's webapps/ROOT directory. Start tomcat and browse to localhost:8080/first.jsp. We put java code in <% %> block , other parts are standart html. <%=variable %> prints variable into html code just like <%out.print(variable);%> does.<br /><br />request , response and out objects are standart JSP objects that can be used to access http request and response.<br /><pre name="code" class="cpp"><br /><%@ page language="java" contentType="text/html; charset=ISO-8859-1"<br />pageEncoding="ISO-8859-1"%><br /><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><br /><html><br /><head><br /><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><br /><title>Sample JSP</title><br /></head><br /><body><br /><%<br />String strNumber= request.getParameter("number");<br />double mySqrt = 0.0;<br />if ( strNumber == null )<br />strNumber = "";<br />else {<br />double dNumber = Double.parseDouble(strNumber);<br />mySqrt = Math.sqrt(dNumber);<br />}<br />%><br /><form action="first.jsp" method="post"><br />Enter Number: <input type="text" name="number" value="<%=strNumber%>"><br /><input type="submit"><br /><br><br><br />The Sqrt is <%out.print(mySqrt);%><br /></body><br /></html><br /></pre>adminhttp://www.blogger.com/profile/10028997688633545873noreply@blogger.com0tag:blogger.com,1999:blog-3319637562268350646.post-63053991773142096372009-04-28T08:44:00.000-07:002011-02-07T04:43:31.413-08:00Sample JSP Date ComboThis method produces required html code for date time combos. Date parameter is selected in case of equality.<br /><pre name="code" class="cpp"><br /> public static String printDateCombos(String name , java.util.Date date) {<br /> Calendar today = Calendar.getInstance(new Locale("en-US")) ;<br /> if (date == null)<br /> today.setTimeInMillis( System.currentTimeMillis() );<br /> else<br /> today.setTime( date ) ;<br /> int year = today.get( Calendar.YEAR );<br /> int month = today.get( Calendar.MONTH );<br /> int day = today.get( Calendar.DAY_OF_MONTH );<br /> int hour = today.get( Calendar.HOUR_OF_DAY );<br /> int min = today.get( Calendar.MINUTE );<br /> StringBuffer out = new StringBuffer();<br /> out.append("<select name=\"");<br /> out.append(name);<br /> out.append("_day\">");<br /> for (int i=1;i<32;i++) {<br /> out.append("<option");<br /> if (i==day) out.append(" selected ");<br /> out.append(">");<br /> if (i<10) out.append('0');<br /> out.append(i);<br /> out.append("</option>");<br /> }<br /> out.append("</select>");<br /> // month<br /> out.append("/<select name=\"");<br /> out.append(name);<br /> out.append("_month\">");<br /> for (int i=0;i<12;i++) {<br /> out.append("<option");<br /> if (i==month) out.append(" selected");<br /> out.append(" value=");<br /> out.append(i+1);<br /> out.append(">");<br /> out.append( MONTHS[i] );<br /> out.append("</option>");<br /> }<br /> out.append("</select>");<br /> // year<br /> out.append("/<select name=\"");<br /> out.append(name);<br /> out.append("_year\">");<br /> for (int i=0;i<10;i++) {<br /> out.append("<option");<br /> if (i==0) out.append(" selected");<br /> out.append(">");<br /> out.append(year+i);<br /> out.append("</option>");<br /> }<br /> out.append("</select>");<br /> // hour<br /> out.append("-<select name=\"");<br /> out.append(name);<br /> out.append("_hour\">");<br /> for (int i=0;i<24;i++) {<br /> out.append("<option");<br /> if (i==hour) out.append(" selected");<br /> out.append(">");<br /> if (i<10) out.append('0');<br /> out.append(i);<br /> out.append("</option>");<br /> }<br /> out.append("</select>:");<br /> // minute<br /> out.append("<select name=\"");<br /> out.append(name);<br /> out.append("_minute\">");<br /> for (int i=0;i<60;i++) {<br /> out.append("<option");<br /> if (i==min) out.append(" selected");<br /> out.append(">");<br /> if (i<10) out.append('0');<br /> out.append(i);<br /> out.append("</option>");<br /> }<br /> out.append("</select>");<br /> return out.toString() ;<br /> }<br /></pre>adminhttp://www.blogger.com/profile/10028997688633545873noreply@blogger.com0tag:blogger.com,1999:blog-3319637562268350646.post-91786586178274971752009-04-27T09:59:00.000-07:002011-02-08T05:03:32.024-08:00Write a program to add a link requesting for an Account closure<span id="fullpost">Write a program to add a link requesting for an Account closure. 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 Account closure has been placed.<br /><br />Solution:<br /><br />The files used to run the application are:<br /><br />1. home.jsp<br />2. acclosure.jsp<br />3. MainServlet.java<br /><pre name="code" class="cpp"><br /> <html><br /> <head><br /> <title> home </title><br /> </head><br /> <body><br /> <%<br /> String userName = (String)session.getAttribute("UserName");<br /> %><br /> <h3 align ='center'>Welcome <%=userName%> </h3><br /><br /> While you are at the home page of MARKO Bank, please select<br /> any of the option given below<br><br /> <br><br><br /> <a href="redirecterServlet?action=Withdrawal">Withdrawal<a><br /> <br><br /> <a href="redirecterServlet?action=deposit">Deposit<a><br /> <br><br /> <a href="redirecterServlet?action=chq">Request for cheque<br /> book</a><br /> <br><br /> <a href="redirecterServlet?action=acclosure">Request for<br /> Account Closure</a><br /> <br><br /> </body><br /> </html><br /></pre><br />Update the home.jsp page with hyperlinks for requesting the chequebook and account closure. Save the file in %TOMCAT_HOME%/webapps/Application.<br /><pre name="code" class="cpp"><br /> <html><br /> <head><br /> <title> Account Closure </title><br /> </head><br /> <body><br /><br /> <%<br /> String userName = (String)session.getAttribute("UserName");<br /> java.util.Date d = new java.util.Date();<br /><br /> try<br /> {<br /> Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");<br /> java.sql.Connection connection =<br /> java.sql.DriverManager.getConnection("jdbc:odbc:userdb");<br /> java.sql.Statement statement =<br /> connection.createStatement();<br /> String query_car = "update userdetails set ClosureStatus<br /> ChqStatus= 'Requested for Account Closure on" +<br /> d.toString() + "' where UserName = '" + userName + "'" ;<br /> statement.execute(query_car);<br /> }<br /> catch(Exception e)<br /> {<br /> e.printStackTrace();<br /> }<br /> %><br /> <h1 align='center'>Your Request is Recorded on <%=d.toString()%>...</h1><br /> <br><br><br /> <a href="redirecterServlet?action=home">Marko Home</a><br><br /> <a href="redirecterServlet?action=Withdrawal">Withdrawal<a><br><br /> <a href="redirecterServlet?action=deposit">Deposit<a><br><br /> <a href="redirecterServlet?action=chq">Request for cheque book</a><br><br /> </body><br /> </html><br /></pre><br />Enter the code in Notepad and save the file as acclosure.jsp in %TOMCAT_HOME%/webapps/ Application.<br /><pre name="code" class="cpp"><br /> package MARKO;<br /> import java.io.*;<br /> import javax.servlet.*;<br /> import javax.servlet.http.*;<br /> public class MainServlet extends HttpServlet<br /> {<br /> public void doPost(HttpServletRequest request,<br /> HttpServletResponse response) throws ServletException,<br /> IOException {doGet(request,response);<br /> }<br /> public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException<br /> {<br /><br /> PrintWriter out = response.getWriter();<br /> response.setContentType("text/html");<br /> String action;<br /> HttpSession ses = request.getSession(true);<br /> action = request.getParameter("action");<br /> if (action == null)<br /> {<br /> return;<br /> }<br /> else if (action.equals("home"))<br /> {<br /> RequestDispatcher dis =<br /> request.getRequestDispatcher("/home.jsp");<br /> dis.include(request, response);<br /> }<br /> else if (action.equals("chq"))<br /> {<br /> RequestDispatcher dis =<br /> request.getRequestDispatcher("/chq.jsp");<br /> dis.include(request, response);<br /> }<br /> else if (action.equals("acclosure"))<br /> {<br /> RequestDispatcher dis =<br /> request.getRequestDispatcher("/acclosure.jsp");<br /> dis.include(request, response);<br /> }<br /> else if (action.equals("Withdrawal"))<br /> {<br /> RequestDispatcher dis =<br /> request.getRequestDispatcher("/Withdrawal.jsp");<br /> //Withdrawal.jsp is created in LG<br /> dis.include(request, response);<br /> }<br /> else if (action.equals("deposit"))<br /> {<br /> RequestDispatcher dis =<br /> request.getRequestDispatcher("/deposit.jsp");<br /> //deposit.jsp is created in LG<br /> dis.include(request, response);<br /> }<br /> else<br /> {<br /> out.println("Error in Accessing the Site");<br /> }<br /> out.close();<br /> }<br /> }<br /></pre><br />Update the MainServlet.java page, to redirect the request to the new chequebook page. Save the file in %TOMCAT_HOME%/webapps/Application.<br /><br />The output of the program is as shown in the Figure 15.1.<br /><br /><br /><div style="text-align: center;"><a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi6Ls31gKhtyjlgI1EY1MTVEJAs6CnKBggUudDaY0wfHA2ZX6_Db2YKylaE7oOdqwsmgp_6r35cDAw51kvRn7AXjo1KkaFDS3erpq7JiQPqqi7-6vjKh6dCBCUjSkVXOuvfU1My9sdsFK8/s1600-h/11.JPG"><img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 400px; height: 282px;" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi6Ls31gKhtyjlgI1EY1MTVEJAs6CnKBggUudDaY0wfHA2ZX6_Db2YKylaE7oOdqwsmgp_6r35cDAw51kvRn7AXjo1KkaFDS3erpq7JiQPqqi7-6vjKh6dCBCUjSkVXOuvfU1My9sdsFK8/s400/11.JPG" alt="" id="BLOGGER_PHOTO_ID_5329418347666243570" border="0" /></a>Figure 15.1: Welcome Page<br /></div>The output of the user request for the new chequebook is shown in Figure 15.2.<br /><br /><div style="text-align: center;"><a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiN1f-TwTrIVoNilOqfWnS10GXBdSOPBI3V-k8Oy5pMO_Lq4ky4nqGqmOhkdVD1pHJ-B-gFrLQJOBWn6HtzYLZNkAkArVCehXI8yVedVP0NOcO4t0rzM8-q_OV5AcGUfxqffO8dfRLV-fQ/s1600-h/12.JPG"><img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 400px; height: 282px;" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiN1f-TwTrIVoNilOqfWnS10GXBdSOPBI3V-k8Oy5pMO_Lq4ky4nqGqmOhkdVD1pHJ-B-gFrLQJOBWn6HtzYLZNkAkArVCehXI8yVedVP0NOcO4t0rzM8-q_OV5AcGUfxqffO8dfRLV-fQ/s400/12.JPG" alt="" id="BLOGGER_PHOTO_ID_5329419331070773810" border="0" /></a>Figure 15.2: Request ChequeBook Screen<br /></div><br />The output of the user request for the account closure is shown in Figure 15.3.<br /><br /><div style="text-align: center;"><a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg-jjsDzy0umEKAS2AiF57hCKp_xR_3OlsxlUr3Vu7pyZ0vLYjIaxh7NtgLHewb_lCtSBaClRzoSzGr4pMqKskJ_BDqYlP_p6ep4FDPTwqxeVoyKGs3fRZ_ZbOKKwY-JfgpAbpbNM19vIQ/s1600-h/13.JPG"><img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 400px; height: 280px;" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg-jjsDzy0umEKAS2AiF57hCKp_xR_3OlsxlUr3Vu7pyZ0vLYjIaxh7NtgLHewb_lCtSBaClRzoSzGr4pMqKskJ_BDqYlP_p6ep4FDPTwqxeVoyKGs3fRZ_ZbOKKwY-JfgpAbpbNM19vIQ/s400/13.JPG" alt="" id="BLOGGER_PHOTO_ID_5329418347915002706" border="0" /></a>Figure 15.3: Request Account Closure Screen<br /></div></span>adminhttp://www.blogger.com/profile/10028997688633545873noreply@blogger.com0tag:blogger.com,1999:blog-3319637562268350646.post-57609395082153121452009-04-27T09:37:00.000-07:002011-02-08T05:06:37.128-08:00Write a program to add a link requesting for a bank chequebookWrite 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.<br /><br />Solution:<br />The files used to run the application are:<br />1. home.jsp<br />2. chq.jsp<br />3. acclosure.jsp<br />4. MainServlet.java<br /><pre name="code" class="cpp"><br /> <html><br /> <head><br /> <title> Home </title><br /> </head><br /> <body><br /> <%<br /> String userName = (String)session.getAttribute("UserName");<br /> %><br /> <h3 align ='center'>Welcome <%=userName%> </h3><br /> While you are at the home page of MARKO Bank, please select<br /> any of the options given below<br><br /> <br><br><br /> <a href="redirecterServlet?action=Withdrawal">Withdrawal<a><br /> <br><br /> <a href="redirecterServlet?action=deposit">Deposit<a><br><br /> <a href="redirecterServlet?action=chq">Request for cheque<br /> book</a><br /> <br><br /> </body><br /> </html><br /></pre><br />Update the home.jsp page with hyperlinks for requesting the chequebook and account closure. Save the file in %TOMCAT_HOME%/webapps/Application.<br /><pre name="code" class="cpp"><br /> <html><br /> <head><br /> <title> checque </title><br /> </head><br /> <body><br /> <%<br /> String userName = (String)session.getAttribute("UserName");<br /> java.util.Date date = new java.util.Date();<br /> try<br /> {<br /> Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");<br /> java.sql.Connection connection =<br /> java.sql.DriverManager.getConnection("jdbc:odbc:userdb<br /> ");<br /> java.sql.Statement statement =<br /> connection.createStatement();<br /> String query_car = "update userdetails set ChqStatus =<br /> 'Requested for checque on" + date.toString() + "'<br /> where UserName = '" + userName + "'" ;<br /> statement.execute(query_car);<br /> }<br /> catch(Exception e)<br /> {<br /> e.printStackTrace();<br /> }<br /> %><br /> <h1 align='center'>Your Request is Recorded on <%=date.toString()%>...</h1><br /> <br><br><br /> <a href="redirecterServlet?action=home">Marko Home</a><br><br /> <a href="redirecterServlet?action=Withdrawal">Withdrawal<a><br /> <br><br /> <a href="redirecterServlet?action=deposit">Deposit<a><br /> <br><br /> </body><br /> </html><br /></pre><br />Enter the code in Notepad and save the file as chq.jsp in %TOMCAT_HOME%/webapps/ Application.<br /><pre name="code" class="cpp"><br /> package MARKO;<br /> import java.io.*;<br /> import javax.servlet.*;<br /> import javax.servlet.http.*;<br /> <br /> public class MainServlet extends HttpServlet<br /> {<br /> public void doPost(HttpServletRequest request,<br /> HttpServletResponse response) throws ServletException,<br /> IOException {doGet(request,response);<br /> }<br /><br /> public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException<br /> {<br /> PrintWriter out = response.getWriter();<br /> response.setContentType("text/html");<br /> String action;<br /> HttpSession ses = request.getSession(true);<br /> action = request.getParameter("action");<br /> if (action == null)<br /> {<br /> return;<br /> }<br /> else if (action.equals("home"))<br /> {<br /> RequestDispatcher dis =<br /> request.getRequestDispatcher("/home.jsp");<br /> dis.include(request, response);<br /> }<br /> else if (action.equals("chq"))<br /> {<br /> RequestDispatcher dis =<br /> request.getRequestDispatcher("/chq.jsp");<br /> dis.include(request, response);<br /> }<br /> else if (action.equals("Withdrawal"))<br /> {<br /> RequestDispatcher dis =<br /> request.getRequestDispatcher("/Withdrawal.jsp");<br /> dis.include(request, response);<br /> }<br /> else if (action.equals("deposit"))<br /> {<br /> RequestDispatcher dis =<br /> request.getRequestDispatcher("/deposit.jsp");<br /> dis.include(request, response);<br /> }<br /> else<br /> {<br /> out.println("Error in Accessing the Site");<br /> }<br /> out.close();<br /> }<br /> }<br /></pre><br />Update the MainServlet.java page to redirect the request to the new chequebook page. Save the file in %TOMCAT_HOME%/webapps/Application.<br /><br /><br /><span id="fullpost"><div style="text-align: center;"><div style="text-align: left;"><br /></div></div><br /></span>adminhttp://www.blogger.com/profile/10028997688633545873noreply@blogger.com0tag:blogger.com,1999:blog-3319637562268350646.post-50853661023950811962009-04-19T12:01:00.000-07:002011-02-08T05:10:19.512-08:00Write a program to display the user details, with delete button for each detailWrite a program to display the user details, with delete button for each detail. The details get deleted from the database after clicking the corresponding delete button.<br /><br />The files used to run the application are:<br />1. delete.jsp<br />2. Main.jsp<br />Solution:<br />delete.jsp<br /><pre name="code" class="cpp"><br /> <html><br /> <head><title>Details</title></head><br /> <%@ page import="java.io.*, java.sql.*"%><br /> <body><br /> <center><br /> <H3>Details</h3><br /> <table border="1"><br /> <tr><th>CusNr</th><th>First Name</th><th>Last Name</th><th>Email</th><th>Delete</th></tr><br /> <%<br /> try<br /> {<br /> Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");<br /> Connection connection =<br /> DriverManager.getConnection("jdbc:odbc:Details");<br /> Statement statement = connection.createStatement();<br /> String query = "Select * from UserDetails";<br /> ResultSet resCar = statement.executeQuery(query);<br /> while(res.next())<br /> {<br /> int cno = resCar.getInt("CusNr");<br /> String first = res.getString("Fname");<br /> String last = res.getString("Lname");<br /> String email = res.getString("Email");<br /> %><br /> <TR><br /> <TD><%= cno %></TD><br /> <TD><%= first %></TD><br /> <TD><%= last %></TD><br /> <TD><%= email %></TD><br /> <TD><A HREF='Main.jsp?cusnr=<%= cno<br /> %>'>Delete</A></TD><br /> </TR><br /> <%<br /> }// end while loop<br /> }<br /> catch (ClassNotFoundException cnfe)<br /> {<br /> System.err.println(cnfe);<br /> }<br /> catch (SQLException ex )<br /> {<br /> System.err.println( ex);<br /> }<br /> catch (Exception er)<br /> {<br /> er.printStackTrace();<br /> }<br /> %><br /> </table><br /> </center><br /> </body><br /> </html><br /></pre> <br />Main.jsp<br /><pre name="code" class="cpp"><br /> <html><br /> <head><title>Details</title></head><br /> <%@ page import="java.io.*, java.sql.*"%><br /> <body><br /> <center><br /> <%<br /> String inCusNr = request.getParameter("cusnr");<br /> String delete_cus = "delete from UserDetails where CusNr="+inCusNr;<br /> %><br /><br /> <%<br /> try<br /> {<br /> Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");<br /> Connection connection =<br /> DriverManager.getConnection("jdbc:odbc:banking");<br /> Statement statement = connection.createStatement();<br /> int rowsGone = statement.executeUpdate(delete_cus);<br /> if (rowsGone==1)<br /> {<br /> %><br /> <H2>Details of User <%= inCusNr %> deleted.</H2><br /> <%<br /> }<br /> else<br /> {<br /> %><br /> <h2>Insertion failure</h2><br /> <%<br /> }<br /> }<br /> catch (ClassNotFoundException cnfe)<br /> {<br /> System.err.println(cnfe);<br /> }<br /> catch (SQLException ex )<br /> {<br /> System.err.println( ex);<br /> }<br /> catch (Exception er)<br /> {<br /> er.printStackTrace();<br /> }<br /> %><br /> </center><br /> </body><br /> </html><br /></pre><br /><span id="fullpost">The output of the program is as shown in the Figure 14.6.<br /><div style="text-align: center;"><a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjkApPCfq4ePpMnvWVOe-OSX5OAWbEwGFxiG4kGI4cLxaVxDa-QRpM6f0ipWxg72ONUKEqmbvrfVwyCevewdvZNHlhsdYWqVvBhEx9XkPhD9pfm8na73uNpibCOTCY8GVRUS6e2WvUO6t0/s1600-h/3.JPG"><img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 400px; height: 306px;" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjkApPCfq4ePpMnvWVOe-OSX5OAWbEwGFxiG4kGI4cLxaVxDa-QRpM6f0ipWxg72ONUKEqmbvrfVwyCevewdvZNHlhsdYWqVvBhEx9XkPhD9pfm8na73uNpibCOTCY8GVRUS6e2WvUO6t0/s400/3.JPG" alt="" id="BLOGGER_PHOTO_ID_5326480780951044706" border="0" /></a>Figure 14.6: User Details in Database<br /></div><br />When the user clicks on Delete button for any detail, the corresponding user detail will be deleted from the database, and a message will be displayed to the user, as shown in Figure 14.7.<br /><br /><div style="text-align: center;"><a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhxRcgHOfoS0malC62C5x4pbXOZWWmMEm2IuAyS2TFr-dz2oV_iK0jhnw8EB-2wde_NgyCr5GkPrJ-eE3I2W_5UMToGuvdQVRNLQThuZq7gaDS0X5zoM_fX83yF4Lack4hOi7OQhKNJI5o/s1600-h/4.JPG"><img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 400px; height: 368px;" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhxRcgHOfoS0malC62C5x4pbXOZWWmMEm2IuAyS2TFr-dz2oV_iK0jhnw8EB-2wde_NgyCr5GkPrJ-eE3I2W_5UMToGuvdQVRNLQThuZq7gaDS0X5zoM_fX83yF4Lack4hOi7OQhKNJI5o/s400/4.JPG" alt="" id="BLOGGER_PHOTO_ID_5326480778487819218" border="0" /></a>Figure 14.7: Deletion Message<br /><div style="text-align: left;"><br /></div></div></span>adminhttp://www.blogger.com/profile/10028997688633545873noreply@blogger.com0tag:blogger.com,1999:blog-3319637562268350646.post-35839199945576349302009-04-19T11:53:00.000-07:002011-02-08T05:14:45.878-08:00Write a program to display a user details form to the userWrite a program to display a user details form to the user. After the user clicks Submit button, the details entered should be saved in the database. Display a message to the user after the data is saved to the database. The DSN name is user. The database name is Details. Identify the structure of UserDetails table:<br /><br />CNo Number,<br />Fname Text,<br />Lname Text,<br />Email Text<br /><br />The files used to run the application are:<br /><br />1. Details.jsp<br />2. insert.jsp<br /><br />Solution:<br />Details.jsp<br /><pre name="code" class="cpp"><br /> <html><br /> <head><br /> <title>Add Customer Details</title><br /> </head><br /><br /> <body><br /> <h1> User Details</h1><br /> <form action="insert.jsp" method="post"><br /> <table><br /> <tr><br /> <td align="right">First Name:</td><br /> <td><input type="text" name="first" size="30"></td><br /> </tr><br /> <tr><br /> <td>Last Name:</td><br /> <td> <input type="text" name="last" size="30" /></td><br /> </tr><br /><br /> <tr><br /> <td>Email:</td><br /> <td> <input type="text" name="email" size="30" /></td><br /> </tr><br /> </table><br/><br /> <input type="submit" value="Submit" /><br /> </form><br /> </body><br /> </html><br /></pre><br />insert.jsp<br /><pre name="code" class="cpp"><br /> <html><br /> <head><title>Adding customer details</title></head><br /> <%@ page import="java.io.*, java.sql.*"%><br /> <body><br /> <center><br /><br /> <%<br /> String first = request.getParameter("first");<br /> String last = request.getParameter("last");<br /> String email = request.getParameter("email");<br /> try<br /> {<br /> Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");<br /> Connection connection =<br /> DriverManager.getConnection("jdbc:odbc:Details");<br /><br /> Statement Stmt = connection.createStatement();<br /> String insert = "insert into UserDetails values ('" + first + "','" + last + "','" + email + "');";<br /><br /> int stmtInt = Stmt.executeUpdate(insert);<br /> out.println(“Your Information is Added in our<br /> Database”);<br /> %><br /><br /> <%<br /> }<br /> catch (ClassNotFoundException cnfe)<br /> {<br /> System.err.println(cnfe);<br /> }<br /> catch (SQLException ex )<br /> {<br /> System.err.println( ex);<br /> }<br /> catch (Exception er)<br /> {<br /> er.printStackTrace();<br /> }<br /> %><br /> </body><br /> </html><br /></pre><span id="fullpost"><span style="text-decoration: underline;"><br /></span>The output of the program is as shown in the Figure 14.4.<br /><span id="fullpost"><br /><div style="text-align: center;"><a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgD1TpXoxaCRGi2c7xCOBM5m56TKpr-CXc0VQ7fCfUm-XZw5Eq4RB3FSRKieWnEirY7zPYBmnVP1JFEhVCBGFh2nVR-Mlacl8jGwrlnQzHcYmvZSEnKS609sUQfzWMnbP6rJWQQ1TUh5Bw/s1600-h/3.JPG"><img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 400px; height: 369px;" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgD1TpXoxaCRGi2c7xCOBM5m56TKpr-CXc0VQ7fCfUm-XZw5Eq4RB3FSRKieWnEirY7zPYBmnVP1JFEhVCBGFh2nVR-Mlacl8jGwrlnQzHcYmvZSEnKS609sUQfzWMnbP6rJWQQ1TUh5Bw/s400/3.JPG" alt="" id="BLOGGER_PHOTO_ID_5326478206455508226" border="0" /></a>Figure 14.4: User Details Form<br /></div><br />After entering the details, when the user clicks on Submit button, the details are saved in database, and a message is displayed to the user as shown in Figure 14.5.<br /><br /><div style="text-align: center;"><a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhcOFGFBfGmPHVyp38A-PV6kVFPi5oa7l2W62hOZWPLSPZudFNiu4ic2LQ8F-08Ia9Bhb9mdbaN1BIIyxyaol37tGEmQoDkZxUBJ6d5j56Zq20InI69_hy3dSdRiaeD8oi3glZ7po_tnqg/s1600-h/4.JPG"><img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 400px; height: 368px;" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhcOFGFBfGmPHVyp38A-PV6kVFPi5oa7l2W62hOZWPLSPZudFNiu4ic2LQ8F-08Ia9Bhb9mdbaN1BIIyxyaol37tGEmQoDkZxUBJ6d5j56Zq20InI69_hy3dSdRiaeD8oi3glZ7po_tnqg/s400/4.JPG" alt="" id="BLOGGER_PHOTO_ID_5326478207508157378" border="0" /></a>Figure 14.5: Message<br /><div style="text-align: left;"><br /></div></div></span></span>adminhttp://www.blogger.com/profile/10028997688633545873noreply@blogger.com0tag:blogger.com,1999:blog-3319637562268350646.post-30811898047029296412009-04-19T11:40:00.000-07:002011-02-08T05:16:48.824-08:00Write a program to display a login page to the userWrite a program to display a login page to the user. The login page should ask user to enter the account Id and pin number. The account id and pin number should be compared with the data in database, and should be validated. Display appropriate message to the user after validating the user input to the login form.<br /><br />The files used to run the application are:<br /><br />1. main.jsp<br />2. Login.jsp<br />3. process2.jsp<br />4. success.jsp<br />5. retry.jsp<br /><br />Solution:<br />main.jsp<br /><pre name="code" class="cpp"><br /> <html><br /> <head><br /> <title> </title><br /> </head><br /> <body><br /> <br><br /> <br><br><br /> <form action="process2.jsp” method = "post" ><br /> <center>Account Id</center><br /> <input type = "text" name="acc_id><br /> <center>Pin Number</center><br /> <input type = "Pin Number" name = "pin_num"><br /> <center><input type="submit" name="Submit" value="Login"></center><br /> </form><br /> </body><br /> </html><br /></pre><br />Login.java<br /><pre name="code" class="cpp"><br /> package Java_class;<br /> import java.sql.*;<br /> public class Login<br /> {<br /> private String account_id = "";<br /> private String pin_number = "";<br /> public Login()<br /> {<br /> }<br /> public void setaccount_id(String acc_id)<br /> {<br /> this.acc_id = acc_id;<br /> }<br /> public void setPin_num(String pin_number)<br /> {<br /> this.pin_number = pin_number;<br /> }<br /> public boolean authenticate(String acc_id2, String pin_num2)<br /> {<br /> String query="select * from Registration";<br /> String Dbacc_id="";<br /> String DbPin_num="";<br /><br /> try<br /> {<br /> Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");<br /> Connection<br /> con=DriverManager.getConnection("jdbc:odbc:regist<br /> er");<br /> Statement stat=con.createStatement();<br /> ResultSet rst=stat.executeQuery(query);<br /> while(rst.next())<br /> {<br /> Dbacc_id=rst.getString("account_id");<br /> DbPin_num=rst.getString("pin_number");<br /><br /> if (acc_id2.equals(Dbacc_id) &&<br /> pin_num2.equals(DbPin_num))<br /> {<br /> return true;<br /> // break;<br /> }<br /> }<br /> return false;<br /> }<br /> catch(Exception e)<br /> {<br /> e.printStackTrace();<br /> return false;<br /> }<br /> }<br /> }<br /><br /></pre><br />process2.jsp<br /><pre name="code" class="cpp"><br /> <%@ page import="java.util.*" %><br /> <jsp:useBean id="idHandler" class="Java_class.Login" scope="request"><br /> <jsp:setProperty name="idHandler" property="*"/><br /> </jsp:useBean><br /><br /> <%<br /> String username = request.getParameter("account_id");<br /> String password = request.getParameter("pin_number");<br /><br /> // If authenticated pass control to success.jsp<br /> if (idHandler.authenticate(account_id, pin_number))<br /> {<br /> %><br /> <jsp:forward page="success.jsp"/><br /> <%<br /> } else {<br /> %><br /> <jsp:forward page="retry.jsp"/><br /> <%<br /> }<br /> %><br /> //success.jsp<br /> <html><br /> <head><br /> <title> User Validation Page </title><br /> </head><br /> <body><br /> You have successfully logged in to our Website<br /> </body><br /> </html><br /></pre><br />retry.jsp<br /><pre name="code" class="cpp"><br /> <html><br /> <head><br /> <title> User Validation Page </title><br /> </head><br /> <body><br /> Incorrect username or password!!!!<br /> <A href="main.jsp"> Retry </A><br /> </body><br /> </html><br /></pre><span style="text-decoration: underline;"></span><span id="fullpost"><span style="text-decoration: underline;"><br /></span>The output of the program is as shown in Figure 14.1.<br /><br /><div style="text-align: center;"><a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgFWZlI0leaa_jEbY7kAMpuB5BhrGFNz-BDtkhmF7dCdq3s3MFJtbLk6l2B7L8e_MEm9qyyGZMUTXP01KBGWVzlHjiVOLRljvKVL7PPqHRw40_My-2IxPcMh8_pWb3qg0aBg3rFlpUUar0/s1600-h/6.JPG"><img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 400px; height: 326px;" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgFWZlI0leaa_jEbY7kAMpuB5BhrGFNz-BDtkhmF7dCdq3s3MFJtbLk6l2B7L8e_MEm9qyyGZMUTXP01KBGWVzlHjiVOLRljvKVL7PPqHRw40_My-2IxPcMh8_pWb3qg0aBg3rFlpUUar0/s400/6.JPG" alt="" id="BLOGGER_PHOTO_ID_5326476963920827954" border="0" /></a>Figure 14.1: Login Form<br /></div><br />After entering valid details, when the user clicks on Login button, a message is displayed to the user as shown in Figure 14.2.<br /><br /><div style="text-align: center;"><a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhrAS4RkEtyQg5Dy5cowNJHWavzT2SfJlEZi2JxV5z2OFngS2gINyJw612nOkxiVURNgRZMfHBAs_rLZ1LXBFSv8_Xn9HSlsOtA549OqqgoWvRkEH-Y8_bMd22PmIweA9pvJnTqfPqDNbI/s1600-h/7.JPG"><img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 400px; height: 325px;" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhrAS4RkEtyQg5Dy5cowNJHWavzT2SfJlEZi2JxV5z2OFngS2gINyJw612nOkxiVURNgRZMfHBAs_rLZ1LXBFSv8_Xn9HSlsOtA549OqqgoWvRkEH-Y8_bMd22PmIweA9pvJnTqfPqDNbI/s400/7.JPG" alt="" id="BLOGGER_PHOTO_ID_5326476960814521138" border="0" /></a>Figure 14.2: Logon Success Message<br /></div><br />If the user enters invalid details in the login form, a message is displayed to the user informing that the details entered are invalid. The message appears as shown in Figure 14.3.<br /><br /><div style="text-align: center;"><a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgawxfJiT96UaNUn2tJV_yaI-fzb5awoOdJzDJhEwf9D8HzSLFVd4lYNW7cKZWy_byjBLozWCvtZISeLvRe9morXeU36dS7PTn9YsQAdotn7QfMd0nzFVGdtdMWqkhY2dRYZ4safRB1ROE/s1600-h/8.JPG"><img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 400px; height: 325px;" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgawxfJiT96UaNUn2tJV_yaI-fzb5awoOdJzDJhEwf9D8HzSLFVd4lYNW7cKZWy_byjBLozWCvtZISeLvRe9morXeU36dS7PTn9YsQAdotn7QfMd0nzFVGdtdMWqkhY2dRYZ4safRB1ROE/s400/8.JPG" alt="" id="BLOGGER_PHOTO_ID_5326476956999908946" border="0" /></a>Figure 14.3: Invalid Login Message<br /><div style="text-align: left;"><br /></div></div></span>adminhttp://www.blogger.com/profile/10028997688633545873noreply@blogger.com0tag:blogger.com,1999:blog-3319637562268350646.post-92014604203987542422009-04-19T11:36:00.000-07:002009-08-03T01:44:28.665-07:00Write a program to count and display the number of active sessions connected to the Tomcat server<span style="color: rgb(255, 0, 0);">Write a program to count and display the number of active sessions connected to the Tomcat server.</span><br /><span id="fullpost"><br />Solution:<br /><br />The files used in this exercise are:<br /><br />1. session.jsp<br />2. web.xml<br />3. SessionCount.java<br /><span style="text-decoration: underline;"><blockquote style="color: rgb(153, 0, 0);"><html><br /><head><br /><title>Session</title><br /></head><br /><body><br /><h1>Session</h1><br />There are currently<br /><%=com.java2s.SessionCount.getNumberOfSessions()%> active sessions.<br /></body><br /></html></blockquote><br /></span>Enter the above code in Notepad, and save the file as ‘Session.jsp’ in %TOMCAT_HOME%/webapps/ counter.<br /><span style="text-decoration: underline;"><blockquote style="color: rgb(153, 0, 0);"><?xml version="1.0" encoding="UTF-8"?><br /><!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"<br /> "http://java.sun.com/dtd/web-app_2_3.dtd"><br /><web-app><br /> <listener><br /> <listener-class>com.SessionCount</listener-class><br /> </listener><br /> <taglib><br /> <taglib-uri>http://java.sun.com/jstl/core</taglib-uri><br /> <taglib-location>/WEB-INF/c.tld</taglib-location><br /> </taglib><br /></web-app></blockquote><br /></span>Enter the code in Notepad, and save the file as ‘web.xml’ in %TOMCAT_HOME%/webapps/ counter/WEB-INF.<br /><span style="text-decoration: underline;"><blockquote style="color: rgb(153, 0, 0);">package com;<br />import javax.servlet.http.*;<br />public class SessionCount implements HttpSessionListener<br />{<br /> private static int numberOfSessions = 0;<br /> public void sessionCreated (HttpSessionEvent evt)<br /> {<br /> numberOfSessions++;<br /> }<br />public void sessionDestroyed (HttpSessionEvent evt)<br /> {<br /> numberOfSessions--;<br /> }<br /> public static int getNumberOfSessions()<br /> {<br /> return numberOfSessions;<br /> }<br />}</blockquote></span>Enter the above Java code in Notepad, and save the file as ‘SessionCount.java’. Compile the file from command prompt, and copy the class file in %TOMCAT_HOME%/webapps/counter/ WEB-INF/classes/com.<br /><br />The output of the program is as shown in the Figure 12.3.<br /><br /><div style="text-align: center;"><a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgCckJh0QR0BVq8wmGSl-fYj5erYcGnEFS0iBiZ5K1v-jrq5HMB-ZC3R6AHXBrd4H4Y1hYcd5JFxQ3ASrO9LZyAFv3TFgTHyJkxzHw0Bm6vTxjj8snpUUyS624YZRYCMQseTUIoq9OuRhA/s1600-h/4.JPG"><img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 400px; height: 286px;" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgCckJh0QR0BVq8wmGSl-fYj5erYcGnEFS0iBiZ5K1v-jrq5HMB-ZC3R6AHXBrd4H4Y1hYcd5JFxQ3ASrO9LZyAFv3TFgTHyJkxzHw0Bm6vTxjj8snpUUyS624YZRYCMQseTUIoq9OuRhA/s400/4.JPG" alt="" id="BLOGGER_PHOTO_ID_5326473743518572594" border="0" /></a>Figure 12.3: Counter page<br /><div style="text-align: left;"><br /></div></div></span>adminhttp://www.blogger.com/profile/10028997688633545873noreply@blogger.com0tag:blogger.com,1999:blog-3319637562268350646.post-30470013371871978662009-04-19T11:30:00.000-07:002009-08-03T01:43:46.030-07:00Write a program to display an online shopping cart Web page with a drop down menu<span style="color: rgb(255, 0, 0);">Write a program to display an online shopping cart Web page with a drop down menu. The Web page should have one button to add an item to the cart, and another button to remove an item from the cart. The Web page should display the changes made to the cart.</span><br /><span id="fullpost"><br />Solution:<br />The file used in this exercise is cart.jsp<br /><span style="text-decoration: underline;"><blockquote style="color: rgb(153, 0, 0);"><html><br /> <%<br /> java.util.Vector v = (java.util.Vector)session.getAttribute("array");<br /> if (v == null)<br /> {<br /> v = new java.util.Vector(); <br /> }<br /> String i = null;<br /> String submit = request.getParameter("submit");<br /><br /> if (submit == null)<br /> {<br /> submit = "";<br /> }<br /> if (submit.equals("add") || submit.equals(""))<br /> {<br /> v.addElement(request.getParameter("item"));<br /> %><br /> <br> Your cart Contains :<br /> <ol><br /> <%<br /> String[] items = new String[v.size()];<br /> v.copyInto(items);<br /> for (int ix=1; ix < items.length; ix++) {<br /> %><br /> <li> <% out.print(items[ix]);<br /> }<br /> %><br /> </ol><br /><br /> <%<br /> }<br /> if (submit.equals("remove"))<br /> {<br /> String removeitem=request.getParameter("item");<br /> if(v.contains(removeitem))<br /> {<br /> v.removeElement(removeitem);<br /> }<br /> else<br /> {<br /> out.println("element not found in vector");<br /> }<br /> <br /> %> <br /> <br> Your cart Contains :<br /> <ol><br /> <%<br /> String[] items = new String[v.size()];<br /> v.copyInto(items);<br /> for (int ix=1; ix<items.length; ix++) {<br /> %><br /> <li> <% out.print(items[ix]);<br /> }<br /> %><br /> </ol><br /><br /> <%<br /> }<br /> session.setAttribute("array",v); <br />%><br /></font><br /><hr><br /><font size = 3><br /><form type=POST><br /><BR><br />Please Select the item to add or remove:<br /><br><br />Add / Remove Item:<br /><select name="item"><br /><option>Floppy<br /><option>CD<br /><option>Keyboard<br /></select><br /><br> <br><br /><input type=submit name="submit" value="add"><br /><input type=submit name="submit" value="remove"><br /></form><br /></font><br /></html></blockquote><br /></span>Enter the code in Notepad, and save the file as Cart.jsp in %TOMCAT_HOME%/webapps/ session.<br /><br />The output of the program is as shown in Figure 12.1.<br /><br /><div style="text-align: center;"><a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjr1UjLAqvN87REzftBtFBZausk_c2zJzEtdTRGmm8aXmAy1fQ2IE0liv-L8OXyza6iQ4E4dZkr-Mlf_eT7CJfXjmlZ5UDZXngcJQwLzy_TbKnt6l7SWP5nBVaOjjG0nSo8pVVkAneSptY/s1600-h/3.JPG"><img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 400px; height: 288px;" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjr1UjLAqvN87REzftBtFBZausk_c2zJzEtdTRGmm8aXmAy1fQ2IE0liv-L8OXyza6iQ4E4dZkr-Mlf_eT7CJfXjmlZ5UDZXngcJQwLzy_TbKnt6l7SWP5nBVaOjjG0nSo8pVVkAneSptY/s400/3.JPG" alt="" id="BLOGGER_PHOTO_ID_5326472500793419538" border="0" /></a>Figure 12.1: Shopping cart page<br /><br /></div><div style="text-align: center;"><a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiPfXk3nfovSSsu6iru3nJw8gkKBK3mnvkeJNszGhvDSTWDJBNGKgxHhojuhLelDlmxn4ZINk9ioXzlkTWn-q1fhUqgKfUDlLBbd3kOslzg6Nj5mWUn5W9Vpkit_PS4rEqpmOwv94gXHSs/s1600-h/4.JPG"><img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 400px; height: 271px;" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiPfXk3nfovSSsu6iru3nJw8gkKBK3mnvkeJNszGhvDSTWDJBNGKgxHhojuhLelDlmxn4ZINk9ioXzlkTWn-q1fhUqgKfUDlLBbd3kOslzg6Nj5mWUn5W9Vpkit_PS4rEqpmOwv94gXHSs/s400/4.JPG" alt="" id="BLOGGER_PHOTO_ID_5326472496222623474" border="0" /></a>Figure 12.2 Error page<br /><div style="text-align: left;"><br /></div></div></span>adminhttp://www.blogger.com/profile/10028997688633545873noreply@blogger.com0tag:blogger.com,1999:blog-3319637562268350646.post-28342434678158965662009-04-17T08:07:00.000-07:002009-08-03T01:43:12.143-07:00Write a program to display an error message to the user if an exception occurs in the JSP page<span style="color: rgb(255, 0, 0);">Write a program to display an error message to the user if an exception occurs in the JSP page. In the JSP page, consider a null vector and find out the length of the string using size()method of Java. Create an error handler to handle the exception thrown by this JSP page. </span><br /><span id="fullpost"><br /><span style="font-weight: bold;">Solution:</span><br />The files used to run the application are:<br /><span style="font-weight: bold;">1. Nullexception.jsp</span><br /><span style="font-weight: bold;">2. errorpage.jsp</span><br /><span style="text-decoration: underline;"><span style="color: rgb(51, 204, 0);">//Nullexception.jsp</span><br /><blockquote style="color: rgb(153, 0, 0);"><%@ page errorPage="errorpage.jsp" import="java.util.Vector"%><br /><html><br /><head><br /></head><br /><%! Vector vec = null; %><br /><body bgcolor=#ffffff><br />Vector has <%= vec.size() %> elements.<br /></body><br /></html></blockquote></span><span style="text-decoration: underline;"><span style="color: rgb(51, 204, 0);">//errorpage.jsp</span><br /><blockquote style="color: rgb(153, 0, 0);"><html><br /><%@ page isErrorPage="true" %><br />The exception <font color="red"> <%= exception.getMessage() %> </font> has occurred.<br /></body><br /></html></blockquote><br /></span>The output of the program is as shown in Figure 10.6.<br /><br /></span><div style="text-align: center;"><span id="fullpost"><a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgcAiDjimjkSsDx-genrOnHbvxHsf9eq0OZ6KermgOEykCZylqsukWFQvCVZOtfE-BiiRkBBEE4VMWokuJP05A6RLmM60M1S_1nj8IwAorR_BcJ2xm_wylW-iAlYjG_-oPlKBEbJFj_9uQ/s1600-h/3.JPG"><img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 400px; height: 212px;" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgcAiDjimjkSsDx-genrOnHbvxHsf9eq0OZ6KermgOEykCZylqsukWFQvCVZOtfE-BiiRkBBEE4VMWokuJP05A6RLmM60M1S_1nj8IwAorR_BcJ2xm_wylW-iAlYjG_-oPlKBEbJFj_9uQ/s400/3.JPG" alt="" id="BLOGGER_PHOTO_ID_5325677843866150034" border="0" /></a>Figure 10.6: Output of Nullexception.jsp</span><br /></div>adminhttp://www.blogger.com/profile/10028997688633545873noreply@blogger.com0tag:blogger.com,1999:blog-3319637562268350646.post-44318841504141995622009-04-17T08:00:00.000-07:002009-08-03T01:42:29.256-07:00Write a program to include an HTML file in a JSP page using RequestDispatcher interface method<span style="color: rgb(255, 0, 0);">Write a program to include an HTML file in a JSP page using RequestDispatcher interface method. The included file should display personal details form to the user. In addition, the form should provide a Submit and Reset button. Display the details entered by the user after the user clicks the Submit button.</span><br /><span id="fullpost"><br />Solution:<br />The files used to run the application are:<br /><span style="font-weight: bold;">1. Main.jsp</span><br /><span style="font-weight: bold;">2. Personal.jsp</span><br /><span style="font-weight: bold;">3. Details.jsp</span><br /><span style="text-decoration: underline;"><span style="color: rgb(51, 204, 0);">//Main.jsp</span><br /><blockquote style="color: rgb(153, 0, 0);"><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><br /><html><br /><head><br /><title>Include Example</title><br /><link rel=STYLESHEET href="My-Style-Sheet.css" type="text/css"><br /></head><br /><body bgcolor="#FDF5E6" text="#000000" link="#0000EE"<br />vlink="#551A8B" alink="#FF0000"><P><br /><jsp:include page="Personal.jsp" flush="true"/><br /></body><br /></html></blockquote></span><span style="color: rgb(51, 204, 0);">//Personal.jsp</span><br /><blockquote style="color: rgb(153, 0, 0);"><html><br /><head><br /><title>Personal Details Form</title><br /></head><br /><body><br /><Center><h1><br />Personal Details form<br /></h1></Center><br /><form action="Details.jsp" method="post"><br />First Name:<br /> <input type="text" name="fname"><br /><br><br><br />Last Name:<br /> <input type="text" name="lname"><br /><br><br><br />Address:<br><br /> <br /> <br /><textarea name=address rows=3 cols=15></textarea><br /><br><br><br />Phone Number:<br /><input type="text" name="phone"><br /><br><br><br />City:<br /> <br /><br /><input type="text" name="city"><br /><br><br><br />State:<br /> <br /><input type="text" name="state"><br /><br><br><br /><input type="submit" name="submit" value="Submit"><br /></form><br /></body><br /></html></blockquote><span style="color: rgb(51, 204, 0);">//Details.jsp</span><br /><blockquote style="color: rgb(153, 0, 0);"><html><br /><head><br /><title>Example of Implicit objects</title><br /></head><br /><body><br /><font face=Times New Roman size=3><br />Thank you for your submission. Confirm the details:<br /><br><br><br /><%<br />String sfName = request.getParameter("fname");<br />String slName = request.getParameter("lname");<br />String saddress = request.getParameter("address");<br />String sphone = request.getParameter("phone");<br />String scity = request.getParameter("city");<br />String sstate = request.getParameter("state");<br />%><br />First Name:<%=sfName%><br><br />Last Name:<%=slName%><br><br />Address:<%=saddress%><br><br />Phone:<%=sphone%><br><br />City:<%=scity%><br><br />State:<%=sstate%><br><br /></font><br /></body><br /></html></blockquote><span id="fullpost">The output appears as shown in Figure 10.4.<br /><div style="text-align: center;"><a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgXkjen4tRn6driohpTHGWhwpN-XdK-Hjo3HXqfEjCGk56mCX7C6ispIcmGV_VK23H81sx_FCPEGh54E3QyB5FBkW4coOAbUJF3F-NA2gyHXtsYFhAF3x2ErVDrk3Qc05GSS-Y1hJ6BYYU/s1600-h/4.JPG"><img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 346px; height: 400px;" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgXkjen4tRn6driohpTHGWhwpN-XdK-Hjo3HXqfEjCGk56mCX7C6ispIcmGV_VK23H81sx_FCPEGh54E3QyB5FBkW4coOAbUJF3F-NA2gyHXtsYFhAF3x2ErVDrk3Qc05GSS-Y1hJ6BYYU/s400/4.JPG" alt="" id="BLOGGER_PHOTO_ID_5325676400548035266" border="0" /></a>Figure 10. 4: Output of Main.jsp<br /></div><br />When the user clicks on Submit button after entering the details, the output appears as shown in Figure 10.5.<br /><div style="text-align: center;"><a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi-0JJgQa-vmxiENyaZOPJYcXOZlRju31iXCMmuPpYdAF_s7qPDZ_toRbbeadreN-DsPs32h_8951EAjsG9CqIzRYOpTGm0czWQ7eoz3L4Yx39T8VG1ZKO8NO5xiT45iBt75rVcBd7FRC0/s1600-h/5.JPG"><img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 400px; height: 296px;" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi-0JJgQa-vmxiENyaZOPJYcXOZlRju31iXCMmuPpYdAF_s7qPDZ_toRbbeadreN-DsPs32h_8951EAjsG9CqIzRYOpTGm0czWQ7eoz3L4Yx39T8VG1ZKO8NO5xiT45iBt75rVcBd7FRC0/s400/5.JPG" alt="" id="BLOGGER_PHOTO_ID_5325676400586822018" border="0" /></a>Figure 10.5: Output of Details.jsp<br /><br /></div> </span></span>adminhttp://www.blogger.com/profile/10028997688633545873noreply@blogger.com0tag:blogger.com,1999:blog-3319637562268350646.post-35951416686048059272009-04-17T07:55:00.000-07:002009-08-03T01:42:01.324-07:00Write a program to display an error message to the user if an exception occurs in the JSP page<span style="color: rgb(255, 0, 0);">Write a program to display an error message to the user if an exception occurs in the JSP page. In the JSP page, consider a null string and find out the length of the string using length()method of Java. Create an error handler to handle the exception thrown by this JSP page. </span><br /><span id="fullpost"><br />Solution:<br /><br />The files used to run the application are:<br /><br />1. Exception.jsp<br />2. Example.html<br /><span style="text-decoration: underline;"><span style="color: rgb(51, 204, 0);">//Exception.jsp</span><br /><blockquote style="color: rgb(153, 0, 0);"><html><br /><body><br /><%@ page errorPage="example.jsp" %><br />Example for Null Pointer exception:<br /><%<br />String s=null;<br />s.length();<br />%><br /></body><br /></html></blockquote><br /></span><span style="text-decoration: underline;"><span style="color: rgb(51, 204, 0);">//Error handler file</span><br /><span style="color: rgb(51, 204, 0);">//Example.jsp</span><br /><blockquote style="color: rgb(153, 0, 0);"><html><br /><body><br /><%@ page isErrorPage="true" %><br />The error is:<br /><%= exception %><br /></body><br /></html></blockquote></span>The output of the program is as shown in Figure 10.3.<br /><br /><div style="text-align: center;"><a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEirZOXWbe0xYkUgdweX9WChgGhNvZK1LAg9S0yOjRJR5TJciBgJbrdxSyW9CDe7Vli_JR2A6s7REgIr2GOBlGtn864iEVEX2ZwG0jD8MI9HYhQ4OcO01F-tLFzxt5GzKHemynJubq661P4/s1600-h/3.JPG"><img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 400px; height: 205px;" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEirZOXWbe0xYkUgdweX9WChgGhNvZK1LAg9S0yOjRJR5TJciBgJbrdxSyW9CDe7Vli_JR2A6s7REgIr2GOBlGtn864iEVEX2ZwG0jD8MI9HYhQ4OcO01F-tLFzxt5GzKHemynJubq661P4/s400/3.JPG" alt="" id="BLOGGER_PHOTO_ID_5325674839234793730" border="0" /></a>Figure 10.3: Output of Example.jsp<br /><br /></div></span>adminhttp://www.blogger.com/profile/10028997688633545873noreply@blogger.com0tag:blogger.com,1999:blog-3319637562268350646.post-32878647070662859422009-04-17T07:46:00.000-07:002009-08-03T01:41:20.886-07:00Write a program to include an HTML file in a JSP page using RequestDispatcher interface method<span style="color: rgb(255, 0, 0);">Write a program to include an HTML file in a JSP page using RequestDispatcher interface method. The included file should display a login form to the user. In addition, the form should provide a Submit and Reset button. Display a Welcome message to the user when the user clicks on Submit button after entering the details.</span><br /><span id="fullpost"><br />Solution:<br /><br />The files used to run the application are:<br /><br />1. Include.jsp<br />2. form.html<br />3. Welcome.jsp<br /><span style="text-decoration: underline;"><span style="color: rgb(51, 204, 0);">//Include.jsp</span><br /><blockquote style="color: rgb(153, 0, 0);"><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><br /><html><br /><head><br /><title>Include Example</title><br /><link rel=STYLESHEET<br />href="My-Style-Sheet.css" type="text/css"><br /></head><br /><body bgcolor="#FDF5E6" text="#000000" link="#0000EE"<br />vlink="#551A8B" alink="#FF0000"><P><br /><jsp:include page="form.htm" flush="true"/><br /></body><br /></html></blockquote><br /></span><span style="text-decoration: underline;"><span style="color: rgb(51, 204, 0);">//form.html</span><br /><blockquote style="color: rgb(153, 0, 0);"><html><br /><head><br /><style><br />body, input { font-family:Tahoma; font-size:10pt; }<br /></style><br /></head><br /><body><br /><!-- HTML Form --><br /><form action="Welcome.jsp" method="post"><br />&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp Enter your Account Id:<br /><input type="text" name="Acc_Id" /> &nbsp &nbsp &nbsp &nbsp<br />&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp<br />Enter your Pin number:<br /><input type="text" name="Pin_num"/> <br><br><br><br><br /><Center><input type="submit" value="Submit"/><Center><br /></form><br /></body><br /></html></blockquote><br /></span><span style="text-decoration: underline;"><span style="color: rgb(51, 204, 0);">//Welcome.jsp</span><br /><blockquote style="color: rgb(153, 0, 0);"><%@ page language = "java" import="java.util.*" %><br /><html><br /><head><br /><title>Welcome to our Website</title><br /></head><br /><body><br /><center><br />out.println("Welcome to Online Banking");<br /></center><br /></body><br /></html></blockquote><br /></span><span id="fullpost">The output of the program is as shown in Figure 10.1.<br /><span id="fullpost"><br /><br /><div style="text-align: center;"><a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEggYANaQQu8yFeOnZiuMOHq-I7KUB0fqj9kynLFXI_b0WH_tUOVrBG3iAxMNpmi1tUuLL5u_s_qwfozP-tUKN8ETHjUA5-R-p2bCX3jUgEI7xl47zcBPTDihyphenhyphenKgpFktPgy8BrBESCBowGk/s1600-h/4.JPG"><img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 400px; height: 275px;" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEggYANaQQu8yFeOnZiuMOHq-I7KUB0fqj9kynLFXI_b0WH_tUOVrBG3iAxMNpmi1tUuLL5u_s_qwfozP-tUKN8ETHjUA5-R-p2bCX3jUgEI7xl47zcBPTDihyphenhyphenKgpFktPgy8BrBESCBowGk/s400/4.JPG" alt="" id="BLOGGER_PHOTO_ID_5325673063436242962" border="0" /></a>Figure 10.1: Output of form.html<br /></div><br />When the user enters the details and clicks on Submit button, the output appears as shown in Figure 10.2.<br /><br /><div style="text-align: center;"><a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhCpOL8sG55LKXC4Y_vdB9pgqACtdbPrw4GwKI8jGoP8u9EZ2VTCsVPXSimGhqUAoMs8S35nFEtDuWRxg8-tSE-x2yR_crfglsc3Wch_-0AG_Fh18RmrYpHnQTHM_uxwr-2JrJB-aK9o2g/s1600-h/5.JPG"><img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 400px; height: 275px;" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhCpOL8sG55LKXC4Y_vdB9pgqACtdbPrw4GwKI8jGoP8u9EZ2VTCsVPXSimGhqUAoMs8S35nFEtDuWRxg8-tSE-x2yR_crfglsc3Wch_-0AG_Fh18RmrYpHnQTHM_uxwr-2JrJB-aK9o2g/s400/5.JPG" alt="" id="BLOGGER_PHOTO_ID_5325673064102868082" border="0" /></a>Figure 10.2: Output after clicking Submit button<br /><div style="text-align: left;"><br /></div></div></span></span></span>adminhttp://www.blogger.com/profile/10028997688633545873noreply@blogger.com0tag:blogger.com,1999:blog-3319637562268350646.post-91023404433987396412009-04-17T06:51:00.000-07:002009-08-03T01:40:29.115-07:00Write a program to display the withdrawal status to the user<span style="color: rgb(255, 0, 0);">Write a program to display the withdrawal status to the user. If the account number is valid, display the withdrawal screen to the user. The withdrawal screen should display the balance entered by the user on the withdrawal status screen and ask the user to enter the amount to be withdrawn. Display the balance to the user after withdrawing the amount.</span><br /><span id="fullpost"><br />Solution:<br /><span style="text-decoration: underline;"><span style="color: rgb(51, 204, 0);">//withdraw.jsp</span><br /><blockquote style="color: rgb(153, 0, 0);"><html><br /><head><br /><title>MARKO</title><br /></head><br /><body><br /><H1 align="center">MARKO Amount Withdrawal Screen</H1><br /><%<br />String amt = request.getParameter("amt") ;<br />String balance = request.getParameter("bal");<br />if (amt == null)<br />{<br />%><br /><form method="post"><br /><h3><br />Your balance is <input type='text' readonly name='bal'<br />value='<%=balance%>'><br><br />Please enter the amount to be withdrawn <input<br />type="text"<br />name="amt"> <br /><input type="submit" value="Withdraw"><br /></form><br /><%<br />}<br />else<br />{<br />%><br /><jsp:useBean id="objW" class="MARKO.AccountBean"<br />scope="page" /><br /><jsp:setProperty name="objW" property="balance"<br />value="<%=balance%>"/><br /><jsp:setProperty name="objW" property="amount"<br />value="<%=amt%>"/><br /><H3 align="center">Rs. <%=amt%> withdrawn from your<br />account<br />the new balance is <jsp:getProperty name="objW"<br />property="balance"/><br /></h3> <br /><%<br />}<br />%><br /></body><br /></html></blockquote></span><span style="font-weight: bold;">1. Save the file as withdraw.jsp in basic directory.</span><br /><br /><span style="font-weight: bold;">2. Open Notepad and enter the following code.</span><br /><span id="fullpost"><span style="text-decoration: underline;"><span style="color: rgb(51, 204, 0);">//AccountBean.java</span><br /><blockquote style="color: rgb(153, 0, 0);">package MARKO;<br />public class AccountBean implements java.io.Serializable<br />{<br />private int amount = 0;<br />private int balance = 0 ;<br />public String getAmount()// throws Exception<br />{ <br />return String.valueOf(amount);<br />}<br />public void setAmount(String strAmt)// throws Exception<br />{<br />this.amount=Integer.parseInt(strAmt);<br />}<br />public String getBalance()<br />{<br />if (balance < amount)<br />{<br />return "Balance is not sufficient to withdraw money."; <br />} <br />else<br />{<br />balance = balance - amount;<br />}<br />return ("Rs."+String.valueOf(amount)+" withdrawn from your<br />account. The new balance is Rs"+String.valueOf(balance));<br />}<br />public void setBalance(String strBal)<br />{<br />balance = Integer.parseInt(strBal);<br />}<br />}</blockquote></span><span style="font-weight: bold;">3. Save the file as AccountBean.java in Session8 directory.</span><br /><br /><span style="font-weight: bold;">4. Stop and restart Tomcat server.</span><br /><br /><span style="font-weight: bold;">5. Open Internet Explorer and enter http://localhost:8080/Session8/withdraw.jsp in the browser.</span><br /><br />The output appears as shown in Figure 8.6.<br /><br /><div style="text-align: center;"><a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhEFXPBu6ujfKyghma5qn1CRCdku43LspM6V_l5UhN2v_8p4TqqB_qnMmWr2EuTtb4l1TMTKxRxrVydE25JisTP4Z3MXxhL-8kbQwsHaNl556jrbxwJABvru-h8LbRaivYpM8ZWEBY62Ak/s1600-h/3.JPG"><img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 400px; height: 229px;" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhEFXPBu6ujfKyghma5qn1CRCdku43LspM6V_l5UhN2v_8p4TqqB_qnMmWr2EuTtb4l1TMTKxRxrVydE25JisTP4Z3MXxhL-8kbQwsHaNl556jrbxwJABvru-h8LbRaivYpM8ZWEBY62Ak/s400/3.JPG" alt="" id="BLOGGER_PHOTO_ID_5325658621053569090" border="0" /></a>Figure 8.6: Withdraw Amount Screen<br /></div><br />Enter an amount greater than the balance amount. A message will be displayed to the user informing that balance amount is insufficient.<br /><br /><div style="text-align: center;"><a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjPLsb-SUhgIClqDnn2BYfNymC4uBTeo88WNAOX7CvTYGpUZ3By7-4aow5CVwfVHVjW6SNJgt965KBe3o_hj0wid8w_uUMTKr9aB7KBbqrGpJR1cDNzE8CnUQ9uGZSOVyzS_ST0pe2i8bs/s1600-h/4.JPG"><img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 400px; height: 184px;" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjPLsb-SUhgIClqDnn2BYfNymC4uBTeo88WNAOX7CvTYGpUZ3By7-4aow5CVwfVHVjW6SNJgt965KBe3o_hj0wid8w_uUMTKr9aB7KBbqrGpJR1cDNzE8CnUQ9uGZSOVyzS_ST0pe2i8bs/s400/4.JPG" alt="" id="BLOGGER_PHOTO_ID_5325658616371669458" border="0" /></a>Figure 8.7: Output with invalid amount<br /></div><br />Enter the amount to be withdrawn and click the Withdraw button. The output appears as shown in Figure 8.7 when you click on Withdraw button.<br /><br /><div style="text-align: center;"><a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhWIqjFlg46Cv9gi4X9hB_6baUoz-kKuMrewYP0UZqPB5OnvefXFmkqcJJ4nJp-lpeqYLPdza38NxQnbyO6F7qiDdg9EqKrVD3gpg1jSP0kKUMuAv4cCiubm66thTaeuBYI92qNDiZTAPY/s1600-h/5.JPG"><img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 400px; height: 210px;" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhWIqjFlg46Cv9gi4X9hB_6baUoz-kKuMrewYP0UZqPB5OnvefXFmkqcJJ4nJp-lpeqYLPdza38NxQnbyO6F7qiDdg9EqKrVD3gpg1jSP0kKUMuAv4cCiubm66thTaeuBYI92qNDiZTAPY/s400/5.JPG" alt="" id="BLOGGER_PHOTO_ID_5325658617961071634" border="0" /></a>Figure 8.8: Output with valid amount<br /><br /><div style="text-align: left;"><span class="fullpost"><pre></pre></span></div></div></span></span>adminhttp://www.blogger.com/profile/10028997688633545873noreply@blogger.com0tag:blogger.com,1999:blog-3319637562268350646.post-6115781023509249442009-04-17T06:20:00.000-07:002009-08-03T01:39:53.233-07:00Write a program to display a withdrawal amount screen to the user<span style="color: rgb(255, 0, 0);">Write a program to display a withdrawal amount screen to the user. Provide the user with three fields: Email Id, account number, and Balance amount. Display the withdraw status to the user after the user enters the email Id, account number and amount. In addition, send an e-mail to the user’s email id informing about the withdrawal status.</span><br /><span id="fullpost"><br />Solution:<br /><br />Enter the following code in the notepad and save the file as withdrawlstatus_1.jsp in Session8 folder.<br /><span style="text-decoration: underline;"><span style="color: rgb(51, 204, 0);">//withdrawlstatus_1.jsp</span><br /><blockquote style="color: rgb(153, 0, 0);"><html><br /><head><br /><title>MARKO</title><br /></head><br /><body><br /><H1 align="center">MARKO Withdrawal status checking<br />Screen</H1><br /><form method ='post'><br /><h3>User Email ID <input type='text' name='email'></h3><br /><h3>Account no <input type='text' name='accNo'></h3><br /><h3>Balance Amount <input type='text' name='Bal'></h3><br /><input type='submit' value="Check Status"><br /></form><br /><%<br />String strAccount = request.getParameter("accNo");<br />String strBal = request.getParameter("Bal");<br />String strEmail = request.getParameter("email");<br />if (strAccount == null || strBal == null || strEmail ==<br />null)<br />{<br />%><br /><h3 align="center"> Please enter email address, account No<br />and Balance and click the Check Status button to compute<br />the withrawal status</h3><br /><%<br />}<br />else<br />{<br />%><br /><jsp:useBean id="objBT" class="MARKO.BankTransactionMail"<br />scope="session" ><br /><jsp:setProperty name="objBT" property="accountNo"<br />value="<%=strAccount%>"/><br /><jsp:setProperty name="objBT" property="balance"<br />value="<%=strBal%>"/><br /></jsp:useBean><br /><h3>For Account no <jsp:getProperty name="objBT"<br />property="accountNo"/> :<br /><%<br />String strMessage = objBT.withdrawAllowed();<br />out.println(strMessage);<br />%><br /><br><br /><br><br /><%=objBT.sendMail(strEmail,strMessage)%><br /></h3><br /><%<br />}<br />%><br /></body></blockquote><br /></span>Enter the following code in notepad and save the file as BankTransactionMail.java in Session8 folder.<br /><span id="fullpost"><span style="text-decoration: underline;"><span style="font-weight: bold; color: rgb(51, 204, 0);">//BankTransactionMail.java</span><br /><blockquote style="color: rgb(153, 0, 0);">package MARKO;<br />import javax.mail.*;<br />import javax.mail.internet.*;<br />import java.util.*;<br />public class BankTransactionMail implements java.io.Serializable<br />{<br />int intAccountNo;<br />int intBalance;<br />public String getBalance()<br />{<br />return String.valueOf(intBalance);<br />}<br /> <br />public void setBalance(String strBal)<br />{<br />intBalance = Integer.parseInt(strBal);<br />}<br /><br />public String getAccountNo()<br />{<br />return String.valueOf(intAccountNo);<br />}<br /> <br />public void setAccountNo(String strAccNo)<br />{<br />intAccountNo = Integer.parseInt(strAccNo);<br />}<br /> <br />public String withdrawAllowed()<br />{<br />if (intAccountNo < 5000)<br />{<br />if (intBalance > 10000)<br />{<br />return "withdrawal is allowed!!!<br>To withdraw please<br /><a href='withdraw.jsp?bal= " +<br />String.valueOf(intBalance) + "'>click here.</a>";<br />//The file withdraw.jsp is created in Example 1<br />of Do It Yourself.<br />}<br />else<br />{<br />return "withdrawal is not allowed!!!";<br />}<br />}<br />else<br />{<br />return "withdrawal is allowed!!!<br>To withdraw please <a<br />href='withdraw.jsp?bal=" + String.valueOf(intBalance) +<br />"'>click here.</a>";<br />//The file withdraw.jsp is created later in Do it<br />Yourself part.<br />}<br />} <br />public String sendMail(String strTo,String strMsg)<br />{<br />String result = "<BR><BR><BR>";<br />String host = "Name_of _Machine_Running_SMTP_Service";<br />Properties prop = System.getProperties();<br />prop.put("libmail.n-syst.com", host);<br />Session session = Session.getDefaultInstance(prop, null);<br />session.setDebug(true);<br />try<br />{<br />MimeMessage msg = new MimeMessage(session);<br />msg.setFrom(new InternetAddress("admin@MARKO.com"));<br />msg.setRecipients(Message.RecipientType.TO,<br />InternetAddress.parse(strTo, false));<br />msg.setSubject("Automated Mail from MARKO JSP Page");<br />MimeBodyPart mbp = new MimeBodyPart();<br />mbp.setText(strMsg);<br />Multipart mp = new MimeMultipart();<br />mp.addBodyPart(mbp);<br />msg.setContent(mp);<br />msg.setSentDate(new Date());<br />Transport.send(msg);<br />result = result + "<H3 align='center'>Mail was<br />successfully sent to </B></FONT>: "+ strTo +"<BR>";<br />result = result+"</H3>";<br />}<br />catch (MessagingException ex)<br />{<br />result = result + "<H3 style='color:red'<br />align='center'>Error : </B><BR><HR> "+"<FONT SIZE=3<br />COLOR=\"black\">"+ex.toString()+"<BR><HR></h3>";<br />}<br />catch (Exception e)<br />{ <br />result = result + "<H3 style='color:red'<br />align='center'>Error : </B><BR><HR> "+"<FONT SIZE=3<br />COLOR=\"black\">"+e.toString()+"<BR><HR></h3>";<br />}<br />finally<br />{<br />return result;<br />}<br />}<br />}</blockquote><br /></span>Enter the path http://localhost:8080/Session8/withdrawlstatus_1.jsp in the address bar. The output appears as shown in Figure 8.1.<br /><br /><div style="text-align: center;"><a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgwWN2ppJYs_o0jW7qB4ISvXxPalV_9SOHY6m-peQJ7c4qeoK5AxfXhx5GRlT5beh3mMylINseW1AhcD3zWbhrzJO-DPY2xfUlqCnZWZMjUmTDiC2vmKH2Tf9HPfZbC_osn6Mmj6JTGlv8/s1600-h/5.JPG"><img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 400px; height: 280px;" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgwWN2ppJYs_o0jW7qB4ISvXxPalV_9SOHY6m-peQJ7c4qeoK5AxfXhx5GRlT5beh3mMylINseW1AhcD3zWbhrzJO-DPY2xfUlqCnZWZMjUmTDiC2vmKH2Tf9HPfZbC_osn6Mmj6JTGlv8/s400/5.JPG" alt="" id="BLOGGER_PHOTO_ID_5325655445135255234" border="0" /></a>Figure 8.1: Check Withdrawal Status Screen<br /></div><br />Enter the e-mail Id, account number, and balance amount and click the Check Status button. The output appears as shown in Figure 8.2.<br /><br /><div style="text-align: center;"><a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEilU6WpAdpDYBphrMrQKueGg7fGULRmOKSlRvErJ9gH_GpXN2sbyFiVXOMZ4t56ybk8LfDWK0-Czt43Sbi2dzMPmvHKBSQrz0NWVlHC1usfTIQlL8zA3jFVe_5PSc54FCnmgh4l6p5fGus/s1600-h/6.JPG"><img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 398px; height: 400px;" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEilU6WpAdpDYBphrMrQKueGg7fGULRmOKSlRvErJ9gH_GpXN2sbyFiVXOMZ4t56ybk8LfDWK0-Czt43Sbi2dzMPmvHKBSQrz0NWVlHC1usfTIQlL8zA3jFVe_5PSc54FCnmgh4l6p5fGus/s400/6.JPG" alt="" id="BLOGGER_PHOTO_ID_5325655444430736818" border="0" /></a>Figure 8.2: Withdrawal Status Screen<br /></div><br />The automated mail sent to the user appears as shown in Figure 8.3.<br /><div style="text-align: center;"><a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh_PqGdR_rZ15c1Ab774Rs4_9eeoB1vDdVmoPdffD73jjzgHDwT7zNX0pKSFYPuN9z7D2P6eM75_QbjBb7ZItCg-j2t4ekVryC5EI72oSoh1_WmqsobEgxicHddFqjijK4KhZo0EsjOKVI/s1600-h/7.JPG"><img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 400px; height: 270px;" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh_PqGdR_rZ15c1Ab774Rs4_9eeoB1vDdVmoPdffD73jjzgHDwT7zNX0pKSFYPuN9z7D2P6eM75_QbjBb7ZItCg-j2t4ekVryC5EI72oSoh1_WmqsobEgxicHddFqjijK4KhZo0EsjOKVI/s400/7.JPG" alt="" id="BLOGGER_PHOTO_ID_5325655441603472226" border="0" /></a>Figure 8.3: Automated Mail<br /></div><br />To withdraw, click on the link provided in the figure above. The output appears as shown in Figure 8.4.<br /><br /><div style="text-align: center;"><a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhbkNQ3ZWZGiTg_zy2pCGS8jpSOFvtO97sjOcK7uZ_Dycffxz1GEPYP9uR2eaFBe3AfJFYE6KIAmz4ip1TxGjm_Cyo9xD2P6rlJMpi_LzsaKOLoEnWETlagdFAQZQ5nzEautvVCh3bjaAs/s1600-h/8.JPG"><img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 400px; height: 251px;" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhbkNQ3ZWZGiTg_zy2pCGS8jpSOFvtO97sjOcK7uZ_Dycffxz1GEPYP9uR2eaFBe3AfJFYE6KIAmz4ip1TxGjm_Cyo9xD2P6rlJMpi_LzsaKOLoEnWETlagdFAQZQ5nzEautvVCh3bjaAs/s400/8.JPG" alt="" id="BLOGGER_PHOTO_ID_5325655441035779906" border="0" /></a>Figure 8.4: Withdrawal Screen<br /></div><br />Enter the amount to be withdrawn and click the Withdraw button. The output appears as shown in Figure 8.5.<br /><br /><div style="text-align: center;"><a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjbPGp1B76QOxElmykhgHVIHpni1O_cqUs7_O0VCa-5rjuiiusW8vQEFddo-a6PC9dPYYCHjeoAUUi3PVdjUKUYsa5eNbvhvMpLXqXGBgK9BHTYtpHGa9A1GkJn1j49MxAY42UEjb_OJv0/s1600-h/9.JPG"><img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 400px; height: 210px;" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjbPGp1B76QOxElmykhgHVIHpni1O_cqUs7_O0VCa-5rjuiiusW8vQEFddo-a6PC9dPYYCHjeoAUUi3PVdjUKUYsa5eNbvhvMpLXqXGBgK9BHTYtpHGa9A1GkJn1j49MxAY42UEjb_OJv0/s400/9.JPG" alt="" id="BLOGGER_PHOTO_ID_5325655437173878786" border="0" /></a>Figure 8.5: Withdrawal Confirmation Screen<br /><br /><div style="text-align: left;"><br /></div></div></span></span>adminhttp://www.blogger.com/profile/10028997688633545873noreply@blogger.com0tag:blogger.com,1999:blog-3319637562268350646.post-86616651523626557152009-04-17T06:09:00.000-07:002009-08-03T01:37:47.821-07:00Build a login page for online banking, accepting the user name and password.<span style="color: rgb(255, 0, 0);">1.Build a login page for online banking, accepting the user name and password. Use declarations to define the string variables. Include Java code in the scriptlet block to assign the details to the string variables entered by the user.</span><br /><span id="fullpost"><br />Solution:<br />The file used in this exercise is Expression_Login.jsp. The file is saved in basic directory.<br /><blockquote style="color: rgb(153, 0, 0);"><html> <br /><head> <br /><title>Login Page</title> <br /></head> <br /><body bgcolor="white"> <br /><%if(request.getParameter("userid")==null&& request.getParameter("password")==null ) { %> <br /><center> <br /><h2>Online Banking Login</h2><br /></center><br /><form method="GET" action="http://localhost:8080/basic/Login.jsp"><br /><b>User Id:</b> <input type="text" name="userid" size=15> <br /><p> <br /><b>Password:<b> <input type="password" name="password" size=8> <br /><p><br /><input type="submit" value="LOGIN"> <br /></form> <br /><% } else { %> <br /><%! String userid,password; %> <br /><% <br />userid = request.getParameter("userid");<br />password = request.getParameter("password");<br />%><br /><% } %> <br /></body> <br /></html> </blockquote> <br />The output of the program is as shown in Figure 6.3.<br /><span id="fullpost"><br /><div style="text-align: center;"><a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjCYjkcw5ZGyG-Nj_5qrFOLVyz-0GjAYqJYyf-ruAQIpJliAKXyhX5f45XsyBi-gP2YOhhzvdBBuD7MxlWJVv6q1Enk6Ou5E5EGRT_PIanJgRssAxCd3g_CZLGIULAX2vyDgWWUulr2llg/s1600-h/2.bmp"><img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 400px; height: 284px;" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjCYjkcw5ZGyG-Nj_5qrFOLVyz-0GjAYqJYyf-ruAQIpJliAKXyhX5f45XsyBi-gP2YOhhzvdBBuD7MxlWJVv6q1Enk6Ou5E5EGRT_PIanJgRssAxCd3g_CZLGIULAX2vyDgWWUulr2llg/s400/2.bmp" alt="" id="BLOGGER_PHOTO_ID_5325648602187862834" border="0" /></a>Figure 6.3: Output of Login.jsp<br /></div><br /><span style="color: rgb(255, 0, 0);">2.Use the above code and greet the login user. Make use of the expression elements to display the message. </span><br /><span id="fullpost"><br />Solution:<br />The file used in this exercise is Login.jsp. The file is saved in basic directory.<br /><blockquote style="color: rgb(153, 0, 0);"><html> <br /><head> <br /><title>Login Page</title> <br /></head> <br /><body bgcolor="white"> <br /><%if(request.getParameter("userid")==null&& request.getParameter("password")==null ) { %> <br /><center> <br /><h2>Online Banking Login</h2><br /></center><br /><form method="GET" action="http://localhost:8080/basic/Login.jsp"><br /><b>User Id:</b> <input type="text" name="userid" size=15> <br /><p> <br /><b>Password:<b> <input type="password" name="password" size=8> <br /><P><br /><input type="submit" value="LOGIN"> <br /></form> <br /><% } else { %> <br /><%! String userid,password; %> <br /><% <br />userid = request.getParameter("userid");<br />password = request.getParameter("password");<br />%><br /><p><br /><b>Good Morning!! <%= userid %><p></b><br /><% } %> <br /></body> <br /></html> </blockquote><br />The output of the program is as shown in Figure 6.4.<br /><br /><div style="text-align: center;"><a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh8pjxq8pOpkOuh5IUf4cSJD0N4NTuV03FaM0nVYcvQPUTLucQO-yC4BYH3f2AOcTcHkEwl0gaERfamCKJXycYhyphenhyphenNjBxtLsdz_BWCivS3_8TRe_SB-ej69Dua5Z7I_xUHR1B-nHtwnUGOo/s1600-h/1.2.bmp"><img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 400px; height: 244px;" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh8pjxq8pOpkOuh5IUf4cSJD0N4NTuV03FaM0nVYcvQPUTLucQO-yC4BYH3f2AOcTcHkEwl0gaERfamCKJXycYhyphenhyphenNjBxtLsdz_BWCivS3_8TRe_SB-ej69Dua5Z7I_xUHR1B-nHtwnUGOo/s400/1.2.bmp" alt="" id="BLOGGER_PHOTO_ID_5325648598173616354" border="0" /></a>Figure 6.4: Output of Login.jsp<br /><div style="text-align: left;"><br /></div></div></span></span></span>adminhttp://www.blogger.com/profile/10028997688633545873noreply@blogger.com0