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.

Slides:



Advertisements
Similar presentations
Why not just use Arrays? Java ArrayLists.
Advertisements

STRING AN EXAMPLE OF REFERENCE DATA TYPE. 2 Primitive Data Types  The eight Java primitive data types are:  byte  short  int  long  float  double.
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.
Java Classes ISYS 350. Introduction to Classes A class is the blueprint for an object. – It describes a particular type of object. – It specifies the.
Java review and more. Class Header  Public class Welcome  Case sensitive  Body of the class must be enclosed by braces.
Server-Side Scripting with Java Server Page, JSP.
Programming with Collections Collections in Java Using Arrays Week 9.
Loops Notes adapted from Dr. Flores. It repeats a set of statements while a condition is true. while (condition) { execute these statements; } “while”
11-1 Chapter 11 2D Arrays Asserting Java Rick Mercer.
Java Syntax Primitive data types Operators Control statements.
CS 106 Introduction to Computer Science I 02 / 20 / 2008 Instructor: Michael Eckmann.
Arrays, Loops weeks 4-6 (change from syllabus for week 6) Chapter 4.
PHP Server-side Programming. PHP  PHP stands for PHP: Hypertext Preprocessor  PHP is interpreted  PHP code is embedded into HTML code  interpreter.
Java Servlet. What is Java Servlet? A Servlet is a java based server side web technology. It is a java class that serves a client request and receives.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
AJAX and Java ISYS 350. AJAX Asynchronous JavaScript and XML: – Related technologies: JavaScript, Document Object Model, XML, server-side script such.
1 Programming Section 11 James King 12 August 2003.
Server-Side Scripting with JSP (2) ISYS 350. Java Array Examples of declaring an array: – int[] anArray = new int[10]; 10 elements index from 0 to 9 –
Server-Side Scripting with Java Server Page, JSP ISYS 350.
Announcements  I will discuss the labtest and the written test #2 common mistakes, solution, etc. in the next class  not today as I am still waiting.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Server-Side Scripting with Java Server Page, JSP ISYS 350.
JAVA SERVER PAGES. 2 SERVLETS The purpose of a servlet is to create a Web page in response to a client request Servlets are written in Java, with a little.
Basic Java Programming CSCI 392 Week Two. Stuff that is the same as C++ for loops and while loops for (int i=0; i
GENERIC COLLECTIONS. Type-Wrapper Classes  Each primitive type has a corresponding type- wrapper class (in package java.lang).  These classes are called.
Java Arrays …………. Java Arrays …………. * arrays are objects in Java * arrays are objects in Java * an array variable is a reference * an array variable is.
Pemrograman Dasar Arrays PTIIK - UB. Arrays  An array is a container object that holds a fixed number of values of a single type.  The length of an.
Arrays and ArrayLists in Java L. Kedigh. Array Characteristics List of values. A list of values where every member is of the same type. Each member in.
Chapter 8: Collections: Arrays. 2 Objectives One-Dimensional Arrays Array Initialization The Arrays Class: Searching and Sorting Arrays as Arguments The.
Arrays BCIS 3680 Enterprise Programming. Overview 2  Array terminology  Creating arrays  Declaring and instantiating an array  Assigning value to.
Java Classes ISYS 350. Introduction to Classes Two basic uses of class: – 1. A class is a way to organize functions (methods) to perform calculations.
COP2800 – Computer Programming Using JAVA University of Florida Department of CISE Spring 2013 Lecture 10 – Programming with Java Datatypes Type Casting.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Applications Development
Server-Side Scripting with PHP ISYS 475. PHP Manual Website
Server-Side Scripting with JSP (2) ISYS 350. Java Array Examples of declaring an array: – int[] anArray = new int[10]; 10 elements index from 0 to 9 –
Review and Java Questions 13 June Classes, objects, attributes, methods In Java, by convention a class name begins with Uppercase: Coffee, String.
Arrays Chapter 13 How to do the following with a one dimensional array: Declare it, use an index.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
Java Servlet ISYS 350. What is Java Servlet? It is a java class that serves a client request. Servlets are most often used to: Process or store data that.
Array and ArrayList ISYS 350. Array An array allows you to store a group of items of the same type together. Processing a large number of items in an.
Converting string to integer class StringToInt { public static void main (String arg[]) { // Assume arg[0] is the input string int result = 0, pos; int.
Server-Side Scripting with Java Server Page, JSP.
Chapter 3 Numerical Data. Objectives After you have read and studied this chapter, you should be able to Select proper types for numerical data. Write.
COMPUTER PROGRAMMING 2 ArrayLists. Objective/Essential Standard Essential Standard 3.00Apply Advanced Properties of Arrays Essential Indicator 3.02 Apply.
Server-Side Scripting with Java Server Page, JSP ISYS 350.
An Introduction to Java – Part 1 Erin Hamalainen CS 265 Sec 001 October 20, 2010.
Server-Side Scripting with Java Server Page, JSP ISYS 350.
Chapter 5: Arrays in Java. The objectives of this chapter are:  1. To discuss the creation and use of Arrays.   2. To continue to use the String class.
Computer Programming Arrays 1. Question #1 2 Question Choose the correct answer..
100e Hosted by vhs © Don Link, Indian Creek School, 2004 Jeopardy.
Arrays Chap. 9 Storing Collections of Values 1. Introductory Example Problem: Teachers need to be able to compute a variety of grading statistics for.
First Steps in PHP Creating Very Simple PHP Scripts SoftUni Team Technical Trainers Software University
Java Class and Servlet ISYS 350.
Review of Java … or maybe not.
Java String and Date ISYS 350.
AJAX and Java Servlet ISYS 350.
Exercise on Java Basics
JSP Implicit Objects Implicit Objects in JSP are objects that are automatically available in JSP. request: The request object retrieves the values that.
Java Programming: From Problem Analysis to Program Design, 4e
An Introduction to Java – Part I
Server-Side Scripting with Java Server Page, JSP
Server-Side Scripting with Java Server Page, JSP
AJAX and JSP ISYS 350.
Exercise on Java Basics
Unit-2 Objects and Classes
Java Array ISYS 350.
Object Oriented Programming in java
Java Servlet ISYS 350.
AJAX and JSP ISYS 350.
Presentation transcript:

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. – First time: present an empty form – Post back: present the results.

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

When the page is first loaded, IsPostBack will have a null value because the page has not posted back yet.

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

Use JSP to Present and Process a Form

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: <% } %>

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: ">

Compute Future Value Problem: We don’t see the value of rate and year that computes the output.

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: " />

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

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

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

Converting between Numbers and Strings 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);

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

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

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.

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)); }

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