Introduction to Servlets

Slides:



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

 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.
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.
Comp2513 Java Servlet Basics Daniel L. Silver, Ph.D.
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.
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 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.
1 Chapter 2 The Web Tier  Web Applications and Web ContainersWeb Applications and Web Containers  Dynamic Content CreationDynamic Content Creation 
Objectives Java Servlet Web Components
Java support for WWW Babak Esfandiari (sources: Qusay Mahmoud, Roger Impey, textbook)
CSC 2720 Building Web Applications
111 Java Servlets Dynamic Web Pages (Program Files) Servlets versus Java Server Pages Implementing Servlets Example: F15 Warranty Registration Tomcat Configuration.
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 Servlets & Java Server Pages Lecture July 2013.
Java Servlets Lec 27. Creating a Simple Web Application in Tomcat.
20-Nov-15introServlets.ppt Intro to servlets. 20-Nov-15introServlets.ppt typical web page – source Hello Hello.
Server-side Programming The combination of –HTML –JavaScript –DOM is sometimes referred to as Dynamic HTML (DHTML) Web pages that include scripting are.
Java Servlet API CGI / HTTP Concepts Java Servlet API.
@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.
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)
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,
How CGI and Java Servlets are Run By David Stein 14 November 2006.
Chapter 4 Request and Response. Servlets are controlled by the container.
1 Lecture 8 George Koutsogiannakis/Summer 2011 CS441 CURRENT TOPICS IN PROGRAMMING LANGUAGES.
Java Servlets References: Karen Anewalt, Mary Washington College.
CS122B: Projects in Databases and Web Applications Spring 2017
CS122B: Projects in Databases and Web Applications Winter 2017
Servlets.
Servlet Fudamentals.
Java Servlets By: Tejashri Udavant..
CSE 403: Servlet Technology
Net-centric Computing
Course Outcomes of Advanced Java Programming AJP (17625, C603)
HTTP Servlet Overview Servlets are modules that extend request/response-oriented servers, such as Java-enabled web servers. For example, a servlet might.
Servlet.
Chapter 26 Servlets.
Servlets and JSP 20-Nov-18 servletsJSP.ppt.
CS122B: Projects in Databases and Web Applications Winter 2018
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.
Java Servlets and JSP.
Servlets.
Servlets 7-Apr-19.
J2EE Lecture 1:Servlet and JSP
Servlets Servlets are modules that extend the functionality of a “java-enabled” web-server They normally generate HTML code and web content dynamically.
Java Servlets Servlet Overview Servlets and HTML Forms Servlet Basics
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
Java Chapter 7 (Estifanos Tilahun Mihret--Tech with Estif)
Presentation transcript:

Introduction to Servlets

Topics Web Applications and the Java Server. HTTP protocol. Servlets

Web Applications Two Types of Web Applications: Presentation oriented: Interactive web applications with dynamic content usually provided with DHTML, XML, servlets or java server pages. Service oriented: Web applications that utilize presentation oriented applications as clients. They consist of web services, SOAP protocol services, RMI etc.

Web Applications For example an Aplication server or an RMI server that processes Queries to the databse server. It receives the results of the query formats it and passes it over to the presentation oriented application so that it can be sent to the requestor (client of the presentation application)

Web Applications Web Components: Servlets, JSP (Java Server Pages). Web Components are supported by a web container. For instance: Tomcat has a servlet engine and a JSP engine. Those engines are the web containers.

Web Applications Web Containers provide to web components: Activation Initialization Configuration Translation from http to a servlet request object and Translation from a servlet response object to http.

Web Applications Web Components can communicate directly with a database (via JDBC) or They can communicate with a database via Java Beans components or They can communicate with other service oriented applications such as a RMI server, or web services, or an application server.

Web Applications Database Database Client RMI Server Web Components HTTPServletRequest Object Database HTTP Request Containers Web Components Client HTTPServletResponse Object HTTP Response Java Bean Components RMI Server Web Services Database

HTTP Protocol Request/ Response packets. Client initiates a request via a TCP connection to a port (8080 or 80). Server sends a response packet to client Protocol provides methods such as: GET POST PUT DELETE HEAD And others

HTTP Protocol If GET is used by client then: Data can be part of the URL http://www.cs.iit.edu/index.html?firtsname=“fm”&lastname=“ln” Data is not cached by the browser application. Data is URL encoded i.e. spaces are converted to + signs, nonalphanumeric characters are converted to % signs followed by the two hex digits representing the character etc. Use it when the data is small.

HTTP protocol If client uses POST: The data is part of the body of the HTTP packet. Data is cached. Use it if data is larger and the interaction with the server changes the state of the resource in the server.

HTTP Protocol Error messages can be returned by the server in the response packet. Error messages are coded: 400 means bad request. 401 means unauthorized request. 500 Internal server error. Etc.

Servlets A servlet is a java program that executes on the server side. It extends the capabilities of a server. Two types of servlets: Generic type HTTP type Note: nevertheless other protocols can be supported such as FTP, POP, SMTP We will concentrate on HTTP type.

Servlets HTTP type servlets Extend the web server by executing requests received in HTTP (from a client) protocol packets and by returning responses that are translated back to HTTP protocol packets.

Servlets Clients to Servlets Client side programs that sent requests can be: Html files (forms). Applets. JavaScript. XML. Java application programs. Active Server pages (ASP) programs. Other.

Servlets Execution environment: Web Server needs the servlet engine (container). The interface between the servlet engine and the servlet program is defined by the Servlet API.

Servlets The servlet container is responsible for: Creating an instance of the servlet program. Calling the servlet’ s init() method to initialize the servlet. Configures the servlet according to preset properties. Calling the servlet’ s service method. Creating the stream to send to the servlet a request object that encapsulates an y data the client sent as part of the request. Creating the stream to receive the response object from the servlet that encapsulates the data generated by the servlet as a result of the request. Can handle multiple service calls that be made simultineously (multiple requests arriving at the same time). Destroys the servlet by calling destroy() method.

Servlets Servlet applications provide: Processing of a Get or Post type of a request via individual methods that need to be overridden. The servlet engine reads the request type off the arriving HTTP packets, calls the service method of the servlet, the service method will call either a doGet or doPost servlet method. Handle many requests at the same time. Provide session capability with a client so that multiple request/response pairs can be serviced without loosingtrack of the particular client. Can communicate with other servlets forming a chain.

Servlets Can generate a response in Html tags. The servelt engine will extract the html code from the java code and form send it to the client. This facilitates the generation of dynamically formed responses based on the request. Can execute JDBC code to connect to a database and retrieve information. The response object will then encapsulate the data. Can act as clients to a Remote Method Invocation Servers in a distributed objects network. Can communicate with Java Beans.

Servlets Servlet API javax.servlet javax.servlet.http Servlet interface Implemented by abstract class Implemented by abstract class GenericServlet HttpServlet

Servlets The abstract classes provide default implementation for methods: void init(ServletConfig config) ServletConfig getServletConfig() void service(ServletRequest requ, ServletResponse resp) String getServletInfo() void destroy() In addition, the HttpServlet class has methods: doGet (HTTPServletRequest requ, HttpServletResponse resp) doPost (HTTPServletRequest requ, HttpServletResponse resp) These methods have to be overridden by the servlet class that the developer provides.

Servlets Example: Client receives an HTML form. The form in its action tag calls a servlet. The servlet creates html code and sends back to the client for display by the browser.

Servlets Example-File helloworld,html (simplified) <html> <head> <title>Untitled Document</title> </head> <body bgcolor="#FFFFFF"> <p><a href="servlet/HelloWorldExample"></p> </body> </html>

Servlets Example – file HelloWorld.java import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class HelloWorld extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println("<html>"); out.println("<head>"); out.println("<title>Hello World!</title>"); out.println("</head>"); out.println("<body>"); out.println("<h1>Hello World!</h1>"); out.println("</body>"); out.println("</html>"); } }

Servlets Example – file HelloWorld.java The Browser receives the html code: <html> <head> <title>Hello World!</title> </head> <body> <h1>Hello World!</h1> </body> </html>

Servlets-Another example <html> <body> <Form action=http://localhost:8080/ExampleSite/TechSupport” method=“POST”> <table><tr><td> <Input types=“Text” Name=“firstName”></input></td></tr></table> </form> </body> </html>

Servlets-Another example TechSupport.java file import javax.servlet.*; import javax.servlet.http.* import java.io.*; import java.util.*; public class TechSupport extends HttpServlet { protected void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException { res.setContextType(“text/html”); String myparam=req.getParameter(“firstName”); PrintWriter out = res.getWriter(); out.println(“<HTML>”); out.println(“<HEAD>”); out.println(“<BODY>”);

Servlets-Another example if(myparam.equals(“MyName”)) { out.println(“<p”>); out.println(“You can now login”); out.println(“</p>”); } else { out.println(“<p”>); out.println(“I don’t recognize you”); out.println(“</BODY>”); out.println(“</HEAD>”); out.println(“<HTML>); out.close();