Objectives Java Servlet Web Components

Slides:



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

4 Copyright © 2005, Oracle. All rights reserved. Creating the Web Tier: Servlets.
 2002 Prentice Hall. All rights reserved. Chapter 9: Servlets Outline 9.1 Introduction 9.2 Servlet Overview and Architecture Interface Servlet and.
Introduction to Servlets Based on: Hall, Brown, Core Servlets and JavaServer Pages.
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.
WEB1P servintro1 Introduction to servlets and JSP Dr Jim Briggs.
An introduction to Java Servlet Programming
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 34 Servlets.
Core Servlets Chapter 3 Link for Core Servlets code: om/archive/ om/archive/
Introduction to Servlet & JSP
Comp2513 Java Servlet Basics Daniel L. Silver, Ph.D.
Java Server and Servlet CS616 Team 9 Kim Doyle, Susan Kroha, Arunima Palchowdhury, Wei Xu.
Chapter 4 Servlets Concept of Servlets (What, Why, and How) Servlet API Third-party tools to run servlets Examples of Using Servlets HTML tag with GET.
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.
Michael Brockway Application Integration Servlets l Introduction & Overview l HTTP Servlets l HTTP get Requests l HTTP post Requests l Multi-tier Applications.
Java Servlets and JSP.
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.
INTRODUCTION TO WEB DATABASE PROGRAMMING
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.
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)
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.
111 Java Servlets Dynamic Web Pages (Program Files) Servlets versus Java Server Pages Implementing Servlets Example: F15 Warranty Registration Tomcat Configuration.
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,
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.
1 MSCS 237 Overview of web technologies (A specific type of distributed systems)
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.
@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.
L.MARIA MICHAEL VISUWASAM UNIT-4
Java Servlets and Java Server Pages Norman White Stern School of Business.
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.
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.
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.
Java Servlets and 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,
How CGI and Java Servlets are Run By David Stein 14 November 2006.
Java Programming: Advanced Topics 1 Building Web Applications Chapter 13.
Chapter 4 Request and Response. Servlets are controlled by the container.
1 Lecture 8 George Koutsogiannakis/Summer 2011 CS441 CURRENT TOPICS IN PROGRAMMING LANGUAGES.
1 Web Programming with Servlets & JSPs WEB APPLICATIONS – AN OVERVIEW.
Introduction to Servlets
Servlets.
Servlet Fudamentals.
Java Servlets By: Tejashri Udavant..
CSE 403: Servlet Technology
Chapter 26 Servlets.
Objectives In this lesson you will learn about: Need for servlets
J2EE Lecture 1:Servlet and JSP
Java Chapter 7 (Estifanos Tilahun Mihret--Tech with Estif)
Presentation transcript:

Objectives Java Servlet Web Components Support Environments for Java Servlets Servlets with CGI Compared to Java Applets Functionality of Java Servlets Java Servlet API Java Servlet Debugging

Introduction to Java Servlets Java Servlets technology provides an HTTP-based request and response paradigm on web servers. Java Servlets can handle generic service requests and respond to the client’s requests. Java Servlets are used in embedded systems, wireless communication, and any other generic request/response application. The Common Gateway Interface (CGI) was a popular technology used to generate dynamic HTTP web contents in the mid-1990s.

CGI and Java Servlets Technology

Java Servlets Advantages Efficiency: Reduction of the time need for creating new processes and initialization and reduction of memory requirements as well. Convenience: All needed functionality is provided by the Servlets API. Portability: Cross-platform, Write Once Run Anywhere (WORA) code. Security: Built-in security layers. Open source: Free Servlet development kits available for download. Functionality: Session tracking, data sharing, JDBC database connections, etc.

Java Applet and Java Servlet

Java Applet and Java Servlet, contd.

Introduction to Java Servlets, contd. A Servlet component can delegate the requests to its back-end tier such as a database management system, RMI, EAI, or other Enterprise Information System (EIS). A Servlet is deployed as a middle tier just like other web components such as JSP components. The Servlet components are building block components, which always work together with other components such as JSP components, JavaBean components, Enterprise Java Bean (EJB) components, and web service components. A Servlet component is also a distributed component, which can provide services to remote clients and also access remote resources.

Support Environments for Java Servlets A Java Servlet application is supported by its Servlet container. The Apache Tomcat web server is the official reference implementation of Servlet containers, supporting Servlets and JSP.

Web Server Configuration An XML format file called server.xml is used to control and configure the behavior and setting of the Tomcat web server. This file is located in the conf subdirectory of the Tomcat installation directory.

Web Server Configuration, contd. 1. Reset the server port number where the Servlet class or other web component will listen for requests. 2. Turn on Servlet reloading so that you don’t need to reload the recompiled Servlet.

Java Servlets Basics The Java Servlet is a server-side web component that takes a HTTP request from a client, handles it, talks to a database, talks to a JavaBean component, and responds with a HTTP response or dispatches the request to other Servlets or JSP components. Servlets can dynamically produce text-based HTML markup contents and binary contents as well contents based on the client’s request.

Java Servlet Architecture A Java Servlet is a typical Java class that extends the abstract class HttpServlet. The HttpServlet class extends another abstract class called GenericServlet. The GenericServlet class implements three interfaces: javax.servlet.Servlet, javax.servlet.ServletConfig, and java.io.Serializable.

Java Servlet Class Hierarchy

Servlet Lifecycle A Servlet has a lifecycle just like a Java applet. The lifecycle is managed by the Servlet container. There are three methods in the Servlet interface, which each Servlet class must implement. They are init(), service(), and destroy().

The Lifecycle of a Servlet

Processing of HTTP Requests and Responses The prototypes of doGet() and doPost() void doGet( HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException; void doPost( HttpServletRequest request, HttpServletResponse response )

Processing of HTTP Requests and Responses, contd. HttpServletRequest and HttpServletResponse are the two interfaces that provide the Servlet with full access to all information from the request and the response sent back to the client. When the doGet() or doPost() is called, the Servlet container passes in the HttpServletRequest and HttpServletResponse objects.

import java.io.*; import java.net.*; import javax.servlet.*; import javax.servlet.http.*; import java.text.DecimalFormat; public class TestServlet extends HttpServlet { public void doGet(HttpServletRequest req, HttpServletResponse res) throws javax.servlet.ServletException, java.io.IOException String temperature = req.getParameter("temperature"); DecimalFormat twoDigits = new DecimalFormat("0.00"); try double tempF = Double.parseDouble(temperature); String tempC = twoDigits.format((tempF -32)*5.0/9.0); PrintWriter out = res.getWriter(); out.println("<html>"); out.println("<head>"); out.println("</head>"); out.println("<body>"); out.println("<h3>" + temperature + " Fahrenheit is converted to " + tempC + " Celsius</h3><p>"); out.println("</body>"); out.println("</html>"); } catch(Exception e) res.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "There was an input error");

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <head> </head> <body> <h3>Please enter Fahrenheit temperature:</h3><p> <form action="/conv/test"> Temperature(F) : <input type="text" name="temperature"><br> <input type="submit" value="Submit"> </form> </body> </html>

Communication from Clients to HTTP Servlets For GET type HTTP requests you can also directly type http://<host>/<servletName> in the URL location field of the browser if you don’t want to pass in any parameter values. If you do need to pass in some parameter values with the request you can type http://<host>/<servletName>?<paramterName>=<value> such as http://localhost:8080/conv/conversion?feet=100.

Ways Servlets Communicate with Other Web Components There are four web component scopes: page, request, session, and application. The page scope only covers the current page. Web components can share and delegate the data within a request, session, or application scope.

Attribute Scopes in Servlet

Web Client Session Tracking HTTP is a stateless protocol that takes requests from web clients and responds with a file. It does not memorize what has happened in the past. FTP and Telnet protocols know the client states, such as users, connections, and disconnections but HTTP does not. A client session consists of a series of conversations between the client and web applications on the web server.

Web Client Session Tracking, contd. There are two mechanisms to handle client session tracking. One is to save the client identification and session information on the server side once the Servlet gets a request from the client. The other is to save the client information on the client browser at the client side for subsequent request use.

Web Client Session Tracking, contd. The hidden form field is a form field that does not appear on the GUI interface, but it can carry user input data just like any other input field. We can pass data via a hidden form field in a static or dynamic HTML page in an HTML form as follows: <input type =”HIDDEN” name=”myId” value=”1234” />

Web Client Session Tracking, contd. URL Rewriting http://<host>/servlet/Servlet1;jsessionid=ABC123, where ABC123 is produced by the encodeURL() method of HttpServletResponse.

Web Client Session Tracking, contd. Servlet Cookies Cookies are text files that store sets of param/value pairs. The Servlet at the server side generates the cookie based on the client’s HTTP request. The cookie is created on the server side and sent back to the client along with the HttpServletResponse object. The cookie is stored in the client’s browser.

Servlet Cookie

HttpSession Object in Session Tracking Using the HttpSession API in session management is quite straightforward, and it may be the best option for session tracking in most cases. The HttpSession object can hold a session id that can be used to identify whether the requests are within the same session so that they can share the same data. Each HttpSession object represents a single user HTTP session.

Debugging Servlets The best solution to fix a runtime logic error is to plug in the Tomcat Servlet container to JBuilder, Eclipse, or VisualAge, or just use an integrated debugger in your IDE such as Sun One Studio, BEA WebLogic, or IBM WebSphere.

Debugging Servlets, contd. Check the generated HTML source code by using View Source from the web browser menu. Some simple HTML errors can be easily found this way, which can help to locate the Servlet error. Use the sendError(int sc) method of HttpServletResponse to send an error response to the client with the specified status code and a default message. Use the out.println() method to print debugging messages on the client page. Use the jdb Java debug utility to debug the Servlet.

Debugging Servlets, contd. Use the void log(String message) method of HttpServlet to write debug information into the log file in the Tomcat installation directory. Use System.out.println() or System.err.println() to write error messages. The error messages will be displayed on the server console if Tomcat is started in developer mode. Check the request data and response data separately. Determine whether clients have sent the right data or the Servlets responded the right data. You can have separate classes to detect these problems.

Chapter 2 The End