Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

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

2 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. – 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: – – 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 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; %> Value1: "> Value2: "> Sum is: "> <% } else { %> Value1: Value2: Sum is: <% } %>

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; } %> Value1: "> Value2: "> Sum is: ">

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: "); for (int i = 0; i < rates.length-1; i++) { out.println(" " + displays[i] + " "); } out.println( " ");

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 DateFormat class 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: – DateFormat formatter = 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("txtDa te"));

14 Import Java Class Example: Display Current Date Time Import java.util.Date – Define a Date type variable: – Date currentDate = new Date(); Display in textbox using JSP expression: The time is: ">

15 Date to Date Format Import class: – – 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 Examples G Era designator Text AD y Year Year 1996; 96 M Month in year Month July; Jul; 07 w Week in year Number 27 W Week in month Number 2 D Day in year Number 189 d Day in month Number 10 F Day of week in month Number 2 E Day in week Text Tuesday; Tue a Am/pm marker Text PM H Hour in day (0-23) Number 0 k Hour in day (1-24) Number 24 K Hour in am/pm (0-11) Number 0 h Hour in am/pm (1-12) Number 12 m Minute in hour Number 30 s Second in minute Number 55 S Millisecond Number 978 z Time zone General time zone Pacific Standard Time; PST; G MT-08:00 Z Time zone RFC 822 time zone -0800

17 Example Enter a date:

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 +" "); out.print("Current date is: " + formatter.format(currentDate)+" "); out.print("Entered date is: " + formatter.format(myDate)+" "); out.print ("Days between = " + (currentDate.getTime()- myDate.getTime())/(24*60*60*1000)+" "); DateFormat yearFormat=new SimpleDateFormat("yyyy"); DateFormat monthFormat=new SimpleDateFormat("MM"); DateFormat weekDayFormat=new SimpleDateFormat("E"); out.print(yearFormat.format(currentDate)+" "); out.print(monthFormat.format(currentDate)+" "); out.print(weekDayFormat.format(currentDate)+" "); %>


Download ppt "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."

Similar presentations


Ads by Google