Chapter 4 Request and Response. Servlets are controlled by the container.

Slides:



Advertisements
Similar presentations
Servlets & JSPs - Sharad Ballepu.
Advertisements

4 Copyright © 2005, Oracle. All rights reserved. Creating the Web Tier: Servlets.
Servlets, JSP and JavaBeans Joshua Scotton.  Getting Started  Servlets  JSP  JavaBeans  MVC  Conclusion.
 2002 Prentice Hall. All rights reserved. Chapter 9: Servlets Outline 9.1 Introduction 9.2 Servlet Overview and Architecture Interface Servlet and.
COMP 321 Week 8. Overview MVC Example Servlet Lifecycle Servlet Mechanics MVC Lab 5-2 Solution Lab 8-1 Introduction.
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.
MC365 Application Servers: Servlets. Today We Will Cover: What a servlet is The HTTPServlet and some of its more important methods How to configure the.
Servlets and a little bit of Web Services Russell Beale.
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.
Object-Oriented Enterprise Application Development Introduction to Servlets.
Comp2513 Java Servlet Basics Daniel L. Silver, Ph.D.
Servlet details Russell Beale. Servlet lifecycle The servlet container creates only one instance of each servlet Each use request handled with a separate.
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 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 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.
Servlets. Our Project 3-tier application Develop our own multi-threaded server Socket level communication.
SE-2840 Dr. Mark L. Hornick1 Java Servlet-based web apps Servlet Architecture.
Java Server Pages B.Ramamurthy. Topics for Discussion 8/20/20152 Inheritance and Polymorphism Develop an example for inheritance and polymorphism JSP.
Web server and web browser It’s a take and give policy in between client and server through HTTP(Hyper Text Transport Protocol) Server takes a request.
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.
AN OVERVIEW OF SERVLET TECHNOLOGY SERVER SETUP AND CONFIGURATION WEB APPLICATION STRUCTURE BASIC SERVLET EXAMPLE Java Servlets - Compiled By Nitin Pai.
Java support for WWW Babak Esfandiari (sources: Qusay Mahmoud, Roger Impey, textbook)
CSC 2720 Building Web Applications
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.
COMP 321 Week 7. Overview HTML and HTTP Basics Dynamic Web Content ServletsMVC Tomcat in Eclipse Demonstration Lab 7-1 Introduction.
Chapter 5 Being a Web App. Very few servlet or JSP stands alone Many times in our application, different servlets or JSPs need to share information 
CMPUT 391 – Database Management Systems Department of Computing Science University of Alberta CMPUT 391 Database Management Systems Web based Applications,
Copyright, 1996 © Dale Carnegie & Associates, Inc. Presented by Hsiuling Hsieh Christine Liu.
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.
Java Servlets Lec 27. Creating a Simple Web Application in Tomcat.
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.
@2008 Huynh Ngoc Tin Chapter #2 JAVA SERVLET PRGRAMMING.
Introduction to Server-Side Web Development Introduction to Server-Side Web Development Session II: Introduction to Server-Side Web Development with Servlets.
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.
Introduction to Servlets Allen Day. Notes This is a training NOT a presentation Please ask questions Prerequisites.
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.
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,
April 20023CSG11 Electronic Commerce Java (2) John Wordsworth Department of Computer Science The University of Reading Room 129,
1 Lecture 8 George Koutsogiannakis/Summer 2011 CS441 CURRENT TOPICS IN PROGRAMMING LANGUAGES.
CS122B: Projects in Databases and Web Applications Spring 2017
Introduction to Servlets
CS122B: Projects in Databases and Web Applications Winter 2017
Servlets.
Servlet Fudamentals.
Java Servlets By: Tejashri Udavant..
CSE 403: Servlet Technology
Pre-assessment Questions
Servlet API and Lifecycle
Chapter 26 Servlets.
CS122B: Projects in Databases and Web Applications Winter 2018
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
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:

Chapter 4 Request and Response

Servlets are controlled by the container

Servlet interface Servlet programming is made possible through the classes and interfaces of two packages: javax.servlet and javax.servlet.http. Of these classes and interfaces, the javax.servlet.servlet interface is the most important interface. –All servlets must implement this interface or extend a class that does.

Servlet interface The Servlet interface has five methods, whose signatures are as follows: –public void init(ServletConfig config) throws ServletException –public void service(ServletRequest request, ServletResponse response) throws ServletException, java.io.IOException –public void destroy() –public ServletConfig getServletConfig() –public java.lang.String getServletInfo()

Servlet lifecycle The servlet lifecycle is simple; there is only one main state – initialized If the servlet isn’t initialized, then it is either –Being initialized –Being destroyed –Or simply does not exist

Servlet lifecycle

The three big lifecycle moments –init() –service() –doGet() and/or doPost() doGet() and doPost() are invoked by service()

Servlet lifecycle init() –When it’s called The container calls init() on the servlet instance after the servlet instance is created but before the servlet can service any client request –What it’s for Gives you a chance to initialize your servlet before handling any client requests –Do you override it? Possibly –E.g., If you need to initialize a database connection, you can do it here

Servlet lifecycle service() –When it is called When the first client request come in, the container starts a new thread or allocate a thread from the pool, and causes the servlet’s service() method to be invoked –Wht it is for This method looks at the request, determines the HTTP method (GET, POST, etc.) and invokes the matching doGet(), do Post(), etc. on the servlet –Do you override it? No

Servlet lifecycle doGet(), doPost() –When it is called The service() method invokes doGet() or doPost() based on the HTTP method (GET, POST, etc.) from the request –What is it for You as a servlet developer write your own code in doGet()/doPost() –Do you override it Always at least one of them

Servlet lifecycle In a nutshell, a fully functional servlet container does the following for each HTTP request for a servlet: –When the container starts up (or first client use), it loads the servlet class, instantiates servlet, and calls its init() method (once only) –For each request, construct an instance of javax.servlet.ServletRequest and an instance of javax.servlet.ServletResponse –Invoke the servlet's service() method, passing the ServletRequest and ServletResponse objects. doGet() and/or doPost() are then called by service() method –When the servlet class is shut down, call the servlet's destroy() method and unload the servlet class.

HTTP request Each HTTP request runs in a separate thread –The container runs multiple threads to process multiple requests to a single servlet –Every client request generates a new pair of request and response objects

HTTP request

Question In the picture above you show two different clients, each with its own thread. What happens if the same client makes multiple requests? Is it one thread per client or one thread per request? Answer: –One thread per request. The Container doesn’t care who makes the request-every incoming request means a new thread.

Question When is the servlet loaded into the container? Answer: –This happens either on Container startup or first client use

Servlet Initialization

Servlet does not exist constructorInit() Constructor of the servlet only makes an object, not a servlet. init() makes the object a servlet and ready to service client request.

Servlet Initialization Remember do not put anything in the servlet’s constructor –It is too early to do servlet-specific things in constructor –You can put that in init()

Servlet Initialization 1. A ServletConfig Object 2. A ServletContext Object

ServletConfig object One ServletConfig object per servlet Use it to pass deploy-time information to the servlet Use it to access the ServletContext Parameters are configured in the Deployment Descriptor

ServletContext One Servlet Context per web app Use it to access web app parameters (also configured in the Deployment Descriptor) Use it as a kind of application bulletin- board –Where you can put up messages that other parts of the application can access Use it to get server info –Such as the name and version of the Container, and the version of the API that is supported

HttpServletRequest interface extends ServletRequest interface

HttpServletResponse interface extends ServletResponse interface

Questions Can interface extend interface? Can interface implement interface? What does it mean if some class implements an interface?

Request and reponse Keep in mind that HttpServletRequest and HttpServletResponse are interfaces, not classes Also there are no class in J2EE API that implements these two interfaces –It is the Container that implements these two interfaces –The classes aren’t in the API because they’re left to the vendor to implement

Request and reponse The client’s request always includes a specific HTTP method. –If the HTTP request method is a GET, the service() method calls doGet() –If the HTTP request method is a POST, the service() method calls doPost()

Request and reponse There are other HTTP 1.1 methods besides GET and POST. –They are HEAD, TRACE, OPTIONS, PUT, DELETE, and CONNECT –However, you probably won’t care about these methods

Difference between GET and POST POST has a body, GET does not Both GET and POST can send parameters, but with GET, the parameter data is limited to what you can stuff into the Request line

No, it is not just about the size

Difference between GET and POST When you use GET, the parameter data shows up in the browser’s input bar, right after actual URL (and separated with a “?”) Parameter Name Value ubbget_topic f7 t021696

Difference between GET and POST Security is also an issue –You might not want the parameters to be visible Another issue is whether you want the end-users to be able to bookmark the request page –GET requests can be bookmarked –POST requests cannot be bookmarked

What determines whether the browser sends a GET or POST request

By default, HTTP GET request will be sent to the server Parameters will show up in the browser’s input bar

What determines whether the browser sends a GET or POST request Questions: What will happen if you use HTTP GET (or does not specify request method) in the form and do not have a doGet() in your servlet?

Difference between GET and POST If you want to support both GET and POST no matter the form sends a GET or POST request, you can do this: That is, put the logic in doGet() and have doPost() method delegate to the doGet() method if necessary

Sending parameters each of which has one value

Sending parameters each of which has multiple values

Request redirect and dispatch You can choose to have another servlet or JSP to handle the response for your request –You can either redirect request to a completely different URL –Or you can dispatch the request to some other component in your web application

Request redirect Request redirect makes the browser do the work A redirect lets servlet off the hook completely. After deciding that it cannot do the work, the servlet simply calls the sendRedirect() method

Request redirect

Request dispatch Request dispatch is different from request redirect –Request dispatch does the work on the server side Redirect makes the client do the work while request dispatch makes something else on the server do the work

Request dispatch