Introduction to JavaServer Pages Vijayan Sugumaran Dept. of DIS Oakland University Parts of this presentation was provided by www.coreservlets.com.

Slides:



Advertisements
Similar presentations
Java Script Session1 INTRODUCTION.
Advertisements

Java Server Pages (JSP)
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.
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.
J2EE Servlets and JSP Advanced topics Presented by Bartosz Sakowicz.
18-Jun-15 JSP Java Server Pages Reference: Tutorial/Servlet-Tutorial-JSP.html.
JSP Java Server Pages Reference:
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,
Integrating Servlets and JavaServer Pages Vijayan Sugumaran School of Business Administration Oakland University Parts of this presentation provided by.
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.
Servlets Compiled by Dr. Billy B. L. Lim. Servlets Servlets are Java programs which are invoked to service client requests on a Web server. Servlets extend.
Java Servlets. What Are Servlets? Basically, a java program that runs on the server Basically, a java program that runs on the server Creates dynamic.
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.
AddressBook using Servlets and Intro to JSP Lec - 33.
1 CIS336 Website design, implementation and management (also Semester 2 of CIS219, CIS221 and IT226) Lecture 9 JavaServer Pages (JSP) (Based on Møller.
Netbeans – jsp.zip Introduction to JSP Netbeans – jsp.zip.
M. Taimoor Khan * Java Server Pages (JSP) is a server-side programming technology that enables the creation of dynamic,
J2EE training: 1 Course Material Usage Rules PowerPoint slides for use only in full-semester, for-credit courses at degree-granting.
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.
Introduction to JSP Based on: Marty Hall, Larry Brown, Core Servlets and JavaServer Pages.
JSP Java Server Pages Softsmith Infotech.
J2EE training: 1 Course Material Usage Rules PowerPoint slides for use only in full-semester, for-credit courses at degree-granting.
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.
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.
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:
Chapter 3 Servlet Basics. 1.Recall the Servlet Role 2.Basic Servlet Structure 3.A simple servlet that generates plain text 4.A servlet that generates.
Introduction to JavaServer Pages. 2 JSP and Servlet Limitations of servlet  It’s inaccessible to non-programmers JSP is a complement to servlet  focuses.
Variables and ConstantstMyn1 Variables and Constants PHP stands for: ”PHP: Hypertext Preprocessor”, and it is a server-side programming language. Special.
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.
CSC 2720 Building Web Applications JavaServer Pages (JSP) The Basics.
1 JSP – Java Server Page DBI – Representation and Management of Data on the Internet.
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 (JSP)
 Obtaining a RequestDispatcher Forwarding requests from servlets to dynamic resources  Forwarding requests from servlets to static resources  Using.
1 Processing phase Translation phase JSP page translation and processing phases Client Server Request Response Hello.jsp helloServlet.class helloServlet.java.
Chapter 11 Invoking Java Code with JSP Scripting Elements.
Java Servlets and Java Server Pages Norman White Stern School of Business.
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.
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.
JSP – Java Server Page DBI – Representation and Management of Data on the Internet.
Java Servlets and 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.
SKT-SSU IT Training Center Servlet and JSP. Chapter Eleven: Invoking Java Code with JSP Scripting Elements.
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 -by Rubeena Memon Deepti Jain Jaya Thakar Jisha Vettuventra.
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 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.
1 CSC160 Chapter 1: Introduction to JavaScript Chapter 2: Placing JavaScript in an HTML File.
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.
Scripted Page Web App Development (Java Server Pages)
Invoking Java Code from JSP
Chapter 27 WWW and HTTP.
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:

Introduction to JavaServer Pages Vijayan Sugumaran Dept. of DIS Oakland University Parts of this presentation was provided by

The Need for JSP  With servlets, it is easy to  Read form data  Read HTTP request headers  Set HTTP status codes and response headers  Use cookies and session tracking  Share data among servlets  Remember data between requests  Get fun, high-paying jobs  But, it sure is a pain to  Use those println statements to generate HTML  Maintain that HTML

The JSP Framework  Idea:  Use regular HTML for most of page  Mark servlet code with special tags  Entire JSP page gets translated into a servlet (once), and servlet is what actually gets invoked (for each request)  Example:  JSP  Thanks for ordering  URL  amming  Result  Thanks for ordering Core Web Programming

Benefits of JSP  Although JSP technically can't do anything servlets can't do, JSP makes it easier to:  Write HTML  Read and maintain the HTML  JSP makes it possible to:  Use standard HTML tools such as HomeSite or DreamWeaver  Have different members of your team do the HTML layout than do the Java programming  JSP encourages you to  Separate the (Java) code that creates the content from the (HTML) code that presents it

Advantages of JSP Over Competing Technologies  Versus ASP or ColdFusion  Better language for dynamic part  Portable to multiple servers and operating systems  Versus PHP  Better language for dynamic part  Better tool support  Versus pure servlets  More convenient to create HTML  Can use standard tools (e.g., HomeSite)  Divide and conquer  JSP programmers still need to know servlet programming

Advantages of JSP (Contd.)  Versus client-side JavaScript (in browser)  Capabilities mostly do not overlap with JSP, but  You control server, not client  Richer language  Versus server-side JavaScript (eg, LiveWire, BroadVision, JRun)  Richer language  Versus static HTML  Dynamic features  Adding dynamic features no longer "all or nothing" decision

Setting Up Your Environment  Don’t have to set any CLASSPATH  Don’t have to compile your jsp page  Don’t have to use packages to avoid name conflicts  Don’t have to put JSP page in a special directory  Placed along with html documents  Some servers reserve certain parts of Web hierarchy for JSP pages. Tomcat 3 and JRun (standalone) don't.  Don’t have to use special URL to invoke JSP page  Caveats  Previous rules about CLASSPATH, install dirs, etc., still apply to regular Java classes used by a JSP page

Example <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> JSP Expressions <META NAME="keywords" CONTENT="JSP,expressions,JavaServer,Pages,servlets"> <META NAME="description" CONTENT="A quick example of JSP expressions."> <LINK REL=STYLESHEET HREF="JSP-Styles.css" TYPE="text/css">

Example (Continued) JSP Expressions Current time: Your hostname: Your session ID: The testParam form parameter:

Example Result  If location was  C:\Program Files\Apache Software Foundation\Tomcat 5.0\webapps\vijay\jsp_pages  URL would be

Most Common Misunderstanding  Forgetting JSP is Server-Side Technology  Very common question  I can’t do such and such with HTML. Will JSP let me do it?  Why doesn’t this question make sense?  JSP runs entirely on server  It doesn’t change content the client (browser) can handle  Similar questions  How do I put a normal applet in a JSP page? Answer: send an tag to the client  How do I put an image in a JSP page? Answer: send an tag to the client  How do I use JavaScript/Acrobat/Shockwave/Etc? Answer: send the appropriate HTML tags

Another Common Misunderstanding  Translation/Request Time Confusion  What happens at page translation time?  JSP constructs get translated into servlet code.  What happens at request time?  Servlet code gets executed. No interpretation of JSP occurs at request time. The original JSP page is totally ignored at request time; only the servlet that resulted from it is used.  When does page translation occur?  Typically, the first time JSP page is accessed after it is modified. This should never happen to real user (developers should test all JSP pages they install).  Page translation does not occur for each request.

JSP/Servlets in the Real World  ofoto.com: print and manage digital and conventional photos.

JSP/Servlets in the Real World  First USA Bank: largest credit card issuer in the world; most on-line banking customers

JSP/Servlets in the Real World  Delta Airlines: entire Web site, including real-time schedule info

JSP/Servlets in the Real World  American Century Investments: more than 70 mutual funds, $90 billion under management, two million investors

JSP/Servlets in the Real World  Excite: one of the top five Internet portals; one of the ten busiest sites on the Web

Quick Summary So far…  JSP makes it easier to create and maintain HTML, while still providing full access to servlet code  JSP pages get translated into servlets  It is the servlets that run at request time  Client does not see anything JSP-related  You still need to understand servlets  Understanding how JSP really works  Servlet code called from JSP  Knowing when servlets are better than JSP  Mixing servlets and JSP  Other technologies use similar approach, but aren't as portable and don't let you use Java for the "real code"

JSP Development Model  JSP developer writes a.jsp source file and stores it in the web application directory .jsp file is no different from an ordinary html file  When the.jsp URL is invoked for the first time, the JSP container reads the.jsp file, parses its contents, and generates the source code for an equivalent java servlet  It then compiles and creates a.class file  The JSP container loads the servlet class and uses it to service the HTTP request.  Translation time – Generating the servlet source code from a.jsp file  Request time – Invoking the servlet to handle the HTTP request

Components of a JSP Page  A.jsp file contains  JSP Elements  Instructions to the JSP container about what code to generate and how it should operate  These elements have specific start and end tags that identify them to the JSP compiler  Fixed Template data  Everything else that is not recognized by the JSP container  Usually HTML data, passed through unmodified  Results in HTML code that is sent to the client  Any combination of the two

JSP Elements  Three types of JSP Elements  Directives  Instructions to the JSP container that describes what code should be generated   Three standard directives  page directive  include directive  taglib directive  Scripting Elements  Lets you specify Java code that will become part of the resultant servlet  Actions  Specify existing components that should be used and otherwise control the behavior of JSP engine

Uses of JSP Constructs  Scripting elements calling servlet code directly  Scripting elements calling servlet code indirectly (by means of utility classes)  Beans  Custom tags  Servlet/JSP combo (MVC), with beans and possibly custom tags Simple Application Complex Application

Basic Syntax  HTML Text  Blah  Passed through to client. Really turned into servlet code that looks like  out.print(" Blah ");  HTML Comments   Same as other HTML: passed through to client  JSP Comments   Not sent to client  To get <% in output, use <\%

Types of Scripting Elements  Expressions  Format:  Evaluated and inserted into the servlet’s output. I.e., results in something like out.print(expression)  Scriptlets  Format:  Inserted verbatim into the servlet’s _jspService method (called by service)  Declarations  Format:  Inserted verbatim into the body of the servlet class, outside of any existing methods

JSP Expressions  Format   Result  Expression evaluated, converted to String, and placed into HTML page at the place it occurred in JSP page  That is, expression placed in _jspService inside out.print  Examples  Current time:  Your hostname:  XML-compatible syntax  Java Expression  XML version not supported by Tomcat 3. Until JSP 1.2, servers are not required to support it. Even then, you cannot mix versions within a single page.

JSP/Servlet Correspondence  Original JSP A Random Number  Possible resulting servlet code public void _jspService(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); HttpSession session = request.getSession(true); JspWriter out = response.getWriter(); out.println(" A Random Number "); out.println(Math.random());... }

Example Using JSP Expressions JSP Expressions Current time: Your hostname: The URI Requested: The Protocol Used: Your session ID: The testParam form parameter:

Predefined Variables  request  The HttpServletRequest (1st argument to service/doGet)  response  The HttpServletResponse (2nd arg to service/doGet)  out  The Writer (a buffered version of type JspWriter) used to send output to the client  session  The HttpSession associated with the request (unless disabled with the session attribute of the page directive)  application  The ServletContext (for sharing data) as obtained via getServletConfig().getContext().

JSP Scriptlets  Format   Result  Code is inserted verbatim into servlet's _jspService method  Example   XML-compatible syntax  Java Code

JSP/Servlet Correspondence  Original JSP  Possible resulting servlet code public void _jspService(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); HttpSession session = request.getSession(true); JspWriter out = response.getWriter(); out.println(foo()); bar();... }

Example Using JSP Scriptlets Color Testing <% String bgColor = request.getParameter("bgColor"); boolean hasExplicitColor; if (bgColor != null) { hasExplicitColor = true; } else { hasExplicitColor = false; bgColor = "WHITE"; } %>

Example Using JSP Scriptlets (Contd.) "> Color Testing <% if (hasExplicitColor) { … } else { … } %>

JSP Scriptlets: Results

JSP Declarations  Format   Result  Code is inserted verbatim into servlet's class definition, outside of any existing methods  Examples   XML-compatible syntax  Java Code

JSP/Servlet Correspondence  Original JSP Some Heading <%! private String randomHeading() { return(" " + Math.random() + " "); } %>  (Alternative: make randomHeading a static method in a separate Java class)

JSP/Servlet Correspondence  Possible resulting servlet code public class xxxx implements HttpJspPage { private String randomHeading() { return(" " + Math.random() + " "); } public void _jspService(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); HttpSession session = request.getSession(true); JspWriter out = response.getWriter(); out.println(" Some Heading "); out.println(randomHeading());... }... }

Example Using JSP Declarations JSP Declarations <LINK REL=STYLESHEET HREF="JSP-Styles.css" TYPE="text/css"> JSP Declarations Accesses to page since server reboot:

JSP Declarations: Result  After 15 total visits by an arbitrary number of different clients

JSP Declarations: the jspInit and jspDestroy Methods  JSP pages, like regular servlets, sometimes want to use init and destroy  Problem: the servlet that gets built from the JSP page might already use init and destroy  Overriding them would cause problems.  Thus, it is illegal to use JSP declarations to declare init or destroy.  Solution: use jspInit and jspDestroy.  The auto-generated servlet is guaranteed to call these methods from init and destroy, but the standard versions of jspInit and jspDestroy are empty (placeholders for you to override).

JSP Declarations & Predefined Variables  Problem  The predefined variables (request, response, out, session, etc.) are local to the _jspService method. Thus, they are not available to methods defined by JSP declarations or to methods in helper classes. What can you do about this?  Solution: pass them as arguments. E.g.  Note that the println method of JspWriter throws IOException  Use “throws IOException” for methods that use println

Using JSP Expressions as Attribute Values  Static Value   Dynamic Value  ’ />

Attributes That Permit JSP Expressions  The name and value properties of jsp:setProperty  See upcoming section on beans  The page attribute of jsp:include  See upcoming section on including files and applets  The page attribute of jsp:forward  See upcoming section on integrating servlets and JSP  The value attribute of jsp:param  See upcoming section on including files and applets

Summary  JSP Expressions  Format:  Wrapped in out.print and inserted into _jspService  JSP Scriptlets  Format:  Inserted verbatim into the servlet’s _jspService method  JSP Declarations  Format:  Inserted verbatim into the body of the servlet class  Predefined variables  request, response, out, session, application  Limit the Java code that is directly in page  Use helper classes, beans, custom tags, servlet/JSP combo