DAT602 Database Application Development Lecture 16 Java Server Pages Part 2.

Slides:



Advertisements
Similar presentations
Michelle Johnston, Firebird Services Ltd
Advertisements

Presenter: James Huang Date: Sept. 29,  HTTP and WWW  Bottle Web Framework  Request Routing  Sending Static Files  Handling HTML  HTTP Errors.
Java Server Pages (JSP)
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.
 Copyright Wipro Technologies JSP Ver 1.0 Page 1 Talent Transformation Java Server Pages.
Bin Fu 03/27/ Spring 2012 Homework 7 Building A Web Application School of Computer Science.
Chapter 51 Scripting With JSP Elements JavaServer Pages By Xue Bai.
18-Jun-15 JSP Java Server Pages Reference: Tutorial/Servlet-Tutorial-JSP.html.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 34 Servlets.
Session Management A290/A590, Fall /25/2014.
Three types of scripting elements: 1.Expressions 2.Scriptlets 3.Declarations Scripting elements.
CMPUT 391 – Database Management Systems Department of Computing Science University of Alberta CMPUT 391 Database Management Systems JavaServer Pages (JSP)
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.
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.
Sys Prog & Scripting - HW Univ1 Systems Programming & Scripting Lecture 15: PHP Introduction.
Advance Database Management Systems Lab no. 5 PHP Web Pages.
Reading Data in Web Pages tMyn1 Reading Data in Web Pages A very common application of PHP is to have an HTML form gather information from a website's.
LING 408/508: Programming for Linguists Lecture 17 October 21 st.
Chapter 16 The World Wide Web Chapter Goals Compare and contrast the Internet and the World Wide Web Describe general Web processing Describe several.
State Management. What is State management Why State management ViewState QueryString Cookies.
JSP Java Server Pages Softsmith Infotech.
Java Servlets and Java Server Pages Carol Wolf Computer Science.
Java Server Pages Lecture July Java Server Pages Java Server Pages (JSPs) provide a way to separate the generation of dynamic content (java)
CSCI 6962: Server-side Design and Programming Introduction to Java Server Faces.
 Embeds Java code  In HTML tags  When used well  Simple way to generate dynamic web-pages  When misused (complex embedded Java)  Terribly messy.
Chapter 8 Script-free pages. Problem with scripting in JSP When you use scripting (declaration, scriplet, expressions) in your JSP, you actually put Java.
JSP Most of the web developers deploying web applications using servlets mixes the presentation logic and business logic. Separation of business logic.
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.
Week seven CIT 354 Internet II. 2 Objectives Database_Driven User Authentication Using Cookies Session Basics Summary Homework and Project 2.
Maintaining State MacDonald Ch. 9 MIS 324 MIS 324 Professor Sandvig Professor Sandvig.
PHP1-1 PHP Lecture 2 Xingquan (Hill) Zhu
Chapter 6 Server-side Programming: Java Servlets
Writing Enterprise Applications with J2EE (Fourth lesson) Alessio Bechini June 2002 (based on material by Monica Pawlan)
Topics Sending an Multipart message Storing images Getting confirmation Session tracking using PHP Graphics Input Validators Cookies.
® IBM Software Group © 2007 IBM Corporation Best Practices for Session Management
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.
Leman Akoglu 11/11/ Fall 2009 Recitation Homework 9 Building A Web Application Phase-II School of Computer Science.
CSCI 6962: Server-side Design and Programming Java Server Faces Scoping and Session Handling.
Introduction to JavaScript CS101 Introduction to Computing.
OOSSE Week 8 JSP models Format of lecture: Assignment context JSP models JSPs calling other JSPs i.e. breaking up work Parameter passing JSPs with Add.
COMP9321 Web Application Engineering Semester 2, 2015 Dr. Amin Beheshti Service Oriented Computing Group, CSE, UNSW Australia Week 3 1COMP9321, 15s2, Week.
Creating a Java Application and Applet
Since you’ll need a place for the user to enter a search query. Every form must have these basic components: – The submission type defined with the method.
PHP and Sessions. Session – a general definition The GENERAL definition of a session in the “COMPUTER WORLD” is: The interactions (requests and responses)
MIT AITI 2004 JSP – Lecture 4 Sessions and Cookies.
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.
Bayu Priyambadha, S.Kom. Static content  Web Server delivers contents of a file (html) 1. Browser sends request to Web Server 3. Web Server sends HTML.
8-Mar-16 More About Servlets Session Tracking. Persistent information A server site typically needs to maintain two kinds of persistent (remembered) information:
Java Server Pages Yes it is Java. What you should know HTML Java.
JavaScript: A short introduction Joseph Lee Created by Joseph Lee.
Net-centric Computing Servlets & JSP. Lecture Outline  Tracking Sessions  Cookies  Examples  JSP  Differences between JSP and Servlets  JSP Constructs.
Separating Programming and Presentation: JSP Technology WEB TECHNOLOGIES A COMPUTER SCIENCE PERSPECTIVE JEFFREY C. JACKSON Jackson, Web Technologies: A.
Unit 4 Working with data. Form Element HTML forms are used to pass data to a server. A form can contain input elements like text fields, checkboxes, radio-buttons,
Section 10.1 Define scripting
JSP java server pages.
JSP Based on
COMP9321 Web Application Engineering Semester 2, 2017
Java Servlets.
Scripted Page Web App Development (Java Server Pages)
Scope and State Handling in JSPs
Chapter 6 Server-side Programming: Java Servlets
15-415/615 Spring 2013 Homework 7 Building A Web Application
PHP / MySQL Introduction
Intro to PHP & Variables
Client side & Server side scripting
Tutorial 10: Programming with javascript
Scripted Page Web Application Development (Java Server Pages)
Presentation transcript:

DAT602 Database Application Development Lecture 16 Java Server Pages Part 2

JSP Declarations You can also add variable and method declarations to this class. You can then use these variables and methods from your scriptlets and expressions. To add a declaration, you must use the sequences to enclose your declarations, as shown below. Database Application Development - Lecture 16 <%! Date theDate = new Date(); Date getDate() { System.out.println( "In getDate() method" ); return theDate; } %> Hello! The time is now

JSP Declarations The variables be declared in will be kept until JSP program be terminated or server restart. What value will be when you reload the page? Database Application Development - Lecture 16

JSP Tags JSP tags do not use <%, but just the < character. A JSP tag is somewhat like an HTML tag. If the tag does not require a body, the start and end can be conveniently merged together, as Database Application Development - Lecture 16

JSP Tags: Tags can be of two types: loaded from an external tag library, or predefined tags. Predefined tags start with jsp:characters. For instance, jsp:include is a predefined tag that is used to include other pages. Another very useful tag is jsp:forward. Database Application Development - Lecture 16 Going to include hello.jsp...

Retrieving the data posted to a JSP file from HTML To retrieve the value entered by the user we uses the request.getParameter(“ParameterName”); Database Application Development - Lecture 16

JSP Sessions Why we need session? On a typical web site, a visitor might visit several pages and perform several interactions. If you are programming the site, it is very helpful to be able to associate some data with each visitor. For this purpose, "session"s can be used in JSP. For security reason, we use session to identify users status: authorized or unauthorized. Database Application Development - Lecture 16

JSP Sessions What is session? A session is an object associated with a visitor. Data can be put in the session and retrieved from it, much like a Hashtable. A different set of data is kept for each visitor to the site. Database Application Development - Lecture 16

JSP Sessions In this instance, user can input user name in GetName.html, and GetName.html will sent this user name to SaveName.jsp, then SaveName.jsp will save given user name into session. In NextPage.jsp, you can read the user name from session. Database Application Development - Lecture 16 GetName.htmlSaveName.jsp username NextPage.jsp Read session.theNameGet user namePut user name into session

What's your name? Database Application Development - Lecture 16 <% String name = request.getParameter( "username" ); session.setAttribute( "theName", name ); %> Continue GetName.html SaveName.jsp JSP Sessions

NextPage.jsp shows how to retrieve the saved name. Database Application Development - Lecture 16 Hello,

JSP Sessions If you bring up two different browsers (not different windows of the same browser), or run two browsers from two different machines, you can put one name in one browser and another name in another browser, and both names will be kept track of. The session is kept around until a timeout period. Then it is assumed the user is no longer visiting the site, and the session is discarded. Database Application Development - Lecture 16

Cookie What is cookie? Why we need it? Cookie just likes session, it can be used for storing information. But there are two differences between cookie and session: 1.Cookie is stored in client side, session stored in server side. 2.Life cycles of them are different. Session only exists when service is running (browser is not closed), The cookies are saved to clients hard disk in the form of small text file. Database Application Development - Lecture 16

Cookie In JSP cookie are the object of the class javax.servlet.http.Cookie. Simple example of using cookie Database Application Development - Lecture 16 GetName.htmlSetCookie.jsp ShowCookieValue.jsp Get user inputted name Get passed name and save it into cookie Read stored value from cookie

Enter Your Name: Database Application Development - Lecture 16 GetName.html Cookie

Database Application Development - Lecture 16 <% String username=request.getParameter("username"); if(username==null) username=""; Cookie cookie = new Cookie ("username",username); cookie.setMaxAge(365 * 24 * 60 * 60); Cookie pcookie = new Cookie("); response.addCookie(cookie); %> Next Page to view the cookie value SetCookie.jsp

No Cookie found with the name Welcome:. Database Application Development - Lecture 16 Cookie ShowCookieValue.jsp

Beans and Form processing Forms are a very common method of interactions in web sites. JSP makes forms processing specially easy. The standard way of handling forms in JSP is to define a "bean“. You just need to define a class that has a field corresponding to each field in the form. The class fields must have "setters" that match the names of the form fields. Database Application Development - Lecture 16

Beans and Form processing For instance, let us modify our GetName.html to also collect address and age. Database Application Development - Lecture 16 GetName.htmlSaveName.jspNextPage.jsp Read info from bean in session Get inputted infoPut info into bean “UserData” Name Age UserData.class

GetName.jsp What's your name? What's your address? What's your age? Database Application Development - Lecture 16

package user; public class UserData { String username; String ; int age; public void setUsername( String value ) { username = value; } public void set ( String value ) { = value; } public void setAge( int value ) { age = value; } public String getUsername() { return username; } public String get () { return ; } public int getAge() { return age; } } Database Application Development - Lecture 16

Beans and Form processing The useBean tag will look for an instance of the "user.UserData" in the session. If the instance is already there, it will update the old instance. Otherwise, it will create a new instance of user.UserData (the instance of the user.UserData is called a bean), and put it in the session. The setProperty tag will automatically collect the input data, match names against the bean method names, and place the data in the bean! Database Application Development - Lecture 16 Continue SaveName.jsp

Bean and Form processing The bean is available as the variable named "user" of class "user.UserData". The data entered by the user is all collected in the bean. Database Application Development - Lecture 16 You entered Name: Age: NextPage.jsp

Techniques for form editing Sometimes we want to edit the values of a form. You must edit each HTML tag yourself, and put in a default value. Following example shows how to set default value to form of GetName.jsp. Database Application Development - Lecture 16 What's your name?<INPUT TYPE=TEXT NAME=username SIZE=20 VALUE=" "> What's your address? <INPUT TYPE=TEXT NAME= SIZE=20 VALUE=" "> What's your age? <INPUT TYPE=TEXT NAME=age SIZE=4 VALUE= >

Reference: Database Application Development - Lecture 16