Server-Side Scripting with Java Server Page, JSP ISYS 350.

Slides:



Advertisements
Similar presentations
Lecture 6/2/12. Forms and PHP The PHP $_GET and $_POST variables are used to retrieve information from forms, like user input When dealing with HTML forms.
Advertisements

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.
 Copyright Wipro Technologies JSP Ver 1.0 Page 1 Talent Transformation Java Server Pages.
Chapter 51 Scripting With JSP Elements JavaServer Pages By Xue Bai.
DT228/3 Web Development WWW and Client server model.
Server-Side Scripting with Java Server Page, JSP.
Servlets and a little bit of Web Services Russell Beale.
Apache Tomcat Server – installation & use Server-side language-- use Java Server Pages Contrast Client-side languages HTML Forms Servers & Server-side.
Chapter 11 ASP.NET JavaScript, Third Edition. 2 Objectives Learn about client/server architecture Study server-side scripting Create ASP.NET applications.
Three types of scripting elements: 1.Expressions 2.Scriptlets 3.Declarations Scripting elements.
JSP Architecture  JSP is a simple text file consisting of HTML or XML content along with JSP elements  JSP packages define the interface for the compiled.
Computer Science 101 Web Access to Databases Overview of Web Access to Databases.
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.
Christopher M. Pascucci Basic Structural Concepts of.NET Browser – Server Interaction.
Form Handling, Validation and Functions. Form Handling Forms are a graphical user interfaces (GUIs) that enables the interaction between users and servers.
Java Server Pages B.Ramamurthy. Topics for Discussion 8/20/20152 Inheritance and Polymorphism Develop an example for inheritance and polymorphism JSP.
Overview of JSP Technology. The need of JSP With servlets, it is easy to – Read form data – Read HTTP request headers – Set HTTP status codes and response.
DAT602 Database Application Development Lecture 15 Java Server Pages Part 1.
ASHIMA KALRA.  INTRODUCTION TO JSP INTRODUCTION TO JSP  IMPLICIT OBJECTS IMPLICIT OBJECTS  COOKIES COOKIES.
M. Taimoor Khan * Java Server Pages (JSP) is a server-side programming technology that enables the creation of dynamic,
FALL 2005CSI 4118 – UNIVERSITY OF OTTAWA1 Part 4 Web technologies: HTTP, CGI, PHP,Java applets)
Comp2513 Forms and CGI Server Applications Daniel L. Silver, Ph.D.
AJAX and Java ISYS 350. AJAX Asynchronous JavaScript and XML: – Related technologies: JavaScript, Document Object Model, XML, server-side script such.
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 –
Java support for WWW Babak Esfandiari (sources: Qusay Mahmoud, Roger Impey, textbook)
Server-Side Scripting with Java Server Page, JSP ISYS 350.
Chapter 8 Cookies And Security JavaScript, Third Edition.
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.
JAVA SERVER PAGES CREATING DYNAMIC WEB PAGES USING JAVA James Faeldon CS 119 Enterprise Systems Programming.
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.
Mark Dixon 1 03 – Passing Data between pages: Forms, Sessions, & Query Strings.
Website Development with PHP and MySQL Saving Data.
Introduction to JavaServer Pages. 2 JSP and Servlet Limitations of servlet  It’s inaccessible to non-programmers JSP is a complement to servlet  focuses.
Chapter 6 Server-side Programming: Java Servlets
CSC 2720 Building Web Applications Server-side Scripting with PHP.
JSP Fundamentals Elements of a JSP Using Beans with JSP Integrating Servlets and JSP.
1 MSCS 237 Overview of web technologies (A specific type of distributed systems)
Web App GUI: JSP Basics & Forms 3680 Enterprise Programming.
Jsp (Java Server Page) Is a server side program.
Java server pages. A JSP file basically contains HTML, but with embedded JSP tags with snippets of Java code inside them. A JSP file basically contains.
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.
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 –
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.
COMP9321 Web Application Engineering Semester 2, 2015 Dr. Amin Beheshti Service Oriented Computing Group, CSE, UNSW Australia Week 3 1COMP9321, 15s2, Week.
8 Chapter Eight Server-side Scripts. 8 Chapter Objectives Create dynamic Web pages that retrieve and display database data using Active Server Pages Process.
1 State and Session Management HTTP is a stateless protocol – it has no memory of prior connections and cannot distinguish one request from another. The.
STRUCTURE OF JSP PRESENTED BY: SIDDHARTHA SINGH ( ) SOMYA SHRIVASTAV ( ) SONAM JINDAL ( )
©SoftMoore ConsultingSlide 1 Overview of Servlets and JavaServer Pages (JSP)
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 21 Java Servlets Wed. 11/22/00 based on material.
MIT AITI 2004 JSP – Lecture 4 Sessions and Cookies.
Server-Side Scripting with Java Server Page, JSP.
1 Java Server Pages A Java Server Page is a file consisting of HTML or XML markup into which special tags and code blocks are inserted When the page is.
Servers- Apache Tomcat Server Server-side scripts- Java Server Pages.
Server-Side Scripting with Java Server Page, JSP ISYS 350.
Server-Side Scripting with Java Server Page, JSP ISYS 350.
Chapter 6 Chapter 6 Server Side Programming (JSP) Part 1 1 (IS 203) WebProgramming (IS 203) Web Programming.
Java Programming: Advanced Topics 1 Building Web Applications Chapter 13.
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.
Session 11: Cookies, Sessions ans Security iNET Academy Open Source Web Development.
World Wide Web has been created to share the text document across the world. In static web pages the requesting user has no ability to interact with the.
Net-centric Computing Servlets & JSP. Lecture Outline  Tracking Sessions  Cookies  Examples  JSP  Differences between JSP and Servlets  JSP Constructs.
Database Processing with JSP ISYS 350. Database Applications Applications Database Server Queries/Updates Results.
Java Class and Servlet ISYS 350.
JSP Implicit Objects Implicit Objects in JSP are objects that are automatically available in JSP. request: The request object retrieves the values that.
Server-Side Scripting with Java Server Page, JSP
Server-Side Scripting with Java Server Page, JSP
AJAX and JSP ISYS 350.
Java Servlet ISYS 350.
AJAX and JSP ISYS 350.
Presentation transcript:

Server-Side Scripting with Java Server Page, JSP ISYS 350

Java Website Java Tutorial: – JSP, Servlet tutorial: – – gx.html gx.html –

Java Platforms Java SE (Standard Edition) provides the core functionality of the Java programming language. It defines everything from the basic types and objects of the Java programming language to high-level classes that are used for networking, security, database access, etc. Java EE (Enterprise Edition) is built on top of the Java SE platform for developing and running large-scale, multi- tiered, scalable, reliable, and secure network applications. Java ME (Mobile Edition) provides an API and a small- footprint virtual machine for running Java programming language applications on small devices, like mobile phones.

Using Java Classes To develop Java applications, we need to use many different Java classes. Groups of related Java classes are organized into packages. To use a class from a package we use the “import” statement to import the class.

Hyper Text Transfer Protocol: Request & Response Web Server Browser HTTP Request HTTP Response Web Application

Data Sent with Request and Response Request: – requested URL, cookies, queryString, data from a form, etc. Response: – Web page content in HTML code – Cookies – Etc.

Java Server Pages, JSP Java Server Pages (JSP) is a technology that lets you mix regular, static HTML with dynamically- generated HTMLrequest: JSP objects: – Request object: retrieves the values that the client browser passed to the server during an HTTP request – Response object: This denotes the HTTP Response data. – Session: This denotes the data associated with a specific session of user. The main use of Session Objects is for maintaining states when there are multiple page requests. – Out object: print, println

Methods of request Object getParameter(String name): Returns the value of a request parameter as a String, or null if the parameter does not exist. getQueryString(): Gets any query string that is part of the HTTP request URI. getCookies(): Gets the array of cookies found in this request. getRequestURI(): Gets the URI to the current JSP page. etc.

Methods of response Object addCookie(Cookie): Adds the specified cookie to the response. It can be called multiple times to set more than one cookie. sendRedirect(String): Sends a temporary redirect response to the client using the specified redirect location URL.

JSP Out Object println: The println method of out object is used to write the value to the output, including the newline character. – Ex. out.println("The Output is:" + ex); print: The print method of out object writes the value to the output without a newline character. out.print("Welcome!!!"); out.print("To JSP Training"); Output is: Welcome!!! To JSP Training

Use JSP to Process a Form

FORM Form attribute: – Method: Preferred method is: Post – Action: Specify the URL of a program on a server or an address to which a form’s data will be submitted. – Method: Post/Get Button: – Type = “submit”

In addition to the eight primitive data types listed above, the Java programming language also provides special support for character strings via the java.lang. String class. String s = "this is a string";.

Compute the Sum of 2 Numbers:

Form Example Compute the sum of two numbers Enter num1: Enter num2:

Example of JSP scriptlet Compute the Sum of 2 Numbers: <% String value1, value2; double n1, n2, sum; value1=request.getParameter("num1"); value2=request.getParameter("num2"); n1= Double.parseDouble(value1); n2= Double.parseDouble(value2); sum=n1+n2; out.println("The sum is:" + sum); %> Note 1: Double is an object that offers the parseDouble method, not “double” data type. Note 2: “out” is an implicit object that does not need to be declared. It is already predefined.

Writing HTML code as output <% String value1, value2; double n1, n2, sum; value1=request.getParameter("num1"); value2=request.getParameter("num2"); n1= Double.parseDouble(value1); n2= Double.parseDouble(value2); sum=n1+n2; out.println(" Value1: "); out.println(" Value2: "); out.println("The sum is: " ); %>

Embed JSP code in HTML Using JSP Expression: <% String value1, value2; double n1, n2, sum; value1=request.getParameter("num1"); value2=request.getParameter("num2"); n1= Double.parseDouble(value1); n2= Double.parseDouble(value2); sum=n1+n2; %> Value1: "> Value2: "> Sum is: ">

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

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

Compute Future Value: Process form with various controls

Form Code Enter present value: Select interest rate: 4% 5% 6% 7% 8% Select year: 10-year 15-year 30-year

JSP Code Example <% String myPV, myRate, myYear; myPV=request.getParameter("PV"); myRate=request.getParameter("Rate"); myYear=request.getParameter("Year"); double FV, PV, Rate, Year; PV=Double.parseDouble(myPV); Rate=Double.parseDouble(myRate); Year=Double.parseDouble(myYear); FV=PV*Math.pow(1+Rate,Year); out.println("FutureValue is:"+ FV); %>

Use Client-Side Validating and Sumbit Validated Form function Validating(){ var Valid; Valid=true; if (document.fvForm.PV.value=="") {Valid=false;} if (Valid==false){alert("Cannot contain blank");} else {document.fvForm.submit();} } Note: Must use a standard button, not submit button:

Depreciation Table Straight Line Depreciation Table Enter Property Value: Enter Property Life:

Output

<% String strValue, strLife; strValue=request.getParameter("pValue"); strLife=request.getParameter("pLife"); double value, life, depreciation,totalDepreciation=0; value=Double.parseDouble(strValue); life=Double.parseDouble(strLife); NumberFormat nf = NumberFormat.getCurrencyInstance(); out.println("Straight Line Depreciation Table" + " "); out.println("Property Value: "); out.println("Property Life: "); depreciation=value/life; totalDepreciation=depreciation; out.println( " "); out.println(" Year Value at BeginYr "); out.println(" Dep During Yr Total to EndOfYr "); out.println(" "); for (int count = 1; count <= life; count++) { out.write(" "); out.write(" " + count + " "); out.write(" " + nf.format(value) + " "); out.write(" " + nf.format(depreciation) + " "); out.write(" " + nf.format(totalDepreciation) + " "); value -= depreciation; totalDepreciation+=depreciation; } %>

Number to Currency Format Import class: – Define a format: – NumberFormat nf = NumberFormat.getCurrencyInstance(); Convert: – Example: Display payment with currency format: nf.format(payment)

Using JSP Expression <% String strValue, strLife; strValue=request.getParameter("pValue"); strLife=request.getParameter("pLife"); double value, life, depreciation,totalDepreciation=0; value=Double.parseDouble(strValue); life=Double.parseDouble(strLife); NumberFormat nf = NumberFormat.getCurrencyInstance(); depreciation=value/life; totalDepreciation=depreciation; %> Straight Line Depreciation Table Property Value: ' /> Property Life: '/> Year Value at BeginYr Dep During Yr Total to EndOfYr <% for (int count = 1; count <= life; count++) { %> <% value -= depreciation; totalDepreciation+=depreciation; } %>

Display Percentage Format: Import class: double rate = ; NumberFormat pFormat = NumberFormat.getPercentInstance(); pFormat.setMinimumFractionDigits(2); out.println("Percent format: " + pFormat.format(rate));

Cookie Cookie is a small data file added by a website to reside in user’s system. Define a cookie: – new Cookie(“Key”, “value”); – Ex. Cookie cookieCID = new Cookie ("cookieCID",CID); Write a cookie: – response.addCookie(cookieCID);

Example: <% String CID, Cname; CID=request.getParameter("CID"); Cname=request.getParameter("Cname"); Cookie cookieCID = new Cookie ("cookieCID",CID); response.addCookie(cookieCID); Cookie cookieCname = new Cookie ("cookieCname",Cname); response.addCookie(cookieCname); out.println("CID cookie= " + CID + "added"); out.println("Cname cookie= " + Cname + "added"); %> Note: To view cookies with FireFox:Tools/Options/Privacy/Remove cookies Enter CID: Enter Cname:

Reading Cookies <% Cookie[] cookies = request.getCookies(); out.println(cookies[0].getName() + cookies[0].getValue() + " "); out.println(cookies[1].getName() + cookies[1].getValue() + " "); %> Use request.getCookies() method. This method retrieve cookies and return them in an array.

Expire a cookie Add a expiration time: – cookieCID setMaxAge(N); N is # of seconds N = 0, the cookie will be deleted right way N < 0 : the cookie will be deleted once the user exit the browser Cookie cookieCID = new Cookie ("cookieCID",CID); cookieCID.setMaxAge(-1);

JSP Session Object Session Object denotes the data associated with a specific session of user. Methods: – session.setAttribute(“Key”, object) add an object to the session object associated with a key. – session.getId() return the unique identifier associated with the session – session.getAttribute(“key”) return the object with the specified key given in parameter

Add an object to session object Add Cname to session, then redirect to another page to read the cname <% Cookie[] cookies = request.getCookies(); out.println(cookies[0].getName() + cookies[0].getValue() + " "); out.println(cookies[1].getName() + cookies[1].getValue() + " "); session.setAttribute("Cname", cookies[1].getValue()); response.sendRedirect("readSession.jsp"); %>

Retrieve an object from session <% Object custName; custName=session.getAttribute("Cname"); out.println("Welcome, " + custName.toString()); %>