Java Server Pages (JSP)

Slides:



Advertisements
Similar presentations
Chapter 6 Server-side Programming: Java Servlets
Advertisements

Java Script Session1 INTRODUCTION.
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: JavaServer Pages Juan Cruz Kevin Hessels Ian Moon.
Servlets and a little bit of Web Services Russell Beale.
WEB1P servintro1 Introduction to servlets and JSP Dr Jim Briggs.
28/1/2001 Seminar in Databases in the Internet Environment Introduction to J ava S erver P ages technology by Naomi Chen.
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.
1 Java Server Pages Can web pages be created specially for each user? What part does Java play?
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 Server Pages B.Ramamurthy. Topics for Discussion 8/20/20152 Inheritance and Polymorphism Develop an example for inheritance and polymorphism JSP.
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)
CNIT 133 Interactive Web Pags – JavaScript and AJAX JavaScript Environment.
JSP Java Server Pages Softsmith Infotech.
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 A JSP page is a text-based document that contains two types of text: static template data, which can be expressed in any text-based format,
 Embeds Java code  In HTML tags  When used well  Simple way to generate dynamic web-pages  When misused (complex embedded Java)  Terribly messy.
9 Chapter Nine Compiled Web Server Programs. 9 Chapter Objectives Learn about Common Gateway Interface (CGI) Create CGI programs that generate dynamic.
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:
JSTL Lec Umair©2006, All rights reserved JSTL (ni) Acronym of  JavaServer Pages Standard Tag Library JSTL (like JSP) is a specification, not an.
Introduction to JavaServer Pages. 2 JSP and Servlet Limitations of servlet  It’s inaccessible to non-programmers JSP is a complement to servlet  focuses.
Chapter 6 Server-side Programming: Java Servlets
Writing Enterprise Applications with J2EE (Fourth lesson) Alessio Bechini June 2002 (based on material by Monica Pawlan)
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.
3/6/00jsp00 1 Java Server Pages Nancy McCracken Northeast Parallel Architectures Center at Syracuse University.
1 Java Servlets l Servlets : programs that run within the context of a server, analogous to applets that run within the context of a browser. l Used to.
JSP BASICS AND ARCHITECTURE. Goals of JSP Simplify Creation of dynamic pages. Separate Dynamic and Static content.
© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/3 JSP Application Models.
Basic JSP Celsina Bignoli Problems with Servlets Servlets contain –request processing, –business logic –response generation all lumped.
8 Chapter Eight Server-side Scripts. 8 Chapter Objectives Create dynamic Web pages that retrieve and display database data using Active Server Pages Process.
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 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.
Chapter 14 Using JavaBeans Components in JSP Documents.
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.
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.
Java Server Pages Can web pages be created specially for each user?
Plateform for Enterprise Solution: Java EE5
WWW and HTTP King Fahd University of Petroleum & Minerals
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.
Java Server Pages.
Java Server Pages By: Tejashri Udavant..
Java Servlets.
Scripted Page Web App Development (Java Server Pages)
Pre-assessment Questions
MSIS 655 Advanced Business Applications Programming
JSP Syntax.
Introduction to Java Server Pages
Chapter 27 WWW and HTTP.
PHP.
Java Server Pages B.Ramamurthy.
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:

Java Server Pages (JSP) SE IT/ sem 4/ WP

JSP overview Java technology to create dynamic web pages Uses special JSP tags in HTML which begins with <% and ends with %> It is type of Java Servlet which acts as user interface for Java Web Application. Using JSP, you can collect input from users through web page forms, present records from a database or another source, and create web pages dynamically. JSP tags can be used for a variety of purposes, such as retrieving information from a database or registering user preferences, accessing JavaBeans components, passing control between pages and sharing information between requests, pages etc.

Why use JSP? JSP allows embedding Dynamic Elements in HTML Pages, JSP are always compiled before it's processed by the server, JSP also has access to all the powerful Enterprise Java APIs, including JDBC, JNDI, EJB, JAXP etc , JSP pages can be used in combination with servlets, Finally, JSP is an integral part of J2EE, a complete platform for enterprise class applications. This means that JSP can play a part in the simplest applications to the most complex and demanding.

Advantages of JSP over other languages vs. Active Server Pages (ASP): The advantages of JSP are twofold. First, the dynamic part is written in Java, not Visual Basic or other MS specific language, so it is more powerful and easier to use. Second, it is portable to other operating systems and non-Microsoft Web servers, vs. Pure Servlets: It is more convenient to write (and to modify!) regular HTML than to have plenty of println statements that generate the HTML , vs. JavaScript: JavaScript can generate HTML dynamically on the client but can hardly interact with the web server to perform complex tasks like database access and image processing etc. vs. Static HTML: Regular HTML, of course, cannot contain dynamic information.

How to start Apache Tomcat for JSP? Install JDK & JRE from java.sun.com Go for tomcat.apache.org  download section  binary distribution  core (zip) Unzip and rename (keep JSP files in web apps folder of Apache tomcat folder) Set environment variable as name : JAVA_HOME and value : location of JRE Start server by going in bin folder Apache tomcat  click on startup.bat In browser write localhost:8080/ and view the JSP page To stop server go in bin folder of Apache tomcat  click on shutdown.bat

Alternate way Install NetBeans IDE with Apache Tomcat Server option on. Follow the same process as specified in Servlet section.

JSP architecture The web server needs a JSP engine ie. container to process JSP pages. The JSP container is responsible for intercepting requests for JSP pages. Apache has built-in JSP container to support JSP pages development. This JSP engine will talk interact with database to perform operation.

JSP processing As with a normal page, browser sends an HTTP request to the web server. The web server recognizes that the HTTP request is for a JSP page and forwards it to a JSP engine. This is done by using the URL or JSP page which ends with .jsp instead of .html. The JSP engine loads the JSP page from disk and converts it into a servlet content. This conversion is very simple in which all template text is converted to println( ) statements and all JSP elements are converted to Java code that implements the corresponding dynamic behavior of the page.

JSP Processing (cont…) The JSP engine compiles the servlet into an executable class and forwards the original request to a servlet engine. A part of the web server called the servlet engine loads the Servlet class and executes it. During execution, the servlet produces an output in HTML format, which the servlet engine passes to the web server inside an HTTP response. The web server forwards the HTTP response to browser in terms of static HTML content. Finally web browser handles the dynamically generated HTML page inside the HTTP response exactly as if it were a static page.

JSP Processing (figure)

JSP v/s PHP Typically, the JSP engine checks to see whether a servlet for a JSP file already exists and whether the modification date on the JSP is older than the servlet. If the JSP is older than its generated servlet, the JSP container assumes that the JSP hasn't changed and that the generated servlet still matches the JSP's contents. This makes the process more efficient than with other scripting languages (such as PHP) and therefore faster. Where as PHP runs the script every time the page is loaded.

JSP life cycle A JSP life cycle can be defined as the entire process from its creation till the destruction which is similar to a servlet life cycle with an additional step which is required to compile a JSP into servlet. The following are the paths followed by a JSP Compilation Initialization Execution Cleanup

JSP life cycle (figure)

JSP life cycle : Compilation When a browser asks for a JSP, the JSP engine first checks to see whether it needs to compile the page. If the page has never been compiled, or if the JSP has been modified since it was last compiled, the JSP engine compiles the page. The compilation process involves three steps: Parsing the JSP. Turning the JSP into a servlet. Compiling the servlet.

JSP life cycle : initialization When a container loads a JSP it invokes the jspInit() method before servicing any requests. If you need to perform JSP-specific initialization, override the jspInit() method. Typically initialization is performed only once and as with the servlet init method, you generally initialize database connections, open files, and create lookup tables in the jspInit method.

JSP life cycle : execution This phase of the JSP life cycle represents all interactions with requests until the JSP is destroyed. Whenever a browser requests a JSP and the page has been loaded and initialized, the JSP engine invokes the _jspService() method in the JSP. The _jspService() method takes an HttpServletRequest and an HttpServletResponse as its parameters. The _jspService() method of a JSP is invoked once per a request and is responsible for generating the response for that request and this method is also responsible for generating responses to all seven of the HTTP methods ie. GET, POST, DELETE etc.

JSP life cycle : cleanup The destruction phase of the JSP life cycle represents when a JSP is being removed from use by a container. The jspDestroy() method is the JSP equivalent of the destroy method for servlets. Override jspDestroy when you need to perform any cleanup, such as releasing database connections or closing open files.

JSP syntax : Scriptlet A scriptlet can contain any number of JAVA language statements, variable or method declarations, or expressions that are valid in the page scripting language. Syntax: <% code fragment %> XML equivalent of the above syntax: <jsp:scriptlet> code fragment </jsp:scriptlet> Any text, HTML tags, or JSP elements you write must be outside the scriptlet.

JSP : declarations A declaration declares one or more variables or methods that you can use in Java code later in the JSP file. You must declare the variable or method before you use it in the JSP file. Syntax : <%! declaration; [ declaration; ]+ ... %> Example <%! int i = 0; %>

JSP : expression A JSP expression element contains a scripting language expression that is evaluated, converted to a String, and inserted where the expression appears in the JSP file. The expression element can contain any expression that is valid according to the Java Language Specification but you cannot use a semicolon to end an expression. Syntax <%= expression %>

JSP : comments JSP comment marks text or statements that the JSP container should ignore. A JSP comment is useful when you want to hide or "comment out" part of your JSP page. Syntax <%-- This is JSP comment --%> <!– this HTML comment -->

JSP : actions JSP actions use constructs in XML syntax to control the behavior of the servlet engine. You can dynamically insert a file, reuse JavaBeans components, forward the user to another page, or generate HTML for the Java plugin. Syntax <jsp:action_name attribute="value" />

JSP : actions list Action Purpose jsp:include Includes a file at the time the page is requested jsp:useBean Finds or instantiates a JavaBean jsp:setProperty Sets the property of a JavaBean jsp:getProperty Inserts the property of a JavaBean into the output jsp:forward Forwards the requester to a new page jsp:plugin Generates browser-specific code that makes an OBJECT or EMBED tag for the Java plugin jsp:element Defines XML elements dynamically. jsp:attribute Defines dynamically defined XML element's attribute. jsp:body Defines dynamically defined XML element's body. jsp:text Use to write template text in JSP pages and documents.

JSP : implicit objects JSP supports nine automatically defined variables, which are also called implicit objects.

JSP : if-else statement Syntax: if(condition) { } else if(condition) else

JSP : for loop Syntax <%! int loopVar; %> <% for(initialize; condition; increDecre) { code } %>

JSP : operators

How to include one jsp file in another? Syntax <jsp:include page="relative URL" flush="true" /> flush : The boolean attribute determines whether the included resource has its buffer flushed before it is included.

Dynamic XML To create XML element <jsp:element name="xmlElement"> … </jsp:element> To set attribute of an XML element <jsp:attribute name="xmlElementAttr"> Value for the attribute </jsp:attribute> To set body for XML element <jsp:body> Body for XML element </jsp:body>

JSP : form processing One can send data via GET or POST method. request.getParameter() method to get the value of a form parameter. getParameterValues() method if the parameter appears more than once and returns multiple values, for example checkbox. getParameterNames() method if you want a complete list of all parameters in the current request.

JSP : servlet cookies methods

JSP : cookie writing Creating a Cookie object Cookie cookie = new Cookie("key","value"); Setting the maximum age cookie.setMaxAge(60*60*24); Sending the Cookie into the HTTP response headers response.addCookie(cookie);

JSP : cookies reading Cookie x = null; Cookie[] a = request.getCookies(); if(a!=null) { for(int i=0; i<a.length; i++) x = a[i]; out.println(x.getName() + " : " + x.getValue() + "<br>" ); }

JSP : cookie deleting Read an already existing cookie and store it in Cookie object. Set cookie age as zero using setMaxAge() method to delete an existing cookie. Add this cookie back into response header.

Session tracking HTTP is a "stateless" protocol which means each time a client retrieves a Web page, the client opens a separate connection to the Web server and the server automatically does not keep any record of previous client request. Still there are following four ways to maintain session between web client and web server: Cookies Hidden form fields <input type="hidden" name="sessionid" value="12345"> URL rewriting http://xyz.com/file.htm?sessionid=12345 Session

JSP : session JSP makes use of servlet provided HttpSession Interface which provides a way to identify a user across more than one page request or visit to a Web site and to store information about that user. By default, JSPs have session tracking enabled and a new HttpSession object is instantiated for each new client automatically.

Session methods

Page redirection <% // New location to be redirected String site = new String("http://www.google.com"); response.setStatus(response.SC_MOVED_TEMPORARILY); response.setHeader("Location", site); %>

JSP hit counter