Java Server Pages Yes it is Java. What you should know HTML Java.

Slides:



Advertisements
Similar presentations
JSP and Servelets.
Advertisements

CGI programming. Common Gateway Interface interface between web server and other programs (cgi scripts) information passed as environment variables passed.
1 Servlets Based on Notes by Dave Hollinger & Ethan Cerami Also, the Online Java Tutorial by Sun.
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.
Technology Overview JAVA Servlets CS-611 S. Witherspoon.
Introduction to Servlets Based on: Hall, Brown, Core Servlets and JavaServer Pages.
J.Sant Servlets Joseph Sant Sheridan Institute of Technology.
Object-Oriented Enterprise Application Development Tomcat 3.2 Configuration Last Updated: 03/30/2001.
Servlets and a little bit of Web Services Russell Beale.
DT211/3 Internet Application Development
18-Jun-15 JSP Java Server Pages Reference: Tutorial/Servlet-Tutorial-JSP.html.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 34 Servlets.
JSP Java Server Pages Reference:
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)
Chapter 4 Servlets Concept of Servlets (What, Why, and How) Servlet API Third-party tools to run servlets Examples of Using Servlets HTML tag with GET.
Java web development Servlet & Java server pages.
Servlets Life Cycle. The Servlet Life Cycle A servlet life cycle can be defined as the entire process from its creation till the destruction. The following.
CSCI 6962: Server-side Design and Programming History and Background.
Java Servlets and JSP.
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.
DAT602 Database Application Development Lecture 15 Java Server Pages Part 1.
M. Taimoor Khan * Java Server Pages (JSP) is a server-side programming technology that enables the creation of dynamic,
Java Servlets CS-422. Application Mapping Your servlet application will be mapped to a directory structure: –“myapp” maps to some directory C:/docs/apps/myapp.
Servlets. - Java technology for Common Gateway Interface (CGI) programming. - It is a Java class that dynamically extends the function of a web server.
AN OVERVIEW OF SERVLET TECHNOLOGY SERVER SETUP AND CONFIGURATION WEB APPLICATION STRUCTURE BASIC SERVLET EXAMPLE Java Servlets - Compiled By Nitin Pai.
Objectives Java Servlet Web Components
JSP Java Server Pages Softsmith Infotech.
Java support for WWW Babak Esfandiari (sources: Qusay Mahmoud, Roger Impey, textbook)
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.
 Embeds Java code  In HTML tags  When used well  Simple way to generate dynamic web-pages  When misused (complex embedded Java)  Terribly messy.
COMP 321 Week 7. Overview HTML and HTTP Basics Dynamic Web Content ServletsMVC Tomcat in Eclipse Demonstration Lab 7-1 Introduction.
CMPUT 391 – Database Management Systems Department of Computing Science University of Alberta CMPUT 391 Database Management Systems Web based Applications,
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.
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.
Webcommerce Computer Networks Webcommerce by Linnea Reppa Douglas Martindale Lev Shalevich.
Java Servlets & Java Server Pages Lecture July 2013.
DAT602 Database Application Development Lecture 16 Java Server Pages Part 2.
JSP Fundamentals Elements of a JSP Using Beans with JSP Integrating Servlets and JSP.
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.
Copyright © 2002 ProsoftTraining. All rights reserved. JavaServer Pages.
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.
Introduction to Server-Side Web Development Introduction to Server-Side Web Development Session II: Introduction to Server-Side Web Development with Servlets.
JavaServer Page by Antonio Ko. Overview ► Introduction ► What is a servlet? ► What can servlets do? ► Servlets Vs JSP ► Syntax ► Samples ► JavaBean ►
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.
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.
CSI 3125, Preliminaries, page 1 SERVLET. CSI 3125, Preliminaries, page 2 SERVLET A servlet is a server-side software program, written in Java code, that.
COMP9321 Web Application Engineering Semester 2, 2015 Dr. Amin Beheshti Service Oriented Computing Group, CSE, UNSW Australia Week 3 1COMP9321, 15s2, Week.
©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.
 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.
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.
Speaker Name Speaker Title Speaker Title Oracle Corporation Olivier Le Diouris Principal Product Manager Oracle Corporation Building Servlet and JSP Applications.
JSP Based on
Servlets.
Servlet Fudamentals.
Java Servlets.
Scripted Page Web App Development (Java Server Pages)
MSIS 655 Advanced Business Applications Programming
Chapter 26 Servlets.
Presentation transcript:

Java Server Pages Yes it is Java

What you should know HTML Java

Getting Familiar with your JSP server There are many such servers available – Blazix from Desiderata Software (1.5 Megabytes, JSP, Servlets and EJBs) TomCat from Apache (Approx 6 Megabytes) WebLogic from BEA Systems (Approx 40 Megabytes, JSP, Servlets and EJBs) WebSphere from IBM (Approx 100 Megabytes, JSP, Servlets and EJBs) Blazix TomCat WebLogic WebSphere

Installing Tomcat Download Tomcat Extract content to a convenient location Set CATALNA_HOME, CATALNA_BASE, JAVA_HOME, and JRE_HOME Execute start.bat and shutdown.bat to start and shutdown the tomcat server respectively.

First JSP page Hello Save this as first.jsp in CATALINA_HOME/webapps folder You may create subfolders as necessary

Adding Dynamic Contents Hello! The time is now The character sequences enclose Java expressions, which are evaluated at run time.

How to put more codes in JSP? Scriptlets Scriptlet allows you to write blocks of Java code inside the JSP. <% System.out.println( "Evaluating date now" ); java.util.Date date = new java.util.Date(); %> Hello! The time is now

How to output text from Scriptlet? <% System.out.println( "Evaluating date now" ); java.util.Date date = new java.util.Date(); %> Hello! The time is now <% // This scriptlet generates HTML output out.println( String.valueOf( date )); %> out variable is already defined in Scriptlet. The "out" variable is of type javax.servlet.jsp.JspWriter.

Yet another pre-defined: request Variable is "request". It is of type javax.servlet.http.HttpServletRequest <% System.out.println( "Evaluating date now" ); java.util.Date date = new java.util.Date(); %> Hello! The time is now <% out.println( date ); out.println( " Your machine's address is “); out.println( request.getRemoteHost()); %>

Response pre-defined variable response.sendRedirect( anotherUrl );

Mixing HTML and Scriptlet Using out variable to output all the HTML is a tedious task. Use a mixture of HTML and Scriptlet instead Ex: Number

JSP Directives We used java.util.Date. Right? Why not this? import java.util.*; and then Date It is possible to use "import" statements in JSPs. How? <% System.out.println( "Evaluating date now" ); Date date = new Date(); %> Hello! The time is now First Statement is a directive

Directives Cont’d A JSP "directive" starts with characters. Page directive – Include directive – The include directive is used to physically include the contents of another file. – – Going to include hello.jsp... –

JSP Declarations The JSP you write turns into a class definition. All the scriptlets you write are placed inside a single method of this class. You can also add variable and method declarations to this class. You can then use these variables and methods from your scriptlets and expressions.

How to put declarations To add a declaration, you must use the sequences to enclose your declarations <%! Date theDate = new Date(); Date getDate() { System.out.println( "In getDate() method" ); return theDate; } %> Hello! The time is now

Check Servelet Generated for a JSP file by Tomcat Path: if my folder is first – C:\Tomcat7\work\Catalina\localhost\first\org\apa che\jsp public void _jspService(final javax.servlet.http.HttpServletRequest request, final javax.servlet.http.HttpServletResponse response) throws java.io.IOException, javax.servlet.ServletException { …. } This is the function that includes all your scriptlets Declarations directly go into the class

Declarations go in the class as variables and methods

Why not changing the values when I refresh the page? Good question Lets study Tomcat class container – By the way what is a container?? Tomcat has three parts – Catalina Catalina is Tomcat's servlet container. Catalina implements Sun Microsystems' specifications for servlet and JavaServer Pages (JSP).servlet containerSun MicrosystemsservletJavaServer Pages – Coyote Coyote is a Connector component for Tomcat that supports the HTTP 1.1 protocol as a web server. This allows Catalina, nominally a Java Servlet or JSP container, to also act as a plain web server that serves local files as HTTP documents – Jasper Jasper is Tomcat's JSP Engine. Jasper parses JSP files to compile them into Java code as servlets (that can be handled by Catalina). At runtime, Jasper detects changes to JSP files and recompiles them.JSP files

Web Container A web container is responsible for managing the lifecycle of servlets, mapping a URL to a particular servlet and ensuring that the URL requester has the correct access rightsURL Container manages the life cycle of a Servlet – This will be discussed later when we talk about Servlets

Include and forward Tags Going to include hello.jsp... Use and see the difference

Sessions 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. JSP provides an object named session in each JSP page You can use session object to keep track of the user

Session Cont’d By default, JSPs have session tracking enabled and a new HttpSession object is instantiated for each new client automatically. You can disable sessions explicitly –

Example 1 2

Beans and Form Processing The standard way of handling forms in JSP is to define a "bean". This is not a full Java bean. You just need to define a class that has a field corresponding to each field in the form. The class fields must have "setters" that match the names of the form fields. Setters????

Setter (Yes Java Class!) public class TestSetter{ Private String name; Private int age; public void setName(String tname) // this is the setter { this.name=tname; } public String getName() // this is the getter { return this.name; }

Example What's your name? What's your address? What's your age?

Corresponding Bean Package user; public class UserData { String username; String ; int age; public void setUsername( String value ) { username = value; } public void set ( String value ) { = value; } public void setAge( int value ) { age = value; } Public String getUsername() { return username; } public String get () { return ; } public int getAge() { return age; } } Once you have defined the class, compile it and make sure it is available in the web-server's classpath

Where to save this Bean? Put it in WEB-INF\classes folder in your application folder Ex: If you create an application called blog – Create a sub folder in your blog folder as WEB-INF – Create a sub folder in WEB-INF called classes – Put your package in corresponding folders Here UserData.class must be in {Tomcat_home\webapps\blog\}WEB- INF\classes\user\UserData.class

"SaveName.jsp“ which handles the request Continue Note: jsp:useBean tag and the jsp:setProperty tag! The useBean tag will look for an instance of the "UserData" in the session. The setProperty tag will automatically collect the input data, match names against the bean method names, and place the data in the bean!

Bean Scope Page - accessible only on the page Request – can access within the same requets forwarded page can access it too. Session – throughout the session Application – whole application

Scopes cont’d //Example of JSP Page Scope //Example of JSP Request Scope //Example of JSP Session Scope //Example of JSP Application Scope

Access the user bean anywhere You entered Name: Age:

Controllers pre-processing needs to be done after the user has submitted a form Such pre-processing code is frequently referred to as a "controller“

Simple Controller <% String tgtPage = null; if ( user.choice.equals( "choice1" )) tgtPage = "tgt1.jsp"; else if ( user.choice.equals( "choice2" )) tgtPage = "tgt2.jsp"; else tgtPage = "tgtDefault.jsp"; response.sendRedirect( tgtPage ); %>

Techniques for Form Editing What's your name? "> What's your address? "> What's your age? >

Servlet Servlets provide a component-based, platform-independent method for building Web-based applications, without the performance limitations of CGI programs. Servlets have access to the entire family of Java APIs, including the JDBC API to access enterprise databases.

Architecture HTTP request HTTP response Web server JSP page JSP container compiles to a servlet URL request JavaBean Library DB properties, call methods HTTP page response Browser

Servlet Life Cycle A servlet life cycle can be defined as the entire process from its creation till the destruction. – The servlet is initialized by calling the init () method. – The servlet calls service() method to process a client's request. – The servlet is terminated by calling the destroy() method. – Finally, servlet is garbage collected by the garbage collector of the JVM.

The init() method : The init method is designed to be called only once. It is called when the servlet is first created, and not called again for each user request Used for one time initialization – public void init() throws ServletException { // Initialization code... }

The service() method : The service() method is the main method to perform the actual task When requests come from clients, servlet container invokes this method for each request – public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException { //Code goes here.}

Service Method: Cont’d The service() method checks the HTTP request type (GET, POST, PUT, DELETE, etc.) and calls doGet, doPost, doPut, doDelete, etc. methods as appropriate. The service () method is called by the container and service method invokes doGe, doPost, doPut, doDelete, etc. methods as appropriate. So you have nothing to do with service() method but you override either doGet() or doPost() depending on what type of request you receive from the client.

The doGet(), doPost() Methods: public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // Servlet code } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // Servlet code }

The destroy() method : The destroy() method is called only once at the end of the life cycle of a servlet. This method gives your servlet a chance to close database connections, halt background threads, write cookie lists or hit counts to disk, and perform other such cleanup activities – public void destroy() { // Finalization code... }

Overview

Hello World

Explanation Servlets are Java classes which service HTTP requests and implement the javax.servlet.Servlet interface Compile – As usual javac HelloWorld.java Servlet Deployment: – Place the class file in …./WEB-INF/classes – Modify web.xml in WEB-INF folder

Add this to web.xml Web.xml is in WEB-INF folder. If it is not availabe, create one. HelloWorld HelloWorld HelloWorld /HelloWorld

Sample web.xml file

Example

Example-GET

Example-POST

Examle-CheckBox

Database Connection