18-Jun-15 JSP Java Server Pages Reference: Tutorial/Servlet-Tutorial-JSP.html.

Slides:



Advertisements
Similar presentations
Java Server Pages (JSP)
Advertisements

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.
Java Servlet & JSP © copyright 2005 SNU OOPSLA Lab.
Servlets Stoney Jackson
WEB1P servintro1 Introduction to servlets and JSP Dr Jim Briggs.
An introduction to Java Servlet Programming
JSP Java Server Pages Reference:
DT228/3 Web Development JSP: Directives and Scripting elements.
27-Jun-15 Directories and DDs. 2 Web apps A web application is basically a web site that: “Knows who you are”--it doesn’t just give you static pages,
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,
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.
CSCI 6962: Server-side Design and Programming History and Background.
Java Servlets and JSP.
Introduction to Java web programming Dr Jim Briggs JWP intro1.
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.
Gayle J Yaverbaum, PhD Professor of Information Systems Penn State Harrisburg.
Java Server Pages B.Ramamurthy. Topics for Discussion 8/20/20152 Inheritance and Polymorphism Develop an example for inheritance and polymorphism JSP.
Introduction Servlets and JSP Celsina Bignoli
DAT602 Database Application Development Lecture 15 Java Server Pages Part 1.
Netbeans – jsp.zip Introduction to JSP Netbeans – jsp.zip.
JSP Java Server Pages Softsmith Infotech.
Java support for WWW Babak Esfandiari (sources: Qusay Mahmoud, Roger Impey, textbook)
J2EE training: 1 Course Material Usage Rules PowerPoint slides for use only in full-semester, for-credit courses at degree-granting.
SKT-SSU IT Training Center Servlet and JSP. Chapter Three: Servlet Basics.
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.
111 Java Servlets Dynamic Web Pages (Program Files) Servlets versus Java Server Pages Implementing Servlets Example: F15 Warranty Registration Tomcat Configuration.
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.
CMPUT 391 – Database Management Systems Department of Computing Science University of Alberta CMPUT 391 Database Management Systems Web based Applications,
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.
Java Servlets Lec 27. Creating a Simple Web Application in Tomcat.
Introduction to JavaServer Pages. 2 JSP and Servlet Limitations of servlet  It’s inaccessible to non-programmers JSP is a complement to servlet  focuses.
JSP Presented by K.Venkata Ratnam HOD MCA (Dept) Newton’s Institute of Engineering.
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.
Chapter 2 Web app architecture. High-level web app architecture  When a client request coming in and needs servlet to serve dynamic web content, what.
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.
20-Nov-15introServlets.ppt Intro to servlets. 20-Nov-15introServlets.ppt typical web page – source Hello Hello.
Chapter 11 Invoking Java Code with JSP Scripting Elements.
Java Servlets and Java Server Pages Norman White Stern School of Business.
Middleware 3/29/2001 Kang, Seungwoo Lee, Jinwon. Description of Topics 1. CGI, Servlets, JSPs 2. Sessions/Cookies 3. Database Connection(JDBC, Connection.
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 ( )
Configuration Web Server Tomcat - Install JDK Install Tom cat Configure Tom cat for running Servlet C:\Program Files\Apache Software Foundation\Tomcat.
©SoftMoore ConsultingSlide 1 Overview of Servlets and JavaServer Pages (JSP)
Java Servlets and Java Server Pages
INVOKING JAVA CODE WITH JSP SCRIPTING ELEMENTS. Creating Template Text A large percentage of your JSP document consists of static text (usually HTML),
Introduction To HTML Dr. Magdi AMER. HTML elements.
 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.
Apr 3, 2013 JSP Java Server Pages. 2 A “Hello World” servlet (from the Tomcat installation documentation) public class HelloServlet extends HttpServlet.
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.
S ERVLETS Form Data 19-Mar-16. F ORM P ROCESSING You must have come across many situations when you need to pass some information from your browser to.
World Wide Web has been created to share the text document across the world. In static web pages the requesting user has no ability to interact with the.
JSP java server pages.
Introduction Servlets and JSP
Developing JavaServer Pages
JSP Java Server Pages Reference:
Java Server Pages.
Servlets and Java Server Pages
Directories and DDs 25-Apr-19.
COSC 2956 Internet Tools Java Server Pages.
Directories and DDs 21-Jul-19.
Directories and DDs 14-Sep-19.
Presentation transcript:

18-Jun-15 JSP Java Server Pages Reference: Tutorial/Servlet-Tutorial-JSP.html

2 A “Hello World” servlet (from the Tomcat installation documentation) public class HelloServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); String docType = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 " + "Transitional//EN\">\n"; out.println(docType + " \n" + " Hello \n" + " \n" + " Hello World \n" + " "); } This is mostly Java with a little HTML mixed in

3 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 HTML mixed in The HTML is enclosed in out.println( ) statements JSP (Java Server Pages) is an alternate way of creating servlets JSP is written as ordinary HTML, with a little Java mixed in The Java is enclosed in special tags, such as The HTML is known as the template text JSP files must have the extension.jsp JSP is translated into a Java servlet, which is then compiled Servlets are run in the usual way The browser or other client sees only the resultant HTML, as usual Tomcat knows how to handle servlets and JSP pages

4 How JSP works When Tomcat needs to use a JSP page, it: Translates the JSP into a Java servlet Compiles the servlet Creates one instance of the JSP servlet Executes the servlet as normal Java code Hence, when you are writing JSP, you are writing “higher-level” Java code Each call to the JSP servlet is executed in a new Thread Since there is only one JSP object, you have to use synchronization if you use any instance variables of the servlet You have two basic choices when using JSP: Let the JSP do all the work of a servlet Write a servlet that does the work and passes the results to JSP to create the resultant HTML page This works because a servlet can call another servlet Bottom line: JSP is just a convenient way of writing Java code!

5 JSP scripting elements There is more than one type of JSP “tag,” depending on what you want done with the Java The expression is evaluated and the result is inserted into the HTML page The code is inserted into the servlet's service method If code contains declarations, they become local variables of the service method This construction is called a scriptlet The declarations are inserted into the servlet class, not into a method Hence, declarations made here become instance variables

6 Example JSP Hello! The time is now Notes: The tag is used, because we are computing a value and inserting it into the HTML The fully qualified name ( java.util.Date ) is used, instead of the short name ( Date ), because we haven’t yet talked about how to do import declarations

7 Variables You can declare your own variables, as usual JSP provides several predefined variables request : The HttpServletRequest parameter response : The HttpServletResponse parameter session : The HttpSession associated with the request, or null if there is none out : A JspWriter (like a PrintWriter ) used to send output to the client Example: Your hostname:

8 Scriptlets Scriptlets are enclosed in tags Scriptlets are executable code and do not directly affect the HTML Scriptlets may write into the HTML with out.print( value ) and out.println( value ) Example: Scriptlets are inserted into the servlet exactly as written, and are not compiled until the entire servlet is compiled Example: Have a nice day! Have a lousy day!

9 The case against scriptlets One of the principle motivations for JSP is to allow Web designers who are not Java programmers to get some of the features of Java into their pages Hence, in some cases it is desirable to put as little actual Java into your JSP as possible Where this is a goal, a better approach is to provide the necessary Java functionality via methods in a class that is loaded along with the servlet

10 Declarations Use for declarations to be added to your servlet class, not to any particular method Caution: Servlets are multithreaded, so nonlocal variables must be handled with extreme care If declared with, variables are local and OK If declared with, variables may need to be synchronized Data can also safely be put in the request or session objects Example: Accesses to page since server reboot: You can use to declare methods as easily as to declare variables

11 Directives Directives affect the servlet class itself A directive has the form: or The most useful directive is page, which lets you import packages Example:

12 The include directive The include directive inserts another file into the file being parsed The included file is treated as just more JSP, hence it can include static HTML, scripting elements, actions, and directives Syntax: The URL is treated as relative to the JSP page If the URL begins with a slash, it is treated as relative to the home directory of the Web server The include directive is especially useful for inserting things like navigation bars

13 Actions Actions are XML-syntax tags used to control the servlet engine Inserts the indicated relative URL at execution time (not at compile time, like the include directive does) This is great for rapidly changing data " /> Jump to the (static) URL or the (dynamically computed) JavaExpression resulting in a URL

14 JSP in XML JSP can be embedded in XML as well as in HTML Due to XML’s syntax rules, the tags must be different (but they do the same things) HTML: XML: expression HTML: XML: code HTML: XML: declarations HTML: XML:

15 Comments You can put two kinds of comments in JSP: This is an ordinary HTML comment, and forms part of the page that you send to the user Hence, the user can see it by doing View source JSP scriptlets in HTML comments will be executed This kind of comment will be stripped out when the JSP is compiled It’s intended for page developers; the user will never see it

16 The End