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.

Slides:



Advertisements
Similar presentations
JavaScript FaaDoOEngineers.com FaaDoOEngineers.com.
Advertisements

Java Script Session1 INTRODUCTION.
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.
Using JavaServer Pages Harry R. Erwin, PhD CIT304/CSE301.
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.
JSP Java Server Pages Reference:
DT228/3 Web Development JSP: Directives and Scripting elements.
Java Server Pages B.Ramamurthy. Java Server Pages Servlets are pure Java programs. They introduce dynamism into web pages by using programmatic content.
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,
Comp2513 Java Server Pages Daniel L. Silver, Ph.D.
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.
CMPUT 391 – Database Management Systems Department of Computing Science University of Alberta CMPUT 391 Database Management Systems JavaServer Pages (JSP)
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 web development Servlet & Java server pages.
Java Server Pages B.Ramamurthy. Topics for Discussion 8/20/20152 Inheritance and Polymorphism Develop an example for inheritance and polymorphism JSP.
Java Server Pages (JSP) Presented by: Ananth Prasad & Alex Ivanov May 10, 2001.
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.
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,
FALL 2005CSI 4118 – UNIVERSITY OF OTTAWA1 Part 4 Web technologies: HTTP, CGI, PHP,Java applets)
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.
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.
Webcommerce Computer Networks Webcommerce by Linnea Reppa Douglas Martindale Lev Shalevich.
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.
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.
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.
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.
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 ( )
©SoftMoore ConsultingSlide 1 Overview of Servlets and JavaServer Pages (JSP)
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.
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.
 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,
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.
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.
1 CSC160 Chapter 1: Introduction to JavaScript Chapter 2: Placing JavaScript in an HTML File.
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.
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.
Developing JavaServer Pages
JSP Java Server Pages Reference:
Java Server Pages.
Invoking Java Code from JSP
Java Server Pages B.Ramamurthy.
JSP Directives 1-Jan-19.
Presentation transcript:

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 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 JS P pages

JAVA SERVER PAGES (JSP) JSP is dynamic web page –JSP is written as ordinary HTML, with a little Java mixed –The Java is enclosed in special tags, such as –JSP files must have the extension.jsp JSP is translated in to a Javaservlet,which is then compiled

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!

JSP LIFE CYCLE Some how similar to Servlet life cycle. When any request mapped to a JSP, it is handled by a special Servlet event. Servlet checks the java Server Page’s Servlet is older than the java Server Page. If so it translates to java Server Page into a Servlet class & compiles the class. Next the Servlet sends response to the java server page.

ADVANTAGES OF JSP Build process is performed automatically. Translation phase treat each data type in java Server Pages differently. Template data is transformed into code. This code emits that data stream that returns data to the client. It is faster than Servlet.

THE PROCESSING OF JSP When the browser asks the Web server for a JSP, the Web server passes control to a JSP container. A container works with the Web server to provide the runtime environment and other services a JSP needs. It knows how to understand the special elements that are part of JSPs. Because this is the first time this JSP has been invoked, the JSP container converts it into an executable unit called a Servlet.

CONT’D The entire page, including the parts that are in HTML, is translated into source code. After code translation, the JSP container compiles the Servlet, loads it automatically, and then executes it. Typically, the JSP container checks to see whether a Servlet for a JSP file already exists. If not it do the translation process If version not match do the translation process

ORGANIZATION OF THE PLATFORM

JSP ENVIRONMENT

JSP FLOW

LOOK AT THE FLOW

JSP TAGS –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

STRUCTURE OF A JSP FILE

16 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

EXAMPLE JSP First JSP page. In jsp java codes are written between ' ' tags. So it takes the following form : In this example we have use

18 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

19 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!

20 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 which is loaded along with the servlet

SCRIPLET TAG

CON’T

23 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

DECLARATIONS Use tag for declarations –If declared with, variables are local Example: : You can use to declare methods as easily as to declare variables

JSP COMMENTS Different from HTML comments. HTML comments are visible to client. JSP comments are used for documenting JSP code. JSP comments are not visible client-side.

28 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:

30 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

31 ACTION TAGS 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

32 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:

ACTIONS Name:

JSP IMPLICIT OBJECTS JSP provides several implicitObject –request: The HttpServletRequestparameter –response: The HttpServletResponseparameter –session: The HttpSessionassociated with the request, or nullif there is none –out: A JspWriter(like a PrintWriter) used to send output to the client –application : Exist through out the application –exception :Show the error information

EXAMPLE (IMPLICIT OBJECT) Request : request.getQueryString(); request.getParameter("name"); Example: Your hostname: Response : response.sendRedirect(" response.setHeader("Cache-Control","no-cache"); response.setContentType("text/html");

SESSION IN JSP In session management whenever a request comes for any resource, a unique token is generated by the server and transmitted to the client by the response object and stored on the client machine as a cookie. Sessionmanagement (i)SessionObject (ii)Cookies (iii)HiddenFormFields (iv)URLRewriting

SESSION IN JSP Set Session Attribute String svalue = request.getParameter("sesvalue_txt"); if(svalue!=null) { session.setAttribute("sesval",request.getParameter("sesval ue_txt")); }

SESSION IN JSP Using Session Attribute Hello Remove Session Attribute

APPLICATION OBJECT IN JSP <% Integer hitsCount = (Integer)application.getAttribute("hitCounter"); if( hitsCount ==null || hitsCount == 0 ){ out.println("Welcome to my website!"); HitsCount = 1; }else{ out.println("Welcome back to my website!"); hitsCount += 1; } application.setAttribute("hitCounter", hitsCount); %> Total number of visits: