1 / 18

Server-Side Scripting with JSP (2)

Server-Side Scripting with JSP (2). ISYS 350. Post Back. A postback is call to the same page that the form is on. In other words, the contents of the form are POSTed back to the same URL as the form. If a page is opened because of a postback , then it is not opened for the first time.

pink
Download Presentation

Server-Side Scripting with JSP (2)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Server-Side Scripting with JSP (2) ISYS 350

  2. Post Back • Apostback is call to the same page that the form is on. In other words, the contents of the form are POSTed back to the same URL as the form. • If a page is opened because of a postback, then it is not opened for the first time. • First time: present an empty form • Post back: present the results.

  3. Use a JSP to Presenting Form and Handle the Form • Use the form’s action attribute to call the JSP program itself. • Use a hidden element to indicate that the type is post back: • <input type="hidden" name="IsPostBack" value="yes"> • The JSP program checks to see if the JSP program is called for the first time or not. If it is a postback, then the JSP program processes the data submitted by the form

  4. <input type="hidden" name="IsPostBack" value="yes"> • When the page is first loaded, IsPostBack will have a null value because the page has not posted back yet.

  5. JSP testing for null value if (user_id ! = null) { Out.println (user_id); }

  6. Use JSP to Present and Process a Form

  7. sumPage.jsp <% String value1, value2; double n1=0, n2=0, sum=0; if (request.getParameter("IsPostBack")!=null) { value1=request.getParameter("num1"); value2=request.getParameter("num2"); n1= Double.parseDouble(value1); n2= Double.parseDouble(value2); sum=n1+n2; %> <form name="testForm" method="post" action="sumPage.jsp"> Value1: <input type="text" name="num1" size="20" value="<%=n1%>"><br><br> Value2: <input type="text" name="num2" size="20" value="<%=n2%>"><br><br> Sum is: <input type="text" name="sum" size="20" value="<%=sum%>"><br><br> <input type="hidden" name="IsPostBack" value="yes"> <input type="submit" value="ComputeSum" name="btnCompute"/> </form> <% } else { %> <form name="testForm" method="post" action="sumPage.jsp"> Value1: <input type="text" name="num1" size="20" value=""><br><br> Value2: <input type="text" name="num2" size="20" value=""><br><br> Sum is: <input type="text" name="sum" size="20" value=""><br><br> <input type="hidden" name="IsPostBack" value="yes"> <input type="submit" value="ComputeSum" name="btnCompute"/> </form> <% } %>

  8. sumPage.jsp <% String value1, value2; double n1=0, n2=0, sum=0; if (request.getParameter("IsPostBack")!=null) { value1=request.getParameter("num1"); value2=request.getParameter("num2"); n1= Double.parseDouble(value1); n2= Double.parseDouble(value2); sum=n1+n2; } %> <form name="testForm" method="post" action="testPage.jsp"> Value1: <input type="text" name="num1" size="20" value="<%=n1%>"><br><br> Value2: <input type="text" name="num2" size="20" value="<%=n2%>"><br><br> Sum is: <input type="text" name="sum" size="20" value="<%=sum%>"><br><br> <input type="hidden" name="IsPostBack" value="yes"> <input type="submit" value="ComputeSum" name="btnCompute"/> </form>

  9. Java Array • Examples of declaring an array: • int[] anArray = new int[10]; • 10 elements index from 0 to 9 • double[] anArrayOfDoubles = new double[10]; • String[] anArrayOfStrings = new String[10]; • int[] anArray = { 100, 200, 300, 400, 500, 600, 700, 800, 900, 1000};

  10. Creating a listbox of rates double[] rates= {0.03,0.04,0.05,0.06,0.07}; String[] displays={"3%","4%","5%","6%","7%"}; out.println( "Select interest rate: <select name='Rate'>"); for (inti = 0; i < rates.length-1; i++) { out.println("<option value='" + rates[i] + "'>" + displays[i] + "</option>"); } out.println( "</select><br><br>");

  11. Testing if two strings are equal • The equals() or equalsIgnoreCase method should be used to determine whether two objects have the same values. String string1 = "foo"; String string2 = “Foo"; if (string1.equals(string2)) //if (string1.equalsIgnoreCase(string2)) { out.println("The two strings are the same.") }

  12. Java Date Processing:Date Class • Date class: • Define a date object: • Date myDate; • Define a date object with the current date: • Date currentDate = new Date(); • getTime method: return the date in milliseconds since 1/1/1900

  13. Java Date Processing:DateFormat class • DateFormatclass is used to define a date format to display a date object or parsing a date/time string to create a date object. • Define a date format: • DateFormatformatter = new SimpleDateFormat("dd/MM/yy"); • To print a data object: • out.print("Current date is: " + formatter.format(currentDate)); • To parse a date string: • myDate=formatter.parse(request.getParameter("txtDate"));

  14. Import Java ClassExample: Display Current Date Time • Import java.util.Date • <%@page import="java.util.Date"%> • Define a Date type variable: • Date currentDate = new Date(); • Display in textbox using JSP expression: <p>The time is: <input type="text" name="num2" size="20" value="<%=currentDate%>"></p>

  15. Date to Date Format • Import class: • <%@page import="java.text.DateFormat"%> • Define a format: • SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yy"); • Convert: • Example: Display a date with date format: String displayDate = formatter.format(currentDate);

  16. Define Date Format Letter   Date or Time Component   Presentation       ExamplesG        Era designator              Text                ADy        Year                        Year                1996;    96M        Month in year                Month               July; Jul; 07w        Week in year                Number               27W        Week in month                Number               2D        Day in year                Number               189d        Day in month                Number               10F        Day of week in month        Number               2E        Day in week                Text               Tuesday; Tuea        Am/pm marker                Text               PMH        Hour in day (0-23)        Number               0k        Hour in day (1-24)        Number               24K        Hour in am/pm (0-11)        Number               0h        Hour in am/pm (1-12)        Number               12m        Minute in hour            Number               30s        Second in minute            Number               55S        Millisecond                 Number               978z        Time zone                   General time zone   Pacific Standard Time; PST; GMT-08:00Z        Time zone                  RFC 822 time zone   -0800

  17. Example <form name="dateForm" method="get" action="computeDate.jsp"> Enter a date: <input type="text" name="txtDate" value="7/4/12" /><br><br> <input type="submit" value="compute Date" name="btnSubmit" /> </form>

  18. computeDate.jsp <% Date currentDate = new Date(); Date myDate; DateFormat formatter = new SimpleDateFormat("dd/MM/yy"); myDate=formatter.parse(request.getParameter("txtDate")); out.print(currentDate +"<br>"); out.print("Current date is: " + formatter.format(currentDate)+"<br>"); out.print("Entered date is: " + formatter.format(myDate)+"<br>"); out.print ("Days between = " + (currentDate.getTime()-myDate.getTime())/(24*60*60*1000)+"<br>"); DateFormatyearFormat=new SimpleDateFormat("yyyy"); DateFormatmonthFormat=new SimpleDateFormat("MM"); DateFormatweekDayFormat=new SimpleDateFormat("E"); out.print(yearFormat.format(currentDate)+"<br>"); out.print(monthFormat.format(currentDate)+"<br>"); out.print(weekDayFormat.format(currentDate)+"<br>"); %>

More Related