Java Server Pages (JSP)

Slides:



Advertisements
Similar presentations
Servlets & JSPs - Sharad Ballepu.
Advertisements

4 Copyright © 2005, Oracle. All rights reserved. Creating the Web Tier: Servlets.
8 Copyright © 2005, Oracle. All rights reserved. Creating the Web Tier: JavaServer Pages.
JSP and Servelets.
CGI programming. Common Gateway Interface interface between web server and other programs (cgi scripts) information passed as environment variables passed.
M-V-C for web applications. Model for Web Applications model consists of data and system state database tables –persistent data session information –current.
1 Web Search Interfaces. 2 Web Search Interface Web search engines of course need a web-based interface. Search page must accept a query string and submit.
Servlets, JSP and JavaBeans Joshua Scotton.  Getting Started  Servlets  JSP  JavaBeans  MVC  Conclusion.
Cookie in a servlet. Cookies are small bits of textual information that a Web server sends to a browser and that the browser returns unchanged when visiting.
1 Servlets Based on Notes by Dave Hollinger & Ethan Cerami Also, the Online Java Tutorial by Sun.
CS4273: Distributed System Technologies and Programming I Lecture 11: JavaServer Pages (JSP)
Apache Tomcat as a container for Servlets and 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.
Java Servlets Java Server Pages (JSP)
JSP1 Java Server Pages (JSP) Introducing JavaServer Pages TM (JSP TM ) JSP scripting elements.
 2002 Prentice Hall. All rights reserved. Chapter 9: Servlets Outline 9.1 Introduction 9.2 Servlet Overview and Architecture Interface Servlet and.
 2003 Prentice Hall, Inc. All rights reserved. Chapter 24: Servlets Outline 24.1 Introduction 24.2 Servlet Overview and Architecture Interface.
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,
Introduction to Servlet & 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.
Servlets. Our Project 3-tier application Develop our own multi-threaded server Socket level communication.
CSCI 6962: Server-side Design and Programming History and Background.
Java Servlets and JSP.
Chapter 10 Servlets and Java Server Pages. A servlet is a Java class designed to be run in the context of a special servlet container An instance of the.
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.
Servlets. - Java technology for Common Gateway Interface (CGI) programming. - It is a Java class that dynamically extends the function of a web server.
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.
SKT-SSU IT Training Center Servlet and JSP. Chapter Three: Servlet Basics.
Web Server Programming 1. Nuts and Bolts. Premises of Course Provides general introduction, no in-depth training Assumes some HTML knowledge Assumes some.
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.
JSP Fundamentals Elements of a JSP Using Beans with JSP Integrating Servlets and JSP.
CSC 2720 Building Web Applications JavaServer Pages (JSP) The Basics.
Jsp (Java Server Page) Is a server side program.
20-Nov-15introServlets.ppt Intro to servlets. 20-Nov-15introServlets.ppt typical web page – source Hello Hello.
A seminar on j2ee by saritha. s. What is J2EE J2EE (Java 2 Platform, Enterprise Edition) is a Java platform designed for the mainframe-scale computing.
Java Servlet API CGI / HTTP Concepts Java Servlet API.
Chapter 11 Invoking Java Code with JSP Scripting Elements.
Java Servlets and Java Server Pages Norman White Stern School of Business.
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.
Middleware 3/29/2001 Kang, Seungwoo Lee, Jinwon. Description of Topics 1. CGI, Servlets, JSPs 2. Sessions/Cookies 3. Database Connection(JDBC, Connection.
CSI 3125, Preliminaries, page 1 SERVLET. CSI 3125, Preliminaries, page 2 SERVLET A servlet is a server-side software program, Responds oriented other.
1 Introduction to Servlets. Topics Web Applications and the Java Server. HTTP protocol. Servlets 2.
CS320 Web and Internet Programming Introduction to Java Servlets Chengyu Sun California State University, Los Angeles.
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)
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 21 Java Servlets Wed. 11/22/00 based on material.
Java Servlets and Java Server Pages
How CGI and Java Servlets are Run By David Stein 14 November 2006.
 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 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.
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.
CS320 Web and Internet Programming Introduction to Java Servlets Chengyu Sun California State University, Los Angeles.
Programming with Java Lecture 6 Elements of a Java Servlet
Java Server Pages.
Servlets and JSP 20-Nov-18 servletsJSP.ppt.
Java Servlets and JSP.
Servlets Servlets are modules that extend the functionality of a “java-enabled” web-server They normally generate HTML code and web content dynamically.
Servlets Servlets are modules that extend the functionality of a “java-enabled” web-server They normally generate HTML code and web content dynamically.
Presentation transcript:

Java Server Pages (JSP)

A Java Servlets Example A simple "HelloWorld" servlet, that also prints the current date. import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class HelloWorld extends HttpServlet { public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { res.setContentType("text/html"); PrintWriter out = res.getWriter(); out.println("<HTML>"); out.println("<HEAD><TITLE>Hello World</TITLE></HEAD>"); out.println("<BODY>"); out.println("<H1>Hello World</H1>"); out.println("Today is: " + (new java.util.Date().toString()) ); out.println("</BODY></HTML>"); } // doGet } // HelloWorld In order to run it, do the following: Place it in a file, HelloWorld.java Compile it. Place the resulting HelloWorld.class file in the "servlets" directory. Run it by pointing browser to: http://server.address/servlets/HelloWorld

A JSP Example The previous page can be written using JSP as shown below: <HTML> <HEAD> <TITLE>Hello World</TITLE> </HEAD> <BODY> <H1>Hello World</H1> Today is: <%= new java.util.Date().toString() %> </BODY> </HTML> In order to run it, do the following: Place it in a file, HelloWorld.jsp in the same directory as your .html files Run it by pointing browser to: http://server.address/~youraccount/HelloWorld.jsp

Elements of JSP Whenever a .jsp is requested for the first time, the server does the following: 1. Translates the .jsp page into a servlet 2. Compiles the servlet into a class file 3. Executes the servlet (response is sent to the client) Subsequent requests (as long as the .jsp page is unchanged) use the same loaded class file. Anatomy of a JSP Page A JSP page is a mixture of standard HTML tags, web page content, and some dynamic content that is specified using JSP constructs. Everything except the JSP constructs is called Template Text.

JSP Constructs: JSP Comments JSP Comments: Different from HTML comments <!-- an HTML comment --> <%-- a JSP comment --%> JSP comments are used for documenting JSP code and are not visible client-side (using browser's View Source option) where as HTML comments are visible.

JSP Constructs: JAVA Expressions Format: <%= some_java_expression %> Example: <%= new java.util.Date().toString() %> Output of the expression is placed in the HTML template at the same location.

JSP Constructs: Variables in JAVA Expressions There are some pre-defined Java variables/objects available for use in expressions (provide access to important servlet functionality): request This is the same object as HttpServletRequest parameter in th get/post methods. Same methods (like, getParameter, getAttribute, etc) can be applied to it. out The servlet printwriter. session Same as servlet session object. Example: <HTML> <HEAD> <TITLE>JSP Expressions: Predefined Objects</TITLE> </HEAD> <BODY> <H1>Using Predefined Objects</H1> <UL> <LI> Your Hostname: <%= request.getRemoteHost() %> <LI> Your Session ID: <%= session.getId() %> <LI> The value of INFO parameter: <%= request.getParameter("INFO") %> </BODY> </HTML>

JSP Constructs: Scriptlets Scriptlets are arbitrary pieces of Java code inserted in the page using the format: <% some_java_code %> Example 2 <HTML> <HEAD> <TITLE>JSP: Scriptlets 2</TITLE> </HEAD> <% String bgColor = request.getParameter("COLOR"); %> <% if (bgColor == null) { %> <BODY BGCOLOR="FFFFFF" > <% } else { %> <BODY BGCOLOR="<%= bgColor %>" > <% } %> <H1>Example Scriptlet: Conditionally sets background color</H1> You did not supply a color, I used white. Here is the color you requested. </BODY> </HTML Example 1 <HTML> <HEAD> <TITLE>JSP: Scriptlets</TITLE> </HEAD> <% String bgColor = request.getParameter("COLOR"); if (bgColor == null) bgColor = "WHITE"; %> <BODY BGCOLOR="<%= bgColor %>" > <H1>Example Scriptlet: Sets background color</H1> </BODY> </HTML>

JSP Constructs: Scriptlets, contd. Using Arrays: One can easily use scriptlets to loop over arrays. In this example, the user is presented with choice boxes. When s/he presses the submit button, the choices are displayed. Example <HTML> <BODY BGCOLOR="WHITE"> <FORM ACTION="choices.jsp"> <INPUT type="checkbox" name="music" value="Classical"> Classical<BR> <INPUT type="checkbox" name="music" value="Rock"> Rock<BR> <INPUT type="checkbox" name="music" value="Jazz"> Jazz<BR> <INPUT type="checkbox" name="music" value="Blues"> Blues<BR> <INPUT type="checkbox" name="music" value="DC-GoGo"> DC GoGo<BR> <INPUT type="submit" value="Submit"> </FORM> <% String[] selected = request.getParameterValues("music"); if (selected != null && selected.length != 0) { %> You like the following kinds of music: <UL> for (int i = 0; i < selected.length; i++) { out.println("<LI>" + selected[i]); } <% } %> </BODY> </HTML>

JSP Constructs: Declarations You can define variables and/or methods: <%! some JAVA declarations %> Example <HTML> <BODY> <%! private int hitCount = 0; String randomColor() { java.util.Random random = new java.util.Random(); int R = (int) (random.nextFloat() * 255); int G = (int) (random.nextFloat() * 255); int B = (int) (random.nextFloat() * 255); return "#" + Integer.toString(R, 16) + Integer.toString(G, 16) + Integer.toString(B, 16); } %> <FONT COLOR="<%= randomColor() %>" > This page has been accessed <%= ++hitCount %> times. </FONT> </BODY> </HTML>

JSP Constructs: Summary Expressions Scriptlets Declarations Availability of pre-defined objects

JSP Directives Format: <%@ directive attribute="value" %> <%@ directive attr1="value" attr2="value" ... attrN="value" %> Directives are used to specify the structure of the resulting servlet. There are three directives: page, include, and taglib

JSP: Page Directive There are 11 specifiable attributes for this directive: import, contentType, isThreadSafe, session, buffer, autoflush, extends, info, errorPage, and language Example <%@ page language="java" contentType="text/html" %> <%@ page contentType="application/vnd.ms-excel" %> <%@ page import="java.util.*" %> <%@ page import="java.util.*,java.io.*" %> Directives can be placed anywhere in the document, but are often placed at the very top. <%@ page language="java" contentType="text/html import="java.util.*" %> <HTML> <HEAD> <TITLE>Hello World</TITLE> </HEAD> <BODY> <H1>Hello World</H1> Today is: <%= new Date().toString() %> </BODY> </HTML>

JSP: Other Directives The include Directive Is used to include a file in the main document. There are two versions of this. The first one, shown below, includes the file at translation time. <%@ include file="relative URL of file" %> The second version, includes the file at request time. <jsp:include page="ralative URL of file" flush=true /> The taglib directive This is used to define custom markup tags. Refer to JSP documentation for details on this.