K. K. Wagh Polytechnic, Nashik

Slides:



Advertisements
Similar presentations
Internet i jego zastosowania 1 J2EE Servlets. Internet i jego zastosowania 2 Agenda Overview Servlet Interface Servlet Context Request Response Sample.
Advertisements

Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 10 Servlets and Java Server Pages.
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.
J.Sant Servlets Joseph Sant Sheridan Institute of Technology.
Objectives Ch. D - 1 At the end of this chapter students will: Know the general architecture and purpose of servlets Understand how to create a basic servlet.
Servlets and a little bit of Web Services Russell Beale.
An introduction to Java Servlet Programming
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 34 Servlets.
1 CS6320 – Servlet Structure and Lifecycle L. Grewe.
Servlets CS-328 Dick Steflik. What is a servlet A Java application run on a thread of the webserver in response to an HTTP GET or POST request. The servlet.
Java Server and Servlet CS616 Team 9 Kim Doyle, Susan Kroha, Arunima Palchowdhury, Wei Xu.
Definition Servlet: Servlet is a java class which extends the functionality of web server by dynamically generating web pages. Web server: It is a server.
Session-02.
Servlets Written by Dr. Yaron Kanza, Edited by permission from author by Liron Blecher.
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.
Servlets. Our Project 3-tier application Develop our own multi-threaded server Socket level communication.
Java Servlets.
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.
1 Servlet How can a HTML page, displayed using a browser, cause a program on a server to be executed?
Java Servelets. What Is a Servlet? A servlet is a Java programming language class used to extend the capabilities of servers that host applications accessed.
Servlets Pranav Maydeo. What is a Servlet ? Servlets are modules of Java code that run in a server application to answer client requests. Servlets are.
Servlets. - Java technology for Common Gateway Interface (CGI) programming. - It is a Java class that dynamically extends the function of a web server.
Objectives Java Servlet Web Components
Java support for WWW Babak Esfandiari (sources: Qusay Mahmoud, Roger Impey, textbook)
CSC 2720 Building Web Applications
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.
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.
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.
Servlets O. De Pertat. Servlets Overview Generic Server Business logic API Java Syntax: classes extending the javax.servlet.Servlet interface or any sub-class.
Java Servlet API CGI / HTTP Concepts Java Servlet API.
Introduction to Server-Side Web Development Introduction to Server-Side Web Development Session II: Introduction to Server-Side Web Development with Servlets.
L.MARIA MICHAEL VISUWASAM UNIT-4
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.
Servlets.
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.
JS (Java Servlets). Internet evolution [1] The internet Internet started of as a static content dispersal and delivery mechanism, where files residing.
ORIGINAL BY WENDY LIU CURRENT API: Java Servlet and JSP 1.
Advanced Java Session 6 New York University School of Continuing and Professional Studies.
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.
Java Servlets Java Server Pages (JSP)
® IBM Software Group © 2007 IBM Corporation Servlet API (Part II)
Advanced Java Session 6 New York University School of Continuing and Professional Studies.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 21 Java Servlets Wed. 11/22/00 based on material.
HTTP protocol Java Servlets. HTTP protocol Web system communicates with end-user via HTTP protocol HTTP protocol methods: GET, POST, HEAD, PUT, OPTIONS,
SERVLET THETOPPERSWAY.COM
How CGI and Java Servlets are Run By David Stein 14 November 2006.
Chapter 4 Request and Response. Servlets are controlled by the container.
8-Mar-16 More About Servlets Session Tracking. Persistent information A server site typically needs to maintain two kinds of persistent (remembered) information:
Distributed Web Systems Java Servlets Lecturer Department University.
Introduction to Servlets
Servlets.
Servlets.
Servlet Fudamentals.
Java Servlets By: Tejashri Udavant..
Java Servlets.
Pre-assessment Questions
Server Side Programming: Java Servlets
Servlet.
Chapter 26 Servlets.
Servlets & JSP.
Servlet APIs Every servlet must implement javax.servlet.Servlet interface Most servlets implement the interface by extending one of these classes javax.servlet.GenericServlet.
CS122B: Projects in Databases and Web Applications Winter 2019
Presentation transcript:

K. K. Wagh Polytechnic, Nashik Advance Java Programming Ch. No Name Marks 01 AWT 24 02 Networking 18 03 JDBC 20 04 Swing 05 Servlet Mr. Suyog. S. Dhoot Lecturer (IF Dept) K. K. Wagh Polytechnic, Nashik

Request-response model. Introduction – request-response model Request-response model. HTTP Request request Server <html> <head> <body> … <html> <head> <body> … Client response HTTP HTML

HTTP Request HTTP Response Introduction – what is a request and response HTTP Request HTTP Response Key elements of a “request” stream: HTTP method (action to be performed). The page to access (a URL). Form parameters. Key elements of a “response” stream: A status code (for whether the request was successful). Content-type (text, picture, html, etc…). The content ( the actual content).

HTTP Methods GET HEAD POST OPTIONS PUT DELETE TRACE

Where does Servlet come into the picture? Introduction – What is a Servlet Where does Servlet come into the picture? I can serve only static HTML pages Web Server Application Not a problem. I can handle dynamic requests. Helper Application Web Server machine “The Helper Application is nothing but a SERVLET”

Servlet Small Program execute on server side on web connection Runs on server side Not depend on browser compability It reads Explicit and implicit data and generate result Useful in e -commerce application

CGI Scripts CGI stands for “Common Gateway Interface” server Client sends a request to server Server starts a CGI script client server Script computes a result for server and quits script script Server returns response to client Another client sends a request Server starts the CGI script again Etc.

Servlets A servlet is like an applet, but on the server side server Client sends a request to server Server starts a servlet client server Servlet computes a result for server and does not quit servlet Server returns response to client Another client sends a request Server calls the servlet again Etc.

Servlet Alternatives Cold Fusion PHP: Open Source with file upload and session management ASP Windows IIS ASP.net JSP: Extension of Servlet

What is a Web Container? Servlet Architecture -Web Container Servlet request GET. ….. GET. ….. GET. ….. Web Server Web Container Servlet Client

How does the Container handle a request? Servlet Architecture – Web Container How does the Container handle a request? Servlet request Web Container Http request response Thread Web Server <Html> <Body> ……. </Body> </Html> response Service() Client doGet() Apache: Server Tomcat: Servlet Engine

Benefits Efficiency: More efficient – uses lightweight java threads Persistency: Servlets remain in memory. They can maintain state between requests. Portability: Since servlets are written in Java, they are platform independent. Robustness: Error handling, Garbage collector to prevent problems with memory leaks. Security: Security provided by the server as well as the Java Security Manager. Widespread acceptance: Java is a widely accepted technology. This means that numerous vendors work on Java-based technologies

Disadvantages High Maintenance High Complexity Working Disadvantages High Maintenance High Complexity Lower network efficiency More parts to be configure.

Servlet API Contains two package Javax.servlet.* Javax.servlet.http.* GenericServlet HTTPServlet 14 IBM 14

Javax.servlet.* Interfaces Servlet ServletConfig ServletContext: servlet access information about their environment ServletRequest ServletResponse SingleThreadModel 15 IBM 15

Javax.servlet.* Classes GenericServlet :implants Servlet ServletInputSteam ServletOutputStream: ServletRequest Exception ServletException UnavailableException 16 IBM 16

Life Cylce of Servlet 17 IBM

Life Cylce of Servlet Init() 1) It request for servlet 2) Loading of servlet 3) Resources initialized 4) Once called 5) Parameter: ServletConfig 18 IBM

Life Cylce of Servlet Service() 1) Handle request of client 2) only single instance created 3) Process client request and give response 4) Parameters: ServletRequest, ServletResponse 5) Only work with generic servlet 19 IBM

Life Cylce of Servlet Destroy() This method signifies the end of a servlet’s life. The resources allocated during init( ) are released. Save persistent information that will be used the next time the servlet is loaded. The servlet engine unloads the servlet. Calling destroy( ) yourself will not acutally unload the servlet. Only theservlet engine can do this. 20 IBM

First example import java.io.*; import javax.servlet.*; public class Pract15 extends GenericServlet { public void service(ServletRequest r, ServletResponse res) throws IOException, ServletException res.setContentType("text/html"); PrintWriter p = res.getWriter(); p.println("<HTML>"); p.println("<HEAD> Hello </HEAD>"); p.println("</HTML>"); } 21 IBM

ServletConfig getServletContext() getInitParameter(String name) getServletName() getInitParameterNames() - Enumeration 22 IBM 22

ServletRequest getContentLength() getContentType() getParamenter(String name) getParameterNames() getParameterValues ( String name) getProtocol() getRemoteHost() getRemoteAddr() 23 IBM

ServletResponse getCharacterEncoding() setContentType(string type) setContentLength(int length) 24 IBM

HttpServlet Interface HttpServletRequest HttpServletResponse HttpSession : Allows session data to be read or written HttpSessionBindingListener: Informs Object that it bound or unbound from session 25 IBM

HttpServlet Classes HttpServlet Cookie HttpSessionEvent HttpSessionBindingEvent 26 IBM

HttpServlet Method doGet doPost doTrace doOptions doDelete doPut doHead

HttpServletRequset Method getHeader (String field) getHeaderNames() getMethod() getPathInfo() getQueryString()

HttpServletResponse Method containsHeader (String Field) sendError (int c) sendError (int c,String s) sendRedirect (String url) setHeader (String Field, String Value)

Error Constant 202 SC_ACCEPTED 400 SC_BAD_REQUEST 403 SC_FORBIDDEN 200 SC_OK 404 SC_NOT_FOUND

Session Management Session: how long Particular User alive in the application Session Expires: Server Stopped Client Logout Session Management: Keeping Track of User Activities throughout application

Need of Session Management HTTP is stateless Protocol Separate Connection to web server when cient request for web page Server not maintain information of client E.g. Online Shopping

Techniques for session Tracking User Authentication URL Rewriting Hidden Form Field Cookie

User Authentication Servlet stores client username and password Easy to Implement and access site from different machine Client has to register his account

URL Rewriting Adding extra information along with client request Extra information: Session ID More burden on server E.g. www.msbte.com?session id=252

HiddenFormField Fields are added to html form Fields are not visible in client browser Fields info pass to server that act as visible field Not for static document and e-mail document

Cookie Small amount of information Sent from server that store in client Uniquely identify client Cookie send along with client request Present in javax.servlet.http.* package

Creating Cookie Cookie c1=new Cookie (String name, String value) Name and Value is mandatory parameter Neither the name nor the value should contain white space or any of the following characters: [ ] ( ) = , " / ? @ : ; Cookie may be temporary or persistent browsers generally only accept 20 cookies per site and 300 cookies total and since each cookie can be limited to 4 kilobytes

Cookie Attribute Attribute Type Mandatory? Name String Yes Value MaxAge int No Secure Boolean Comment Path Version

Cookie Methods setX() getX() X: Cookie Attribute Name

Cookie operations Send Cookie by server response.addCookie (Cookie c1) Retrieve Cookie request.getCookies() Return array of cookie

Sample code PrintWriter out= response.getWriter() Cookie c[]= request.getCookies() for( int i=0; i<c.length; i++) { Cookie c1=c[i]; out.println (“”+ c1. getName()); out.println (“”+ c1. getValue()); }

Servlet Filter A filter is an object that is invoked at the preprocessing and postprocessing of a request. It is mainly used to perform filtering tasks such as conversion, logging, compression, encryption and decryption, input validation etc. The servlet filter is pluggable, i.e. its entry is defined in the web.xml 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. The filters execute in the order that they are declared in the deployment descriptor. A filter is simply a Java class that implements the javax.servlet.Filter interface

javax.servlet.Filter methods

Servlet Chaining Servlet chaining is a technique in which two or more servlets can cooperate in servicing a single request.  . In servlet chaining, one servlet's output is piped to the next servlet's input. This process continues until the last servlet is reached. Its output is then sent back to the client. 

Servlet Chaining The javax.servlet.RequestDispatcher interface comes with only two methods of include() and forward() RequestDispatcher is used whenever the Programmer would like dispatch the request to another resource (like HTML. Servlet, JSP etc.) on the server .

Servlet Chaining