Java Servlets.

Slides:



Advertisements
Similar presentations
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 10 Servlets and Java Server Pages.
Advertisements

Java Server Pages (JSP)
Servlets and a little bit of Web Services Russell Beale.
WEB1P servintro1 Introduction to servlets and JSP Dr Jim Briggs.
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 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 Compiled by Dr. Billy B. L. Lim. Servlets Servlets are Java programs which are invoked to service client requests on a Web server. Servlets extend.
SE-2840 Dr. Mark L. Hornick1 Java Servlet-based web apps Servlet Architecture.
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.
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.
ASHIMA KALRA.  INTRODUCTION TO JSP INTRODUCTION TO JSP  IMPLICIT OBJECTS IMPLICIT OBJECTS  COOKIES COOKIES.
AN OVERVIEW OF SERVLET TECHNOLOGY SERVER SETUP AND CONFIGURATION WEB APPLICATION STRUCTURE BASIC SERVLET EXAMPLE Java Servlets - Compiled By Nitin Pai.
Chapter 17 - Deploying Java Applications on the Web1 Chapter 17 Deploying Java Applications on the Web.
Java support for WWW Babak Esfandiari (sources: Qusay Mahmoud, Roger Impey, textbook)
Chapter 5 Java Servlets. Objectives Explain the nature of a servlet and its operation Use the appropriate servlet methods in a web application Code the.
J2EE training: 1 Course Material Usage Rules PowerPoint slides for use only in full-semester, for-credit courses at degree-granting.
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,
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.
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 & Java Server Pages Lecture July 2013.
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.
© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/3 Session Management.
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.
Writing Enterprise Applications with J2EE (Fourth lesson) Alessio Bechini June 2002 (based on material by Monica Pawlan)
Jsp (Java Server Page) Is a server side program.
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.
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.
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.
JS (Java Servlets). Internet evolution [1] The internet Internet started of as a static content dispersal and delivery mechanism, where files residing.
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.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 21 Java Servlets Wed. 11/22/00 based on material.
CSI 3125, Preliminaries, page 1 JSP (Java Server Pages)
HTTP protocol Java Servlets. HTTP protocol Web system communicates with end-user via HTTP protocol HTTP protocol methods: GET, POST, HEAD, PUT, OPTIONS,
 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.
8-Mar-16 More About Servlets Session Tracking. Persistent information A server site typically needs to maintain two kinds of persistent (remembered) information:
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.
CS122B: Projects in Databases and Web Applications Spring 2017
Introduction to Servlets
CS122B: Projects in Databases and Web Applications Winter 2017
Servlets.
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.
Servlet Fudamentals.
Java Server Pages (JSP)
Java Servlets By: Tejashri Udavant..
Pre-assessment Questions
Servlet API and Lifecycle
MSIS 655 Advanced Business Applications Programming
Chapter 26 Servlets.
CS122B: Projects in Databases and Web Applications Winter 2018
Introduction to Servlets
CS122B: Projects in Databases and Web Applications Spring 2018
Servlet APIs Every servlet must implement javax.servlet.Servlet interface Most servlets implement the interface by extending one of these classes javax.servlet.GenericServlet.
Objectives In this lesson you will learn about: Need for servlets
CS3220 Web and Internet Programming Introduction to Java Servlets
J2EE Lecture 1:Servlet and JSP
CS122B: Projects in Databases and Web Applications Winter 2019
Java Chapter 7 (Estifanos Tilahun Mihret--Tech with Estif)
Presentation transcript:

Java Servlets

All Servlets need to extend from javax.servlet.http.HttpServlet

A WebServer is required to a run a servlet. A Servlet is a Server Side Java Program, which accepts request from Web Browser(Client), processes the request, and sends the response(generally HTML code) back to the Client. A WebServer is required to a run a servlet. Below are some examples of Web Server. Apache Tomcat Glass Fish Server,etc..

1.Request (HttpServletRequest) Web Server 1.Request (HttpServletRequest) Web Browser(Client) Java Servlet 2.Response (HttpServletResponse) Database

To develop a Servlet, the package javax.servlet need to be imported. A Servlet program is generally extended from javax.servlet.http.HttpServlet class. Then override any or both of the methods doGet() doPost() Both above methods have two parameters HttpServletRequest – object of this class has all data submitted from Web Browser HttpServletResponse – response from Servlet is sent to Web Browser, by putting in this object.

Deployment Descriptor Web.xml is deployment Descriptor, which has list of Servlet classes, initialization parameters, etc.. Web server gets the details of Servlet thru web.xml http://localhost:8080/ServletExample/NewServlet Folder name on server Domain name Port number Servlet name

Below are life cycle methods of a Servlet Servlet Life Cycle Below are life cycle methods of a Servlet init(ServletConfig config) – gets invoked only once, when first request is being received by Server. Service() – this method further invokes doGet() or doPost() – Get invoked once, for every request destroy() – gets invoked only when server is shutdown or when server is out of memory. After destroy() gets invoked Servlet no more exists in memory. Again when a new request is received, init( ) gets invoked.

Servlet Cookie A Cookie is a small piece of information which a server side program(such as Java Servlet) can store and retrieve, to and from Browser. A Cookie has Name Value(only plain text allowed) Expiry Disadvantages of Cookies are: Cookies can be viewed by User Cookies can be deleted by User

Servlet Filter A filter is an object that is invoked at the preprocessing and postprocessing of a request from and to Browser. It is mainly used to perform filtering tasks such as conversion, logging, compression, encryption and decryption, input validation etc. The servlet filter is dynamically pluggable, i.e. its entry is defined in the web.xml(Deployment Descriptor) file, if we remove the entry of filter from the web.xml file, filter will be removed automatically and we don't need to change the servlet. i..e no recompilation and deployment of webapplication is required Hence, maintenance cost is less

Preprocessing a Request Web Server Preprocessing a Request F I L T E R Web Browser(Client) 1.Request 2 Java Servlet 4.Response 3 Post processing a Response

HttpSession HTTP is the protocol used between Browser and Web Server. A session stores user specific details, for a period for which user is logged in to the site. Since HTTP Protocol is session less, it is the responsibility of server side program(like Java Servlets, JSP) to maintain Session details, when a session is in progress between Browser and Web Server. Generally session stores user specific details, like the user who is currently logged in, etc… A Servlet can Create a new session(this is done, generally when user logs in) Retrieve an existing session Set, Get or remove an attribute to and from a session Destroy a Session(this is done, when user logs out)

Usage of Filter recording all incoming requests logs the IP addresses of the computers from which the requests originate Can block requests from certain IP addr or range of IP addresses. conversion data compression encryption and decryption input validation etc. Advantage of Filter Filter is pluggable. One filter don't have dependency onto another resource. Less Maintenance

Another Advantage of Filters is, it is possible to have multiple Filters at a time, each Filter can be plugged out or plugged in(just by changing web.xml) based on the requirement.

RequestDispatcher: When processing a request, a Servlet may need to forward the request to some other resource Include output of some other resource to the current one. RequestDispatcher provides methods to forward or include a resource.

… Web Server RequestDispatcher Forward 2. Processing Servlet NOTE: any response from Processing Servlet is discarded RequestDispatcher Forward 3.Forward the request to some other Servlet 2. Processing Servlet 4.Forwarded Resource 1. Send HTTP Request to Servlet Web Browser (Client) 5.Send the response to Client … Web Server

… Web Server RequestDispatcher Include Processing Servlet NOTE: any response from Processing Servlet is discarded RequestDispatcher Include Forward the request to some other Servlet Processing Servlet Forwarded Resource Send HTTP Request to Servlet Web Browser (Client) Send the response to Client … Web Server

… … … Web Server Session data Web Browser (Client) Login Servlet Get existing session Get existing session getSession(false) getSession(false) setAttribute() getAttribute() Invalidate() destroys the session getAttribute() getSession() Create new session Login Servlet Servlet1 Servlet2 Logout Servlet Web Browser (Client) … … … HTTP Web Server

JSP JSP stands for Java Server Pages. JSP is just an extension over Servlets. Advantages of JSP over Servlets are JSP Code is more compact compared to Servlets JSP is tag based, and syntactically looks similar to HTML, and has better readability. Development time of Web Applications using JSP is lesser compared with that of Servlets. Note that when .jsp is built, Servlet Java file gets generated behind the scenes. Further .class file is generated from .java Servlet file. And JVM finally executes the .class file on the Web Server, during run time.

JSP Tags JSP is Tag based, and below are most frequently used tags in JSP <%! declaration tag %> <% scriplet Tag %> <%= expression tag %> <%-- comment --%> <%@ directive tag %> Declaration Tag: All variable declarations need to be enclosed in Declaration Tag Eg. <%! int a,b; %> Scriplet Tag: All other normal code need to be enclosed in Scriplet Tag. Expression Tag: any expression gets evaluated and output is sent as response Eg. <%= (a+b) %> Comment Tag: JSP Comment are enclosed Directive Tag: used to import, session, exception handling,etc…

Below is list of Jsp implicit objects, along with the details In JSP implicit objects are the objects created by implicitly, and can be directly used in Jsp programs with out declaring them. This is basically to reduce the code written by developer. Below is list of Jsp implicit objects, along with the details request response out config application session

JSP Forward and Include <jsp:include />

JSP Life Cycle Below are the JSP Life Cycle methods, jspInit() is called only once , when first request is received by JSP page. Any one time initialization code can be added to jspInit(). _jspService() is called once for every request. This method is not written by Developer, it gets generated. jspDestroy() is called, when JSP instance gets destroyed due to any of below reasons a) When Web Server is shutdown. b) When Server is out of memory. c) When requests are not received, for certain amount of time i..e when timeout occurs. Any //jspInit() is not invoked for every request <%! public void jspInit(){ System.out.print("jspinit"); } %> public void jspDestroy(){ System.out.print("jspdestroy");

JSTL 1.Core 2.XML 3.Format 4.SQL