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.

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)
JSP1 Java Server Pages (JSP) Introducing JavaServer Pages TM (JSP TM ) JSP scripting elements.
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.
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.
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,
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.
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 Server Pages B.Ramamurthy. Topics for Discussion 8/20/20152 Inheritance and Polymorphism Develop an example for inheritance and polymorphism JSP.
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.
© Yaron Kanza Server-Side Programming using Java Server Pages Written by Dr. Yaron Kanza, Edited by permission from author by Liron Blecher.
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.
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.
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.
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.
JSP Fundamentals Elements of a JSP Using Beans with JSP Integrating Servlets and JSP.
CSC 2720 Building Web Applications JavaServer Pages (JSP) The Basics.
Copyright © 2002 ProsoftTraining. All rights reserved. JavaServer Pages.
Java Server Pages (JSP)
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.
Fall 2007cs4201 Advanced Java Programming Umar Kalim Dept. of Communication Systems Engineering
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.
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 ( )
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.
1 Chapter 27 JavaServer Page. 2 Objectives F To know what is a JSP page is processed (§27.2). F To comprehend how a JSP page is processed (§27.3). F To.
INVOKING JAVA CODE WITH JSP SCRIPTING ELEMENTS. Creating Template Text A large percentage of your JSP document consists of static text (usually HTML),
 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.
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.
® IBM Software Group © 2007 IBM Corporation JSP Tag Files
JSP Java Server Pages. Hello, !
Developing JavaServer Pages
Java Server Pages.
COMP9321 Web Application Engineering Semester 2, 2017
Java Servlets.
Pre-assessment Questions
Chengyu Sun California State University, Los Angeles
Invoking Java Code from JSP
JSP implicit objects & directive elements
Introduction.
COSC 2956 Internet Tools Java Server Pages.
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:

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 application’s state, business logic, or data View – responsible for getting input from the user displaying output to the user Controller – accepts input from a view instructs the model to perform actions on the input decides what view to display for output does not generally generate HTML SE-2840 Dr. Mark L. Hornick2 Model (“regular” java classes) View (HTML) Controller (java servlets) Changes to model displays Input from views User input

JSP code looks a lot like HTML, with the addition of scripting elements page language="java" contentType="text/html; charset=utf-8" pageEncoding=“utf-8"%> Page title Hello, World! Today is: <% // The code within this tag is a java scriptlet - plain old java. Date d = new Date(); // d is a local variable, as is df (below) DateFormat df = new SimpleDateFormat("EEEE, d MMMMM yyyy HH:mm:ss z"); // out is a predefined PrintWriter output stream out.println(df.format(d)); %> SE-2840 Dr. Mark L. Hornick3 JSP’s look almost like HTML to non-Java webpage authors Files containing JSP code end with.jsp instead of.html

JSP’s are automatically translated into Servlets SE-2840 Dr. Mark L. Hornick 4 Eclipse compiles jsps into a well-hidden workspace folder: D:\Workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\work\Catalina\localhost\Example2015\org\apache\jsp

JSP’s are translated into Servlets having the methods: _ jspService() This is where the translated HTML and your scripting code gets put jspDestroy() You can override this in a declaration jspInit() Similar in function to the regular init() method You can override this too in a declaration SE-2840 Dr. Mark L. Hornick5

There are 6 different kinds of JSP elements 1. Scriptlets 2. Directives 3. Expressions 4. Declarations 5. Comments 6. ${ …} Expression Language code SE-2840 Dr. Mark L. Hornick6

Scriptlets contain plain old java code <% // The code within this tag is a java scriptlet - plain old java. java.util.Date d = new java.util.Date(); java.text.DateFormat df = new java.text.SimpleDateFormat("EEEE, d MMMMM yyyy HH:mm:ss z"); // out is a predefined PrintWriter output stream out.println("Today is " + df.format(d)); // out: an implicit object %> SE-2840 Dr. Mark L. Hornick7 Every JSP Servlet has a _jspService() method that is generated by the JASPER translator. _jspService() is the method called by Tomcat as a substitute for the doGet() and doPost() methods of regular servlets. Scriptlet code you write in a.jsp file becomes the body of the _jspService() method.

A JSP has access to various implicit objects 1. request – an HttpServletRequest 2. response – an HttpServletResponse 3. out – a JspWriter (same api as PrintWriter) 4. session – an HttpSession 5. config – a ServletConfig (for this JSP) 6. application – a ServletContext 7. page – an Object used for custom tags 8. pageContext – a PageContext (similar to config) 9. exception – a Throwable (available to error pages only) SE-2840 Dr. Mark L. Hornick8

Directives tell the translator what to do page language="java" contentType="text/html; charset=ISO " pageEncoding="ISO "%> Three types of directives: page, include, taglib The page directive can have 10 additional attributes: isThreadSafe[true], contentType[text/html], errorPage, isErrorPage[true if referred], session[true], autoFlush[true], buffer, info, IsELIgnored, extends SE-2840 Dr. Mark L. Hornick9

Expressions become the argument to out.print() Is the same as SE-2840 Dr. Mark L. Hornick10 Note the absence of the semicolon in the expression. The expression can evaluate to any type of object, which is then rendered to a String value in the usual Java way.

Declarations <%! // a helper method called from scriplet code private int doubleCount() { count = count*2; return count; } %> SE-2840 Dr. Mark L. Hornick11 Normally, scriptlet code (i.e. ) gets put into the body of the _jspService() method. Declarations are inserted before the body of the _jspService() method. Thus, Declarations can be used to declare Servlet class attributes and methods.

Comments HTML comments are passed into the generated HTML JSP comments are removed. SE-2840 Dr. Mark L. Hornick12

JSP Servlets can have init parameters like regular servlets... HelloWorld /hello.jsp max_value 10 HelloWorld /hello... SE-2840 Dr. Mark L. Hornick13 Note that the tag replaces the tag

Be careful of how JSP’s handle sessions The _jspService() method will automatically call getSession() and return a HttpSession to the implicit session reference A new session may be created if one doesn’t yet exist – is that always what you want? To suppress this, use: Or, use a Session Listener to create the session data. SE-2840 Dr. Mark L. Hornick14

Dispatching a Request from a Servlet to a JSP To make a regular Servlet get a JSP to handle outputting a response, insert the following in the Servlet’s doGet() or doPost() methods: RequestDispatcher view = request.getRequestDispatcher(“/view.jsp"); view.forward( request, response ); SE-2840 Dr. Mark L. Hornick15

You can add attributes to an HttpServletRequest, effectively enabling you to “pass” arguments to the JSP RequestDispatcher view = request.getRequestDispatcher(“view.jsp"); // add an attribute to be attached to the request request.setAttribute(“foo”, “bar”); // Now, view.jsp will be able to get “bar” from the “foo” attribute attached to the request view.forward( request, response ); SE-2840 Dr. Mark L. Hornick16

Remember: You can’t forward a Request if you’ve already committed a Response If your Servlet (or JSP) has already generated output, you’ll get an IllegalStateException: PrintWriter pw = response.getWriter(); pw.println(“ ); // start of output… … RequestDispatcher view = request.getRequestDispatcher("view.jsp"); view.forward( request, response ); // IllegalStateException! SE-2840 Dr. Mark L. Hornick17