1 / 20

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.

aran
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. 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); } %> <form name="fvForm" method="post" action="fvPage.jsp"> Enter present value: <input type="text" name="PV" value="<%=PV%>" /><br><br> Select interest rate: <select name="Rate"> <option value=.04>4%</option> <option value=.05>5%</option> <option value=.06>6%</option> <option value=.07>7%</option> <option value=.08>8%</option> </select><br><br> Select year: <br> <input type="radio" name="Year" value="10" />10-year<br> <input type="radio" name="Year" value=“20" />20-year<br> <input type="radio" name="Year" value="30" />30-year<br><br> Future value is: <input type="text" name="FV" value="<%=FV%>" /><br><br> <input type="submit" value="ComputeFV" name="btnCompute" /> <input type="hidden" name="IsPostBack" value="yes"> </form>

  11. Code Example <% String myPV, myRate, myYear,displayValue; double FV=0, PV=0, Rate, Year; NumberFormatnf = 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 ("<form name='fvForm' method='post' action='fvPage.jsp'>"); out.println( "Enter present value: <input type='text' name='PV' value='" + myPV + "' /><br><br>"); out.println( "Select interest rate: <select name='Rate'>"); for (double v=.04; v<=.08;v+=.01){ displayValue=v*100 + "%"; if (Math.abs(v-Rate)< 0.00001) out.println("<option selected value='" + v + "'>" + displayValue + "</option>"); else out.println("<option value='" + v + "'>" + displayValue + "</option>"); } out.println( "</select><br><br>"); out.println("Select year: <br>"); for (int v=10; v<=30;v+=10){ displayValue= v + "-year"; if (v==Year)out.println("<input type='radio' name='Year' checked value='" + v+ "'/>" + displayValue + "<br>"); else out.println("<input type='radio' name='Year' value='" + v+ "'/>" + displayValue + "<br>"); } out.println("<br>Future value is:<input type='text' name='FV' value='" + nf.format(FV) + "'/><br><br>"); out.println("<input type='submit' value='ComputeFV' name='btnCompute' />"); out.println("<input type='hidden' name='IsPostBack' value='yes'>"); }

  12. else { %> <form name="fvForm" method="post" action="fvPage.jsp"> Enter present value: <input type="text" name="PV" value="<%=PV%>" /><br><br> Select interest rate: <select name="Rate"> <option value=.04>4%</option> <option value=.05>5%</option> <option value=.06>6%</option> <option value=.07>7%</option> <option value=.08>8%</option> </select><br><br> Select year: <br> <input type="radio" name="Year" value="10" />10-year<br> <input type="radio" name="Year" value="20" />20-year<br> <input type="radio" name="Year" value="30" />30-year<br><br> Future value is: <input type="text" name="FV" value="<%=FV%>" /><br><br> <input type="submit" value="ComputeFV" name="btnCompute" /> <input type="hidden" name="IsPostBack" value="yes"> </form> <% } %> Note: Pay attention to the code after </form>

  13. 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.") }

  14. Converting between Numbers and Stringshttp://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: • Inti=5; • String s3 = Integer.toString(i); • Implicitly converted when concatenates with a string: out.println (“The sum is” + sum);

  15. 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};

  16. 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>");

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

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

  19. Creating a listbox of rates String display, rate; ArrayListrateValues = new ArrayList(); ArrayListrateDisplays=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: <select name='Rate'>"); for (inti = 0; i < rateValues.size(); i++) { rate= rateValues.get(i).toString(); display=rateDisplays.get(i).toString(); out.println("<option value='" + rate + "'>" + display + "</option>"); } out.println( "</select><br><br>");

More Related