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 Compute Future Value Problem: We don’t see the value of rate and year that computes the output.

10 fvPage.jsp <% String myPV, myRate, myYear; double FV=0, PV=0, Rate, Year; if (request.getParameter("IsPostBack")!=null) { myPV=request.getParameter("PV"); myRate=request.getParameter("Rate"); myYear=request.getParameter("Year"); PV=Double.parseDouble(myPV); Rate=Double.parseDouble(myRate); Year=Double.parseDouble(myYear); FV=PV*Math.pow(1+Rate,Year); } %> Enter present value: " /> Select interest rate: 4% 5% 6% 7% 8% Select year: 10-year 20-year 30-year Future value is: " />

11

12 Code Example <% String myPV, myRate, myYear,displayValue; double FV=0, PV=0, Rate, Year; NumberFormat nf = NumberFormat.getCurrencyInstance(); if (request.getParameter("IsPostBack")!=null) { myPV=request.getParameter("PV"); myRate=request.getParameter("Rate"); myYear=request.getParameter("Year"); PV=Double.parseDouble(myPV); Rate=Double.parseDouble(myRate); Year=Double.parseDouble(myYear); FV=PV*Math.pow(1+Rate,Year); out.println (" "); out.println( "Enter present value: "); out.println( "Select interest rate: "); for (double v=.04; v<=.08;v+=.01){ displayValue=v*100 + "%"; if (Math.abs(v-Rate) " + displayValue + " "); else out.println(" " + displayValue + " "); } out.println( " "); out.println("Select year: "); for (int v=10; v<=30;v+=10){ displayValue= v + "-year"; if (v==Year)out.println(" " + displayValue + " "); else out.println(" " + displayValue + " "); } out.println(" Future value is: "); out.println(" "); }

13 else { %> Enter present value: " /> Select interest rate: 4% 5% 6% 7% 8% Select year: 10-year 20-year 30-year Future value is: " /> <% } %> Note: Pay attention to the code after

14 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)) { // this line WILL print out.println("The two strings are the same.") }

15 Converting between Numbers and Strings http://docs.oracle.com/javase/tutorial/java/data/converting.html Converting Strings to Numbers – The Number subclasses that wrap primitive numeric types ( Byte, Integer, Double, Float, Long, and Short) each provide a method named parseXXXX() method. For example: PV=Double.parseDouble(myPV); Converting Numbers to Strings: – Each of the Number subclasses includes a class method, toString(), that will convert its primitive type to a string. For example: Int i=5; String s3 = Integer.toString(i); – Implicitly converted when concatenates with a string: out.println (“The sum is” + sum);

16 Java Array Examples of declaring an array: – int[] anArray = new int[10];; – double[] anArrayOfDoubles = new double[10]; – String[] anArrayOfStrings = new String[10]; – int[] anArray = { 100, 200, 300, 400, 500, 600, 700, 800, 900, 1000};

17 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( " ");

18 Java ArrayList Need: java.util.* package or java.util.ArrayList Defining ArrayList with interest rates: – ArrayList rateValues = new ArrayList(); Adding member to the ArrayList: – rateValues.add(0.03); Checking size of ArrayList: – rateValues.size() Note: Members of the ArrayList are objects.

19 Retrieving Item from arrayList in a loop using ArrayList’s get(index) method for (int i = 0; i < rateValues.size(); i++) { out.println(rateValues.get(i)); }

20 Creating a listbox of rates String display, rate; ArrayList rateValues = new ArrayList(); ArrayList rateDisplays=new ArrayList(); rateValues.add(0.03); rateDisplays.add("3%"); rateValues.add(0.04); rateDisplays.add("4%"); rateValues.add(0.05); rateDisplays.add("5%"); rateValues.add(0.06); rateDisplays.add("6%"); rateValues.add(0.07); rateDisplays.add("7%"); out.println( "Select interest rate: "); for (int i = 0; i < rateValues.size(); i++) { rate= rateValues.get(i).toString(); display=rateDisplays.get(i).toString(); out.println(" " + display + " "); } out.println( " ");


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