Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "DAT602 Database Application Development Lecture 16 Java Server Pages Part 2."— Presentation transcript:

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

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

3 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

4 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

5 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...

6 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

7 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

8 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

9 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

10 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

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

12 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

13 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

14 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

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

16 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

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

18 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

19 Beans and Form processing For instance, let us modify our GetName.html to also collect email 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 Email Age UserData.class

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

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

22 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

23 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: Email: Age: NextPage.jsp

24 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 e-mail address? <INPUT TYPE=TEXT NAME=email SIZE=20 VALUE=" "> What's your age? <INPUT TYPE=TEXT NAME=age SIZE=4 VALUE= >

25 Reference: http://www.jsptut.com/ http://www.roseindia.net/jsp/ Database Application Development - Lecture 16


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

Similar presentations


Ads by Google