Michelle Johnston, Firebird Services Ltd

Slides:



Advertisements
Similar presentations
8 Copyright © 2005, Oracle. All rights reserved. Creating the Web Tier: JavaServer Pages.
Advertisements

JSP -exercises- Alessandro Marchetto Fondazione Bruno Kessler-IRST, Centro per la Ricerca Scientifica e Tecnologica.
Servers- Apache Tomcat Server Server-side scripts- Java Server Pages Java Server Pages - Xue Bai.
CS4273: Distributed System Technologies and Programming I Lecture 11: JavaServer Pages (JSP)
Java Server Pages (JSP)
Java Server Pages Jeffrey Jongko. Introduction Java Server Pages (JSP) technology was created by Sun Microsystems and is built on top of Sun’s Java Servlet.
JSP1 Java Server Pages (JSP) Introducing JavaServer Pages TM (JSP TM ) JSP scripting elements.
 Copyright Wipro Technologies JSP Ver 1.0 Page 1 Talent Transformation Java Server Pages.
Using JavaServer Pages Harry R. Erwin, PhD CIT304/CSE301.
JSP: JavaServer Pages Juan Cruz Kevin Hessels Ian Moon.
28/1/2001 Seminar in Databases in the Internet Environment Introduction to J ava S erver P ages technology by Naomi Chen.
DT211/3 Internet Application Development
18-Jun-15 JSP Java Server Pages Reference: Tutorial/Servlet-Tutorial-JSP.html.
CS320 Web and Internet Programming JSP Scripting Elements and Page Directives Chengyu Sun California State University, Los Angeles.
JSP Java Server Pages Reference:
Multiple Tiers in Action
DT228/3 Web Development JSP: Directives and Scripting elements.
Java Server Pages Russell Beale. What are Java Server Pages? Separates content from presentation Good to use when lots of HTML to be presented to user,
Comp2513 Java Server Pages Daniel L. Silver, Ph.D.
1 CS6320 – JSP L. Grewe 2 Java Server Pages Servlets require you to write out entire page delivered with print statements Servlets require you to write.
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)
Java Server Pages (JSP) Presented by: Ananth Prasad & Alex Ivanov May 10, 2001.
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.
Java Server Pages CS-422. What are JSPs A logical evolution of java servlets –most servlets dynamically create HTML and integrate it with some computational.
Netbeans – jsp.zip Introduction to JSP Netbeans – jsp.zip.
Using JavaBeans and Custom Tags in JSP Lesson 3B / Slide 1 of 37 J2EE Web Components Pre-assessment Questions 1.The _____________ attribute of a JSP page.
JDeveloper 10g and JavaServer Faces: High-Performance UIs on the Web Avrom Roy-Faderman Senior Programmer May, 2006.
JSP Java Server Pages Softsmith Infotech.
Active Server Pages ASP is Microsoft’s server-side script engine for dynamically-generated web pages. Most common language used is VBScript. If you use.
Java Server Pages Lecture July Java Server Pages Java Server Pages (JSPs) provide a way to separate the generation of dynamic content (java)
 Embeds Java code  In HTML tags  When used well  Simple way to generate dynamic web-pages  When misused (complex embedded Java)  Terribly messy.
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.
Stanisław Osiński, 2002JSP – A technology for serving dynamic web content Java Server Pages™ A technology for serving dynamic web content Stanisław Osiński,
JAVA SERVER PAGES CREATING DYNAMIC WEB PAGES USING JAVA James Faeldon CS 119 Enterprise Systems Programming.
Introduction to JavaServer Pages. 2 JSP and Servlet Limitations of servlet  It’s inaccessible to non-programmers JSP is a complement to servlet  focuses.
DAT602 Database Application Development Lecture 16 Java Server Pages Part 2.
Chapter 7 Being a JSP. JSP introduction JSP is a solution for two issues  Servlet is difficult for HTML designers since they may not know Java  Formatting.
Intro to PHP IST2101. Review: HTML & Tags 2IST210.
JSP Fundamentals Elements of a JSP Using Beans with JSP Integrating Servlets and JSP.
Java Server Pages (JSP) Provide a cross-platform framework for creating dynamic web contents JSP Components: Static HTML/XML parts Special JSP Tags Snippets.
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.
Java Server Pages An introduction to JSP. Containers and Components Several clients – one system.
Chapter 11 Invoking Java Code with JSP Scripting Elements.
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.
CS562 Advanced Java and Internet Application Introduction to the Computer Warehouse Web Application. Java Server Pages (JSP) Technology. By Team Alpha.
COMP9321 Web Application Engineering Semester 2, 2015 Dr. Amin Beheshti Service Oriented Computing Group, CSE, UNSW Australia Week 3 1COMP9321, 15s2, Week.
STRUCTURE OF JSP PRESENTED BY: SIDDHARTHA SINGH ( ) SOMYA SHRIVASTAV ( ) SONAM JINDAL ( )
©SoftMoore ConsultingSlide 1 Overview of Servlets and JavaServer Pages (JSP)
Javascript Overview. What is Javascript? May be one of the most popular programming languages ever Runs in the browser, not on the server All modern browsers.
CSI 3125, Preliminaries, page 1 JSP (Java Server Pages)
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.
Chapter 6 Chapter 6 Server Side Programming (JSP) Part 1 1 (IS 203) WebProgramming (IS 203) Web Programming.
 Java Server Pages (JSP) By Offir Golan. What is JSP?  A technology that allows for the creation of dynamically generated web pages based on HTML, XML,
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.
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.
10 Copyright © 2004, Oracle. All rights reserved. Building ADF View Components.
JSP Java Server Pages. Hello, !
JSP java server pages.
JSP Based on
Developing JavaServer Pages
Java Server Pages.
Scripted Page Web App Development (Java Server Pages)
Chengyu Sun California State University, Los Angeles
Invoking Java Code from JSP
Java Server Pages.
Java Server Pages (JSP)
Presentation transcript:

Michelle Johnston, Firebird Services Ltd JSP Michelle Johnston, Firebird Services Ltd

JSP Pages HTML page can become a jsp just by changing the extension to jsp Allows java to be run within HTML pages

Directives <%@page language="java" %> <%@ include file="/header.jsp" %> <%@ taglib uri="tlds/taglib.tld" prefix="mytag" %> <%@page language="java" import="java.sql.*,mypackage.myclass" %> <%@page language="java" session="true" %> <%@page language="java" session="true" errorPage="error.jsp" %> <%@page language="java" session="true" contentType="text/html;charset=ISO-8859-1" %>

Declaratives Declarations are embedded within <!%= …%> <%@ page import="java.util.*" %> <HTML> <BODY> <%!     Date theDate = new Date();     Date getDate()     {         System.out.println( "In getDate() method" );         return theDate;     } %> Hello!  The time is now <%= getDate() %> </BODY> </HTML>

Declaratives Here we are declaring a variable theDate And the method getDate() Both will now be available in our scriptlets Warning! Declarations/declaratives are only evaluated ONCE per page – reload and the date remains the same!

Request method Get request info <%=request.getMethod()%> <%=request.getRequestURI()%> <%=request.getProtocol()%> <%=request.getPathInfo()%> <%=request.getQueryString()%>

Changing Date value <HTML> <BODY> Hello!  The time is now <%= new java.util.Date() %> </BODY> </HTML> Refresh the above page and see the date change!

Exercise 1 Exercise: Write a JSP to output the values returned by System.getProperty for various system properties such as java.version, java.home, os.name, user.name, user.home, user.dir etc.

Scriptlets Get a parameter <% //java code String userName=null; userName=request.getParameter("userName"); %>

Scriptlets <HTML> <BODY> <% // This is a scriptlet. Notice that the "date" // variable we declare here is available in the // embedded expression later on. System.out.println( "Evaluating date now" ); java.util.Date date = new java.util.Date(); %> Hello! The time is now <%= date %> </BODY> </HTML>

Outputting Info Note that System.out.println writes to stdout (console) To write to the html page, we put the value in <%= %> Another way would be to use out (an object 'given' to you that allows you to write to html)

Outputting Info <HTML> <BODY> <% // This scriptlet declares and initializes "date" System.out.println( "Evaluating date now" ); java.util.Date date = new java.util.Date(); %> Hello! The time is now // This scriptlet generates HTML output out.println( String.valueOf( date )); </BODY> </HTML>

Exercise 2 Exercise:  Write a JSP to output the entire line, "Hello!  The time is now ..." but use a scriptlet for the complete string, including the HTML tags.

Using Tables <TABLE BORDER=2> <% <%     for ( int i = 0; i < n; i++ ) {   %>         <TR>         <TD>Number</TD>         <TD><%= i+1%></TD>   </TR>         } %> </TABLE>

Exercise 3 Write a JSP to output all the values returned by System.getProperties with "<BR>" embedded after each property name and value.  Do not output the "<BR>" using the "out" variable.

Directives Don’t fully qualify classes, import them! <%@ page import="java.util.*" %> <HTML> <BODY> <%     System.out.println( "Evaluating date now" );     Date date = new Date(); %> Hello!  The time is now <%= date %> </BODY> </HTML>

Importing To import more than one package, simply comma separate them.. <%@ page import="java.util.*,java.text.*" %>

Includes Include puts the full text of the included file embedded into the jsp file (can be html/jsp/anything) <HTML> <BODY> Going to include hello.jsp... <BR> <%@ include file="hello.jsp" %> </BODY> </HTML>

Tags < not <% <some:tag> body </some:tag> <some:tag/> (no body to this tag – xml like) Predefined tags <jsp:.. <HTML> <BODY> Going to include hello.jsp... <BR> <jsp:include page="hello.jsp"/> </BODY> </HTML>

Exercise 4 Change the jsp:include to jsp:forward and note the difference. Write a JSP to do either a forward or an include, depending upon a boolean variable (hint:  The concepts of mixing HTML and scriptlets work with JSP tags also!)  

Calling JSP in Forms <HTML> <BODY> <FORM METHOD=POST ACTION="SaveName.jsp"> What's your name? <INPUT TYPE=TEXT NAME=username SIZE=20> <P><INPUT TYPE=SUBMIT> </FORM> </BODY> </HTML>

Creating a session SaveName.jsp saves the name in a session: <%    String name = request.getParameter( "username" );    session.setAttribute( "theName", name ); %> <HTML> <BODY> <A HREF="NextPage.jsp">Continue</A> </BODY> </HTML>

Retrieving Session Info NextPage.jsp shows how to retrieve the saved name. <HTML> <BODY> Hello, <%= session.getAttribute( "theName" ) %> </BODY> </HTML>

Exercise 5 Add another attribute age to the whole of this example (all three pages)

Form Processing Add email and age to the form in GetName.html <BODY> <FORM METHOD=POST ACTION="SaveName.jsp"> What's your name? <INPUT TYPE=TEXT NAME=username SIZE=20><BR> What's your e-mail address? <INPUT TYPE=TEXT NAME=email SIZE=20><BR> What's your age? <INPUT TYPE=TEXT NAME=age SIZE=4><P> <INPUT TYPE=SUBMIT> </FORM> </BODY> </HTML>

Using Beans Create a bean (Java class) with username, email and age as fields Create setter methods (setUsername, setEmail and setAge) Create getter methods (getUsername, getEmail and getAge)

UserData class public class UserData { String username; String email; int age;     public void setUsername( String value)     {         username = value;     }     public void setEmail( String value )     email = value;    

UserData Class cont public void setAge( int value ) { age = value; } }     public String getUsername() { return username; }     public String getEmail() { return email; }     public int getAge() { return age; } }

Bean Compilation Compile bean Put in the classpath of web server Now let us change "SaveName.jsp" to use a bean to collect the data. <jsp:useBean id="user" class="UserData" scope="session"/> <jsp:setProperty name="user" property="*"/>  <HTML> <BODY> <A HREF="NextPage.jsp">Continue</A> </BODY> </HTML>

Retrieving Bean Data Let us modify NextPage.jsp to retrieve the data from bean.. <jsp:useBean id="user" class="UserData" scope="session"/>  <HTML> <BODY> You entered<BR> Name: <%= user.getUsername() %><BR> Email: <%= user.getEmail() %><BR> Age: <%= user.getAge() %><BR> </BODY> </HTML>

Exercise 6 1)  Write a JSP/HTML set that allows a user to enter the name of a system property, and then displays the value returned by System.getProperty for that property name (handle errors appripriately.)   2)  Go back to the exercises where you manually modified boolean variables.  Instead of a boolean variable, make these come from a HIDDEN form field that can be set to true or false.