JSP – Java Server Pages Part 2 Representation and Management of Data on the Internet.

Slides:



Advertisements
Similar presentations
JSP and Servelets.
Advertisements

Apache Struts Technology
© Yaron Kanza Advanced Java Server Pages Written by Dr. Yaron Kanza, Edited by permission from author by Liron Blecher.
Expression Language Lec Umair Javed©2006 Generating Dynamic Contents Technologies available  Servlets  JSP  JavaBeans  Custom Tags  Expression.
 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.
CS320 Web and Internet Programming Java Beans and Expression Language (EL) Chengyu Sun California State University, Los Angeles.
JSP – Java Server Pages Part 1 Representation and Management of Data on the Internet.
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.
DT228/3 Web Development JSP: Directives and Scripting elements.
JSP – Java Server Pages Representation and Management of Data on the Internet.
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.
Java Enterprise Edition Java Web Development Structure of a web project Introduction to Web Applications The first project Introduction to Java Web Development.
Struts 2.0 an Overview ( )
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.
1 CIS336 Website design, implementation and management (also Semester 2 of CIS219, CIS221 and IT226) Lecture 9 JavaServer Pages (JSP) (Based on Møller.
1 JSP – Java Server Pages: The Gory Details 10 fake-points (which aren’t worth anything) to whoever: 1)spots the quote in this slide and names its source.
© Yaron Kanza Server-Side Programming using Java Server Pages Written by Dr. Yaron Kanza, Edited by permission from author by Liron Blecher.
CSC 2720 Building Web Applications Using Java Beans, Custom Tags and Tag Libraries in JSP pages.
1 JSP – Java Server Pages Representation and Management of Data on the Internet.
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.
JSP Java Server Pages Softsmith Infotech.
Li Tak Sing COMPS311F. Static attributes in Servlets Since Servlets are also Java classes, you can also use static attributes to store values that can.
® IBM Software Group © 2007 IBM Corporation JSP Custom Tags
Chapter 7 Java Server Pages. Objectives Explain how the separation of concerns principle applies to JSP Describe the operation and life-cycle of a JSP.
Introduction to JavaServer Pages (JSP) Slides from Dr. Mark Llewellyn.
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.
Chapter 8 Script-free pages. Problem with scripting in JSP When you use scripting (declaration, scriplet, expressions) in your JSP, you actually put Java.
® IBM Software Group © 2007 IBM Corporation JSP Expression Language
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.
JSTL Lec Umair©2006, All rights reserved JSTL (ni) Acronym of  JavaServer Pages Standard Tag Library JSTL (like JSP) is a specification, not an.
Fall 2007cs4201 Advanced Java Programming Umar Kalim Dept. of Communication Systems Engineering
CS320 Web and Internet Programming Java Beans and Expression Language (EL) Chengyu Sun California State University, Los Angeles.
1 (Server-Side Programming using Java Server Pages) cs
JSP Tag Libraries Lec Last Lecture Example We incorporated JavaBeans in “Course Outline” Example But still have to write java code inside java.jsp.
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.
COMP 321 Week 10. Overview Using Beans in JSP Expression Language JSTL Lab 10-1 Introduction Exam Review.
Copyright © 2002 ProsoftTraining. All rights reserved. JavaServer Pages.
CS320 Web and Internet Programming Custom Tag Library Chengyu Sun California State University, Los Angeles.
1 (Server-Side Programming using Java Server Pages) cs
JavaServer Page by Antonio Ko. Overview ► Introduction ► What is a servlet? ► What can servlets do? ► Servlets Vs JSP ► Syntax ► Samples ► JavaBean ►
JSP Custom Tags. Prerequisites Servlet API Mapping to JSP implicit objects JavaServer Pages Basic syntax Implementation via servlet API XML.
CSC 2720 Building Web Applications JavaServer Pages (JSP) JSP Directives and Action Elements.
JSP Pages. What and Why of JSP? JSP = Java code imbedded in HTML or XML –Static portion of the page is HTML –Dynamic portion is Java Easy way to develop.
Web Technologies Java Beans & JSP By Praveen Kumar G.
JSP BASICS AND ARCHITECTURE. Goals of JSP Simplify Creation of dynamic pages. Separate Dynamic and Static content.
CS562 Advanced Java and Internet Application Introduction to the Computer Warehouse Web Application. Java Server Pages (JSP) Technology. By Team Alpha.
JAVA BEANS JSP - Standard Tag Library (JSTL) JAVA Enterprise Edition.
© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/3 JSP Application Models.
Basic JSP Celsina Bignoli Problems with Servlets Servlets contain –request processing, –business logic –response generation all lumped.
COMP9321 Web Application Engineering Semester 2, 2015 Dr. Amin Beheshti Service Oriented Computing Group, CSE, UNSW Australia Week 3 1COMP9321, 15s2, Week.
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 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.
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.
JSP (Java Server Page) JSP is server side technology which is used to create dynamic web pages just like Servlet technology. This is mainly used for implementing.
Introduction to JSP Part 2
Scripted Page Web App Development (Java Server Pages)
JSP – Java Server Pages: The Gory Details
Java Server Pages B.Ramamurthy.
CS3220 Web and Internet Programming Expression Language (EL)
CS3220 Web and Internet Programming Expression Language (EL)
Introduction to JSP Dept. of B.Voc Software Development and System Administration St. Joseph’s College(Autonomous) Trichy-02 By Dr. J. Ronald Martin Introduction.
Scripted Page Web Application Development (Java Server Pages)
Presentation transcript:

JSP – Java Server Pages Part 2 Representation and Management of Data on the Internet

Interacting with other Resources

JSP Cooperation We will consider several ways in which JSP and other resources cooperate -Forwarding the request handling to other resources -Including the content of other sources -Including the code of other JSP files -Forwarding exception handling to other JSPs

Actions JSP actions use constructs in XML syntax to control the behavior of the Servlet engine Using actions, you can -forward the request to another resource in the application -dynamically include a resource content in the response

The forward Action jsp:forward - Forwards the requester to a new resource }"> <jsp:param name="parameterName" value="{parameterValue | }" /> * This action is translated to an invocation of the RequestDispatcher

The include Action jsp:include - Include a resource content at run time }"> <jsp:param name="parameterName" value="{parameterValue | }" /> * This action is also translated to an invocation of the RequestDispatcher

The include Directive This directive lets you include files at the time the JSP page is translated into a Servlet The directive looks like this: JSP content can affect main page In Tomcat 5.x, generated Servlet is updated when included files change (unlike old versions...)

Include - Action File1.jsp Servlet1 File2.jsp Servlet2 HTML content HTML content HTML content

Include Directive File1.jsp File2.jsp Servlet HTML content

include Action vs. Directive When a resource is included using the include action, the generated Servlet uses the dispatcher to include its content at runtime When a file is included using the include directive, the file itself is included verbatim into the JSP code, prior to the Servlet generation Question: in which of the above options can the included element change the HTTP headers or status?

Including JSP Here is an interesting page. Bla, Bla, Bla, Bla. BlaBla.jsp Accesses to page since Servlet init: Page Created for Dbi Course at. us here. AccessCount.jsp dbimail.jsp

out.write(" \r\n"); out.write(" Including JSP \r\n"); out.write(" \r\n"); out.write(" Here is an interesting page. \r\n"); out.write(" Bla, Bla, Bla, Bla. \r\n"); out.write(" \r\n"); out.write(" Accesses to page since Servlet init: \r\n"); out.print( ++accessCount ); out.write(" \r\n"); org.apache.jasper.runtime.JspRuntimeLibrary. include(request, response, "/dbimail.jsp", out, false); out.write(" \r\n"); BlaBla_jsp.java

Included Counter Suppose that the file BlaBla2.jsp is similar the BlaBla.jsp How will the counter of BlaBla2.jsp act? What if we used a JSP action instead of a JSP directive for the counter?

Error Pages We can set one JSP page to be the handler of uncaught exceptions of another JSP page, using JSP directives -Defines a JSP page that handles uncaught exceptions -The page in url must have true in the page-directive: -The variable exception holds the exception thrown by the calling JSP

Reading From Database <% Class.forName("oracle.jdbc.driver.OracleDriver"); Connection con = DriverManager.getConnection ("jdbc:oracle:thin:" + %> Connection Established!! connect.jsp

Connection Error Oops. There was an error when you accessed the database. Here is the stack trace: errorPage.jsp

Custom JSP Tags

JSP code may use custom tags - tags that are defined and implemented by the programmer The programmer defines how each of the custom tags is translated into Java code There are two methods to define custom tags: -Tag libraries - used in old versions of JSP -Tag files - much simpler, introduced in JSP 2.0

Tag Libraries A tag library consists of: -Tag handlers - Java classes that define how each of the new tags is translated into Java code -A TLD (Tag Library Descriptor) file, which is an XML file that defines the structure and the implementing class of each tag

package dbi; import javax.servlet.jsp.JspException; import javax.servlet.jsp.tagext.SimpleTagSupport; import java.io.IOException; public class DateTag extends SimpleTagSupport { public void doTag() throws JspException, IOException { getJspContext().getOut().print(new java.util.Date()); } DateTag.java A Simple TagLib Example

date dbi.DateTag empty dbi-taglib.tld Hello. The time is: taglibuse.jsp

Tag Files JSP 2.0 provides an extremely simplified way of defining tags The idea: for each custom tag, write a tag file tagName.tag that implements the tag translation using JSP code This way, the programmer can avoid creating tag handlers and TLD files

The Simplified Example Hello. The time is: date.tag taguse.jsp

Other Capabilities of Custom Tags Attributes -You can define the possible attributes of the Tags -These can be accessed during the Tag translation Tag Body -Tag translation may choose to ignore, include or change the tag body

Java Beans in JSP

Motivation Software components (e.g. objects, data structures, primitives) are extensively used in Web applications For example: -Service local variables -Attributes forwarded in requests -Session attributes, like users information -Application attributes, like access counters

Motivation Standard actions are used to manipulate components: declaration, reading from the suitable context, setting of new values (according to input parameters), storing inside the suitable context, etc. Java Beans provide a specification for automatic handling and manipulation of software components in JSP (and other technologies...)

Java Beans: The Idea Java Beans are simply objects of classes that follow some (natural) coding convention: -An empty constructor -A readable property has a matching getter -A writable property has a matching setter Use JSP actions to access and manipulate the bean, and special action attributes to specify the properties of the bean, like its scope

Example 1: Access Counter In the following example, we use a Bean to maintain an access counter for requests to the pages

package dbi; public class CounterBean { private int counter; public CounterBean() { counter = 0; } public int getCounter() { return counter; } public void setCounter(int i) { counter = i; } public void increment() { ++counter; } } Bean must reside in a package Bean is created by an empty constructor counter is readable and writable other methods can be used Counter Bean CounterBean.java

Bean Example <jsp:useBean id="accessCounter" class="dbi.CounterBean" scope="application"/> Welcome to Page A Accesses to this application: Page B invokes getCounter() pageA.jsp

Bean Example <jsp:useBean id="accessCounter" class="dbi.CounterBean" scope="application"/> Welcome to Page B Accesses to this application: Page A pageB.jsp

dbi.CounterBean accessCounter = null; synchronized (application) { accessCounter = (dbi.CounterBean) _jspx_page_context.getAttribute("accessCounter", PageContext.APPLICATION_SCOPE); if (accessCounter == null) { accessCounter = new dbi.CounterBean(); _jspx_page_context.setAttribute("accessCounter", accessCounter, PageContext.APPLICATION_SCOPE); } From the Generated Servlet

Example 2: Session Data In the following example, we use a Bean in order to keep a user's details throughout the session

package dbi; public class UserInfoBean { private String firstName; private String lastName; public UserInfoBean() { firstName = lastName = null;} public String getFirstName() {return firstName;} public String getLastName() {return lastName;} public void setFirstName(String string) {firstName = string;} public void setLastName(String string) {lastName = string;} } UserInfoBean.java

Information Form Fill in your details: Your First Name: Your Last Name: infoForm.html

Page A Hello, Have a nice session! User Info B Match parameters to corresponding properties infoA.jsp

Page B Hello, Have a nice session! User Info A infoB.jsp

Advantages of Java Beans Easy and standard management of data -Automatic management of bean sharing and lots more Good programming style -Allow standard but not direct access to members -You can add code to the setters and getters (e.g. constraint checks) without changing the client code -You can change the internal representation of the data without changing the client code Increase of separation between business logic (written by programmers) and HTML (written by GUI artists)

JSP Expression Language

JSP expression language is a comfortable tool to access useful objects in JSP This language provides shortcuts in JavaScript- like syntax An expression in EL is written as ${expr} For example: Hi, ${user}. Welcome

EL Variables JSP EL does not recognize JSP's implicit objects, but rather has its own set: param, paramValues, header,headerValues, cookie, initParam, pageScope, requestScope, sessionScope, applicationScope Each of these objects maps names to values For example, use param["x"] or param.x to get the value of the parameter x

EL Variables (cont) A variable that is not an EL implicit object is looked up at the page, request, session (if valid) and application scopes That is, x is evaluated as the first non null element obtained by executing pageContext.getAttribute("x"), request.getAttribute("x"), etc.

Object Properties In JSP EL, Property p of Object o is referred to as o[p] Property p of Object o is evaluated as follows: -If o is a Map object, then o.get(p) is returned -If o is a List or an array, then p is converted into an integer and o.get(p) or o[p] is returned -Otherwise, treat o as a bean, convert p to a string, and return apply the corresponding getter of o The term o.p is equivalent to o["p"]

An Example <% response.addCookie(new Cookie("course","dbi")); session.setAttribute("dbiurl",new java.net.URL(" String[] strs = {"str1","str2"}; session.setAttribute("arr", strs); %> JSP Expressions Write the parameter x: elcall.jsp

EL Examples Expression-Language Examples Parameter x : ${param["x"]} Cookie course : ${cookie.course.value} Header Connection : ${header.Connection} Path of session attr. dbiurl : ${sessionScope.dbiurl.path} Element arr[${param.x}] : ${arr[param.x]} el.jsp