Introduction to Server-Side Web Development Introduction to Server-Side Web Development Session II: Introduction to Server-Side Web Development with Servlets.

Slides:



Advertisements
Similar presentations
4 Copyright © 2005, Oracle. All rights reserved. Creating the Web Tier: Servlets.
Advertisements

Servlets, JSP and JavaBeans Joshua Scotton.  Getting Started  Servlets  JSP  JavaBeans  MVC  Conclusion.
1 Servlets Based on Notes by Dave Hollinger & Ethan Cerami Also, the Online Java Tutorial by Sun.
 2002 Prentice Hall. All rights reserved. Chapter 9: Servlets Outline 9.1 Introduction 9.2 Servlet Overview and Architecture Interface Servlet and.
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.
10-Jun-15 Servlets. 2 Servers A server is a computer that responds to requests from a client Typical requests: provide a web page, upload or download.
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.
June 1, 2000 Object Oriented Programming in Java (95-707) Advanced Topics 1 Lecture 10 Object Oriented Programming in Java Advanced Topics Servlets.
1 CS6320 – Servlet Structure and Lifecycle L. Grewe.
Servlets Replace Common Gateway Interface Scripts Extend Server Functionality Modules (software components) Like applets to browsers No GUI.
Comp2513 Java Servlet Basics Daniel L. Silver, Ph.D.
13-Jul-15 Servlets. 2 Servers A server is a computer that responds to requests from a client Typical requests: provide a web page, upload or download.
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 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 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.
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.
Introduction to Server-Side Web Development Introduction to Server-Side Web Development Introduction to Server-Side Web Development with Servlets and JSP;
1 Servers A server is a computer that responds to requests from a client – Typical requests: provide a web page, upload or download a file, send .
Java support for WWW Babak Esfandiari (sources: Qusay Mahmoud, Roger Impey, textbook)
CSC 2720 Building Web Applications
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.
Servlet Lifecycle Lec 28. Servlet Life Cycle  Initialize  Service  Destroy Time.
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.
Servlets Servlets are modules that extend the functionality of a “java-enabled” web-server They normally generate HTML code and web content dynamically.
Servlets Server-Side Software. Objective What is a servlet? Where do servlets fit in? What can servlets do? Why are servlets better than CGI? Servlet.
20-Nov-15introServlets.ppt Intro to servlets. 20-Nov-15introServlets.ppt typical web page – source Hello Hello.
Java Servlet API CGI / HTTP Concepts Java Servlet API.
Servlets Included in the J2EE.
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.
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.
S ERVLETS Form Data 19-Mar-16. F ORM P ROCESSING You must have come across many situations when you need to pass some information from your browser to.
Introduction to Servlets
Servlets.
Servlet Fudamentals.
Web Programming Course
Servlet.
Chapter 26 Servlets.
Servlets.
Servlet APIs Every servlet must implement javax.servlet.Servlet interface Most servlets implement the interface by extending one of these classes javax.servlet.GenericServlet.
Servlets 7-Apr-19.
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.
CS122B: Projects in Databases and Web Applications Winter 2019
Basic servlet structure
Presentation transcript:

Introduction to Server-Side Web Development Introduction to Server-Side Web Development Session II: Introduction to Server-Side Web Development with Servlets 26 th February 2004 Bogdan L. Vrusias

Introduction to Server-Side Web Development 26 th February 2004Bogdan L. Vrusias © Session II Servlets Architecture Lifecycle Examples

Introduction to Server-Side Web Development 26 th February 2004Bogdan L. Vrusias © Servlet Architecture Overview The central abstraction in the Servlet API is the Servlet interface. All servlets implement this interface, either directly or, more commonly, by extending a class that implements it such as HttpServlet. The Servlet interface provides for methods that manage the servlet and its communications with clients. Servlet writers provide some or all of these methods when developing a servlet. When a servlet accepts a call from a client, it receives two objects: one is a ServletRequest and the other is a ServletResponse. –The ServletRequest class encapsulates the communication from the client to the server –The ServletResponse class encapsulates the communication from the servlet back to the client.

Introduction to Server-Side Web Development 26 th February 2004Bogdan L. Vrusias © ServletRequest The ServletRequest interface allows the servlet access to information such as: –the names of the parameters passed in by the client –the protocol (scheme) being used by the client –and the names of the remote host that made the request and the server that received it. It also provides the servlet with access to the input stream, ServletInputStream, through which the servlet gets data from clients that are using application protocols such as the HTTP POST and PUT methods. Subclasses of ServletRequest allow the servlet to retrieve more protocol-specific data. For example, HttpServletRequest contains methods for accessing HTTP-specific header information.

Introduction to Server-Side Web Development 26 th February 2004Bogdan L. Vrusias © ServletResponse The ServletResponse interface gives the servlet methods for replying to the client. It allows the servlet to: –set the content length and mime type of the reply –and provides an output stream, ServletOutputStream, and a Writer through which the servlet can send the reply data. Subclasses of ServletResponse give the servlet more protocol-specific capabilities. For example, HttpServletResponse contains methods that allow the servlet to manipulate HTTP-specific header information.

Introduction to Server-Side Web Development 26 th February 2004Bogdan L. Vrusias © Servlet Lifecycle I When a server loads a servlet, it runs the servlet's init() method. Even though most servlets are run in multi-threaded servers, there are no concurrency issues during servlet initialization. This is because the server calls the init method once, when it loads the servlet, and will not call it again unless it is reloading the servlet. The server can not reload a servlet until after it has removed the servlet by calling the destroy() method. After the server loads and initializes the servlet, the servlet is able to handle client requests. It processes them in its service() method. Each client's request has its call to the service method run in its own servlet thread: the method receives the client's request, and sends the client its response.

Introduction to Server-Side Web Development 26 th February 2004Bogdan L. Vrusias © Servlet Lifecycle II Servlets run until they are removed from the service, by calling the servlet's destroy() method. The method is run once; the server will not run it again until after it reloads and reinitializes the servlet. NOTE: Servlets can run multiple service methods at a time. It is important, therefore, that service methods be written in a thread-safe manner. If, for some reason, a server should not run multiple service methods concurrently, the servlet should implement the SingleThreadModel interface.

Introduction to Server-Side Web Development 26 th February 2004Bogdan L. Vrusias © Interacting with Clients I Servlet writers who are developing HTTP servlets that specialize the HttpServlet class should override the method or methods designed to handle the HTTP interactions that their servlet will handle. The candidate methods include: –doGet, for handling GET, conditional GET and HEAD requests –doPost, for handling POST requests –doPut, for handling PUT requests –doDelete, for handling DELETE requests

Introduction to Server-Side Web Development 26 th February 2004Bogdan L. Vrusias © Interacting with Clients II An HttpServletRequest object provides access to HTTP header data, such as any cookies found in the request and the HTTP method with which the request was made. It allows the you to obtain the arguments that the client sent as part of the request: –For any HTTP method, you can use the getParameterValues() method, which will return the values of a named parameter. The method getParameterNames() provides the names of the parameters. For responding to the client, an HttpServletResponse object provides ways of returning the response data to the user. You can use the writer returned by the getWriter() method to return text data to the user.

Introduction to Server-Side Web Development 26 th February 2004Bogdan L. Vrusias © Servlet Example I import javax.servlet.*; import java.io.*; import java.util.*; public class SimpleServlet extends GenericServlet { public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); printWriter pw = response.getWriter(); pw.println(" "); for(int counter = 1; counter <= 10; counter++) { pw.println(counter + " Mississippi, "); } pw.println(" Ready or not, here I come! "); pw.println(" "); pw.close(); }

Introduction to Server-Side Web Development 26 th February 2004Bogdan L. Vrusias © Reading Form Data Example - GET When submitted, the browser would execute the following URL: x&anotherKey=y Alternatively

Introduction to Server-Side Web Development 26 th February 2004Bogdan L. Vrusias © Reading Form Data Example - POST The HTML Please Enter your name

Introduction to Server-Side Web Development 26 th February 2004Bogdan L. Vrusias © Reading Form Data Example - POST The Servlet... public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { String name=req.getParameterValues("yourname")[0]; String reply=" \n"+ " My Name Servlet Response \n"+ " \n"+ "Hello "+name+"\n"+ " \n \n "; res.setContentType("text/html"); PrintWriter out=res.getWriter(); out.println(reply); out.close(); }...

Introduction to Server-Side Web Development 26 th February 2004Bogdan L. Vrusias © Demo Some examples will now be demonstrated using the Tomcat server…

Introduction to Server-Side Web Development 26 th February 2004Bogdan L. Vrusias © Session: Closing Questions??? Remarks??? Comments!!! Evaluation!