Java Server Pages (JSP)

Slides:



Advertisements
Similar presentations
Servlets & JSPs - Sharad Ballepu.
Advertisements

8 Copyright © 2005, Oracle. All rights reserved. Creating the Web Tier: JavaServer Pages.
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.
 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.
JSP – Java Server Pages Part 1 Representation and Management of Data on the Internet.
WEB1P servintro1 Introduction to servlets and JSP Dr Jim Briggs.
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:
DT228/3 Web Development JSP: Directives and Scripting elements.
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.
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.
Introduction to Java web programming Dr Jim Briggs JWP intro1.
Java Enterprise Edition Java Web Development Structure of a web project Introduction to Web Applications The first project Introduction to Java Web Development.
1 CIS336 Website design, implementation and management (also Semester 2 of CIS219, CIS221 and IT226) Lecture 9 JavaServer Pages (JSP) (Based on Møller.
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.
CSC 2720 Building Web Applications Using Java Beans, Custom Tags and Tag Libraries in JSP pages.
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.
Introduction to JSP Based on: Marty Hall, Larry Brown, Core Servlets and JavaServer Pages.
JSP Java Server Pages Softsmith Infotech.
Introduction to Java Server Pages (JSPs) Robert Thornton.
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)
Jordan Anastasiade. All rights reserved.
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.
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.
Slides © Marty Hall, book © Sun Microsystems Press 1 JSP Scripting Elements Core Servlets & JSP book:
16-Oct-15 JSP Implicit Objects. 2 JSP Implicit Objects are the Java objects that the JSP Container makes available to developers in each page and developer.
SE-2840 Dr. Mark L. Hornick 1 Java Server Pages. HTML/JSPs are intended to be used as the views in an MVC- based web application Model – represents an.
JSP Fundamentals Elements of a JSP Using Beans with JSP Integrating Servlets and JSP.
CSC 2720 Building Web Applications JavaServer Pages (JSP) The Basics.
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.
Copyright © 2002 ProsoftTraining. All rights reserved. JavaServer Pages.
Java Server Pages An introduction to JSP. Containers and Components Several clients – one system.
CS-4220 Dr. Mark L. Hornick 1 Java Server Pages. HTML/JSPs are intended to be used as the views in an MVC- based web application Model – represents an.
Java Server Pages Introduction. Servlet Drawbacks Web page designer will need to know servlets to design the page. Servlet will have to be compiled for.
Chapter 11 Invoking Java Code with JSP Scripting Elements.
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.
JSP. Types of JSP Scripting Elements Expressions of the form, which are evaluated and inserted into the servlet's output. Scriptlets of the form, which.
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.
Advanced Java Session 6 New York University School of Continuing and Professional Studies.
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.
STRUCTURE OF JSP PRESENTED BY: SIDDHARTHA SINGH ( ) SOMYA SHRIVASTAV ( ) SONAM JINDAL ( )
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 43 JavaServer Page.
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.
INVOKING JAVA CODE WITH JSP SCRIPTING ELEMENTS. Creating Template Text A large percentage of your JSP document consists of static text (usually HTML),
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,
JSP JavaServer Pages. What is JSP? Java based technology that simplifies the development of dynamic web sites JSP pages are HTML pages with embedded code.
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.
Developing JavaServer Pages
Java Server Pages.
Java Server Pages By: Tejashri Udavant..
COMP9321 Web Application Engineering Semester 2, 2017
Scripted Page Web App Development (Java Server Pages)
Pre-assessment Questions
Introduction to JSP Java Server Pages
Pre-assessment Questions
JSP implicit objects & directive elements
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.
Web Technologies Java Beans & JSP
Presentation transcript:

Java Server Pages (JSP) - Sharad Ballepu sharadballepu@sharmanj.com www.sharmanj.com

Servlets & JSPs Introduction JSP Elements Implicit Objects Java Server Pages (JSP) Servlets & JSPs Agenda Introduction JSP Elements Implicit Objects Tag Libraries JSP Actions Error Handling Demo

Introduction – What is JSP JSP Stands for Java Server Pages Presents dynamic content to users Handles the presentation logic in an MVC architecture (business logic) servlet request Container Business Tier Helper Objects JSP (presentation logic) response

Runs within a Web Container Introduction – JSP v/s Servlet How is JSP different / similar to Servlet? Servlets JSP Handles dynamic data Handles business logic Handles presentation logic Lifecylce methods init() : can be overridden service() : can be overridden destroy() : can be overridden jspInit() : can be overridden _jspService() : cannot be overridden jspDestroy() : can be overridden Html within java out.println(“<htrml><body>”); out.println(“Time is” + new Date()); out.println(“</body></html>”); Java within html <html><body> Time is <%=new Date()%> </body></html> Runs within a Web Container

Introduction – JSP is a Servlet In the end, a JSP is just a Servlet Is loaded and Initialized as Is translated to Compiles to writes JSP Import javax. servlet. HttpServlet.* 0010 0001 1100 1001 0001 0011 Servlet MyJsp_jsp Servlet MyJsp.jsp MyJsp_jsp.java MyJsp_jsp.class

Introduction – Comments JSP Comments <%-- {CODE HERE} --%> Does not show the comments on the page Does not show the comments in page source Can only be used outside JSP Elements HTML Comments <!– {CODE HERE} --> Shows the comments in page source Single line Comments // {CODE HERE} When put outside JSP Elements, shows comments on the page & source When put inside Scriptlets/Declarations, does not show on page & source Can be used inside scriptlets/declarations and outside JSP Elements

JSP Elements – Intro Need to write some Java in your HTML? Want to make your HTML more dynamic? JSP Declarations :: Code that goes outside the service method JSP Scriptlets :: Code that goes within the service method JSP Expressions :: Code inside expressions is evaluated JSP Directives :: Commands given to the JSP engine

JSP Elements - Declarations What are declarations? Whatever goes inside the “<%!{JAVA_HERE}%>” tags is called a declaration Everything you write in a declarations goes outside the service method Treat them as instance methods and instance variables What do you declare in declarations? You can declare methods and variables in declarations Why do you need declarations? You have any variable that needs to be shared across different requests. You have repeated common code in you jsp, declare it in a method. Example <%! int instanceVar = 10; %>

JSP Elements - Declarations Request 1 Service1 Service2 Service3 Request 2 Declarations Request 3

JSP Elements - Scriptlets What are declarations? Whatever goes inside the “<%{JAVA_HERE}%>” tags is called a scriptlet Everything you write in a scriptlets goes in the service method Treat variables in scriptlets as method/local variables What do you put in scriptlets? Business logic in JSPs are put in scriptlets. Set of java statements Why do you need scriptlets? Need to perform some small business logic (if logic is complex, do in java) Need to perform some basic validations Example <% int localVar = 10; %>

JSP Elements - Expressions What are expressions? Whatever goes inside the “<%={JAVA_HERE}%>” tags is called an expression Code inside expressions is evaluated, and output is displayed Whatever is put inside expressions should evaluate to a value What do you put in scriptlets? Variables or methods that return some values Why do you need scriptlets? Need to print some text onto the page Example <%= localVar %>

Declarations Scriptlets Expressions JSP Elements - Example <html><head><title>JSP Elements Example</title> </head> <body> <%! int userCnt = 0; %> <% String name = "Sharad"; userCnt++; %> <table> <tr><td> Welcome <%=name%>. You are user number <%=userCnt%> </td></tr> </table> </body> </html> Declarations Scriptlets Expressions

JSP Elements - Directives What are directives? Whatever goes inside the “<%@{DIRECTIVES}%>” tags is called a directive Directives gives pre-processing commands to the JSP Engine. Everything in directives are processed before JSP is translated to Servlet What do you put in directives? Processing commands to the JSP Engine. Why do you need directives? To incorporate certain additional features into the JSP Modifying the Servlet behaviour generated from the JSP Include other html/jsp files in the JSP. Provide tag libraries Example <%@ page import=“java.util.ArrayList” %>

Directives – page directive Any number of the pre-defined attributes can be added to the page directive <%@ page import = “{IMPORTED_CLASSES}” contentType = “{CONTENT_TYPE}” isThreadSafe = “{true/false} session = “{true/false}” buffer = “{BUFFER_SIZE}” autoflush = “{true/false}” extends = “{EXTENDS_FROM_PAGE}” info = “{PAGE_INFO”} errorPage = “{ERROR_PAGE_NAME}” isErrorPage = “{true/false}” language = “{LANGUAGE}”

Hello <%=userName%> Directives – include directive Including other page content into your JSP <%@ include file = {PAGE_URL_TO_INCLUDE} %> Includes files at translation time If included file is modified, the main jsp needs to be recompiled Main page Hello <%=userName%> <%@ include file =“header.jsp"/> header.jsp <table><tr> <td>Contact Details</td> <tr></table> <%@ include file =“footer.jsp"/> main.jsp footer.jsp

Directives – taglib directive Providing output based on common custom logic <%@ taglib uri=“{TLD_FILE}" prefix=“{PREFIX}" %> Components involved in tag libraries Tag handler class Common custom logic and HTML generation Descriptor configuration file Directive’s properties mapping information taglib directive Used in JSPs to use the tag handler functionality

taglib directive – high-level overview <name>txtBox</name> <tagclass>TxtBoxTag</tagclass> <bodycontent>EMPTY</bodycontent> <attribute> <name>name</name> <required>true</required> …. </attribute> … </tag> <%@ taglib uri=“sharmanjTags” prefix=“sharmanj” %> ….. < sharmanj:txtBox length=“10“ name=“userName”/> test.jsp sharmanj-taglib.tld <web-app> <taglib> <taglib-uri>sharmanjTags</taglib-uri> <taglib-location> sharmanj-taglib.tld </taglib-location> </taglib> </web-app> public class TxtBoxTag extends TagSupport { public int doStartTag() { …. } TxtBoxTag.java web.xml

Implicit Objects - Introduction Set of predefined objects readily available for use. out: JspWriter request: HttpRequest response: HttpResponse session: HttpSession config: ServletConfig application: ServletContex page: JSP page pageContext: Special object

Implicit Objects - Example <html> <head> <title>JSP Implicit objects Example</title> </head> <body> <% if (“YES”.equals(request.getAttribute(“SCOPE”))) { out.println(“REQUEST”); } else { if (“YES”.equals(session.getAttribute(“SCOPE”))) { out.println(“SESSION”); out.println(“NONE”); } %> </body> </html>

Implicit Objects – out An instance of JspWriter Writes statements to the page The java way to display text on the webpage Displaying dynamic data

Implicit Objects – request Instance of HttpServletRequest Use the attributes stored in request object for display /basic validations in JSP Forwards request to JSP JSP JSP Servlet Servlet request.setAttribute(“a”, “10”) a = 10 Helper Object (a = 10) Uses helper object

Implicit Objects – response Instance of HttpServletResponse Send the response back to the client (HTML in most of the cases) JSP1 Send response to client response.sendRedirect(“url”); JSP1 JSP2 Redirect to JSP2 Send response to client

Implicit Objects – session Instance of HttpSession Retrieve and set the session attributes in JSP Remember the client across multiple requests Request 1 Web App Response1 CLIENT Request 1 Response2 SESSION

Implicit Objects – config and application config - Instance of ServletConfig Application – instance of ServletContext Servlet Context JSP 1 JSP 1 JSP 1 JSP 1 Servlet Config Servlet Config Servlet Config Servlet Config

Hello <%=userName%> Implicit Objects – page Main page Hello <%=userName%> <jsp:include page=“/header.jsp"/> header.jsp <table><tr> <td>Contact Details</td> <tr></table> <jsp:include page=“/footer.jsp"/> footer.jsp main.jsp

Implicit Objects – pageContext application session pageContext Least visible to most visible request page

JSP Actions Special tags which provides special features The container handles these tags in a special way Addresses certain common functionalities Achieve functionality with less code, and more standard way <jsp:include> <jsp:forward> <jsp:param> <jsp:useBean> <jsp:getProperty> <jsp:setProperty>

JSP Actions - <jsp:include> Definition: Includes a page at the given location in the main page Syntax: <jsp:include page="{PAGE_TO_INCLUDE}" flush="true" />

JSP Actions – Include Action v/s Include Directive Translation time Run time Copies the included file References to the included file For static content For dynamic content Cannot pass parameters Can pass parameters

JSP Actions - <jsp:forward> Definition: Forwards the request to the given page Syntax: <jsp:forward page=“{PAGE_TO_FORWARD}" />

JSP Actions - <jsp:param> Definition: Pass parameters to the included/forwarded page Syntax: <jsp:include page="{PAGE_TO_INCLUDE}" flush="true" > <jsp:param name=“{parameterName}” value="{paramValue}" /> </jsp:include> <jsp:forward page="{PAGE_TO_FORWARD}" flush="true" > <jsp:param name=“{parameterName}” value="{paramValue}" /> </jsp:forward>

JSP Actions - <jsp:useBean> getId(); setId(..) getName() setName(…) How can I use that bean? bean JSP Servlet request.setAttribute(“userBean”, userBean”) <jsp:useBean …./>

JSP Actions - <jsp:useBean> Definition: Instantiate a bean class, use bean properties, and set property values Syntax: <jsp:useBean id=“{beanInstanceName}" scope="page|request|session|application" class=“{package.class}" type=“{package.class}" beanName=" {package.beanName}”> </ jsp:useBean> qwewfhqw UserBean ub = new UserBean();

JSP Actions - <jsp:getProperty> Definition: Gets property values of a Bean Syntax: <jsp:getProperty name=“{beanInstanceName}" property= “{propertyName}">

JSP Actions - <jsp:setProperty> Definition: Sets property values in a Bean Syntax: <jsp:setProperty name=“{beanInstanceName}" property= "*" | property="propertyName" param=“{parameterName}" value="{paramValue}" } />

JSP Actions – useBean <jsp:useBean id=“userBean" scope="session“ class=“com.sharmanj.UserBean" > <jsp:setProperty name=“userBean” property=“userName” value=“Sharad”/> </ jsp:useBean> <jsp:getProperty name=“userBean" property=“userName" />

Errors in java code in scriptlets handle errors in try/catch block Error Handling – Types of errors Errors in java code in scriptlets handle errors in try/catch block may occur at runtime Errors in HTML adhere to the HTML rules can be caught during testing

Why is error handling important in JSPs?

Error pages come to the rescue Error Handling – Error page Error pages come to the rescue <%@page isErrorPage="true" %> <html> <head> <title>My Error Page</title> </head> <body> We are sorry, the page you are trying to access is currently not available. Please try after some time </body> </html>

Error Handling Specify error page in your main page Specify error page in web.xml <%@page errorPage=“errPage.jsp" %> <html> <head> <title>This is the main page</title> </head> <body> ………………….. …………………… // ERROR CODE GOES HERE …………………. </body> </html> <web-app> ……………. …………… <error-page> <exception-type>com.a.myExp</exception-type> <location>/error.jsp</location> </error-page> <error-code>404</error-code> <location>/errorServlet</location> ……………….. ………………. </web-app>

Thanks