Servlets. - Java technology for Common Gateway Interface (CGI) programming. - It is a Java class that dynamically extends the function of a web server.

Slides:



Advertisements
Similar presentations
Servlets, JSP and JavaBeans Joshua Scotton.  Getting Started  Servlets  JSP  JavaBeans  MVC  Conclusion.
Advertisements

Java Server Pages (JSP)
 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 Stoney Jackson
An introduction to Java Servlet Programming
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 34 Servlets.
JSP Java Server Pages Reference:
Servlets. A form The HTML source Chapter 1 Please enter your name and password then press start Name: Password: In Netbeans you can graphically create.
Core Servlets Chapter 3 Link for Core Servlets code: om/archive/ om/archive/
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.
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.
Server Side Programming Web Information Systems 2012.
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.
A Servlet’s Job Read explicit data sent by client (form data) Read implicit data sent by client (request headers) Generate the results Send the explicit.
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 support for WWW Babak Esfandiari (sources: Qusay Mahmoud, Roger Impey, textbook)
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.
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.
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.
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.
S ERVLETS Hits Counter 21-Nov-15. S ERVLETS - H ITS C OUNTER Many times you would be interested in knowing total number of hits on a particular page of.
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.
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.
Advanced Java Session 6 New York University School of Continuing and Professional Studies.
CS320 Web and Internet Programming Introduction to Java Servlets Chengyu Sun California State University, Los Angeles.
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.
Configuration Web Server Tomcat - Install JDK Install Tom cat Configure Tom cat for running Servlet C:\Program Files\Apache Software Foundation\Tomcat.
Java Servlets and Java Server Pages
How CGI and Java Servlets are Run By David Stein 14 November 2006.
J2EE/Java EE Apache Tomcat v9 IBM Websphere v8 Redhat JBoss EAP v6 Oracle Weblogic v12 Fusion Web Tier ServletsJSPJSF Business Tier JMXJMSEJB.
Bayu Priyambadha, S.Kom. Static content  Web Server delivers contents of a file (html) 1. Browser sends request to Web Server 3. Web Server sends HTML.
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.
1 Lecture 8 George Koutsogiannakis/Summer 2011 CS441 CURRENT TOPICS IN PROGRAMMING LANGUAGES.
Java Servlets References: Karen Anewalt, Mary Washington College.
Distributed Web Systems Java Servlets Lecturer Department University.
Introduction to Servlets
Servlet Fudamentals.
Java Servlets By: Tejashri Udavant..
Net-centric Computing
Servlets Hits Counter 20-Jul-18.
HTTP Servlet Overview Servlets are modules that extend request/response-oriented servers, such as Java-enabled web servers. For example, a servlet might.
Servlet.
Jagdish Gangolly State University of New York at Albany
Servlets and Java Server Pages
Web Search Interfaces.
Servlets.
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.
Basic servlet structure
Presentation transcript:

Servlets

- Java technology for Common Gateway Interface (CGI) programming. - It is a Java class that dynamically extends the function of a web server.

WEB SERVER SERVLET ENGINE Servlet Web Browser File System Database

Servlets vs Traditional CGI Programs Servlets are efficient. No new process is started for each HTTP request. Servlets are convenient. Extensive Java infrastructure is already available. Servlets are powerful. It has full access to Java’s advanced features such as networking, security, database access, etc. Servlets are portable. Servlets are Secure. Servlets are inexpensive to implement.

Compiling Servlets Tomcat Standard Installation Directories –Standard location for servlets: Tomcat/webapps/ROOT/WEB-INF/classes –Alternate location for servlet classes: Tomcat/classes –Location of JAR files containing classes: Tomcat/lib To compile servlets: –javac ServletName.java

Servlet Events init() method destroy() method service() method –Request are handled in here. –Objects involved: Request object (implements the ServletRequest interface). Response object(implements the ServletResponse interface).

Servlet Generating Plain Text import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class HelloSample extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { PrintWriter out = response.getWriter(); out.println("HELLO WORLD in PLAIN TEXT"); } /* Make sure you compile this in a path specified in the CLASSPATH such as C:\Tomcat\webapps\ROOT\Web-INF\classes */

A Web Page Invoking the Servlet Testing a Simple Servlet Click ME!

Servlet Generating an HTML Page import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class HelloSample extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { response.setContentType(“text/html”); PrintWriter out = response.getWriter(); out.println(” \n ” + “SAMPLE HTML PAGE” \n” + “ \n” + “ HELLO HTML \n” + “ ”); }

package myServlets; import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class RandomNum extends HttpServlet { private int [] numbers = new int[6]; public void init( ) throws ServletException { for(int i=0; i<numbers.length; i++) numbers[i] = (int) (Math.random() * 100); } public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println(" \n Random Numbers" + " \n \n" + " LOTTERY NUMBERS ARE: \n "); for (int i=0; i < numbers.length; i++) out.println(" " + numbers[i]); out.println(" \n "); }

Form Data Handling GET - form data is attached to the end of the URL after the question mark. POST - form data is sent to the server on separate lines. Parsing this form data is handled by the getParameter( ) method of the HttpServletRequest. This method returns a String data type. String PersonName = Request.getParameter(“name”);

Servlet Handling Form Data import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class FormDataSample extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { response.setContentType(“text/html”); PrintWriter out = response.getWriter(); out.println(” \n ” + “SAMPLE FORM DATA” \n” + “ \n” + “ HELLO “ + request.getParameter(“name”) + “ \n” + “ ”); }

The Web Page Using Form A Simple Form name:

ORDER PROCESSING COMPLETE TRANSACTION INFORMATION Part Number: &nbsp Quantity: &nbsp Unit Price($): First Name: &nbsp Last Name: &nbsp Middle Initial: Shipping Address: Credit Card: &nbsp Visa &nbsp Mastercard &nbsp Discover &nbsp American Express &nbsp BankCard &nbsp Credit Card number: Please let us know by checking the proper box: &nbsp Notification? &nbsp Wait for complete order before shipment? &nbsp Inform you of Promotions?

package myServlets; import java.io.*; import javax.servlet.*; import javax.servlet.http.*; import java.util.*; public class ProcessData extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { response.setContentType("text/html"); PrintWriter out = response.getWriter( ); out.println(" \n INPUT PARAMETERS" + " \n \n" + " PARAMETERS ARE: \n"); Enumeration PNames = request.getParameterNames( ); while (PNames.hasMoreElements( )) { String PNameVal = (String) PNames.nextElement( ); String PValues= request.getParameter(PNameVal); String TString = "CARDNUM"; if (PNameVal.equals(TString))out.println(PNameVal + " = ********* "); elseout.println(PNameVal + " = " + PValues + " "); }// end of while out.println(" \n "); }// end of doGet public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { doGet(request, response); }