Jagdish Gangolly State University of New York at Albany

Slides:



Advertisements
Similar presentations
Apache Tomcat as a container for Servlets and JSP
Advertisements

 2002 Prentice Hall. All rights reserved. Chapter 9: Servlets Outline 9.1 Introduction 9.2 Servlet Overview and Architecture Interface Servlet and.
 2003 Prentice Hall, Inc. All rights reserved. Chapter 24: Servlets Outline 24.1 Introduction 24.2 Servlet Overview and Architecture Interface.
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
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/
Comp2513 Java Servlet Basics Daniel L. Silver, Ph.D.
Java Server and Servlet CS616 Team 9 Kim Doyle, Susan Kroha, Arunima Palchowdhury, Wei Xu.
Server Side Programming Web Information Systems 2012.
Servlets. Our Project 3-tier application Develop our own multi-threaded server Socket level communication.
Java Servlets and JSP.
Gayle J Yaverbaum, PhD Professor of Information Systems Penn State Harrisburg.
Java Servlets CS-422. Application Mapping Your servlet application will be mapped to a directory structure: –“myapp” maps to some directory C:/docs/apps/myapp.
Servlets. - Java technology for Common Gateway Interface (CGI) programming. - It is a Java class that dynamically extends the function of a web server.
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.
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,
Servlet Lifecycle Lec 28. Servlet Life Cycle  Initialize  Service  Destroy Time.
Java Servlets Outline 24.1 Introduction 24.2 Servlet Overview and Architecture Interface Servlet and the Servlet Life Cycle HttpServlet Class.
Java Servlets & Java Server Pages Lecture July 2013.
Java Servlets Lec 27. Creating a Simple Web Application in Tomcat.
Servlets Servlets are modules that extend the functionality of a “java-enabled” web-server They normally generate HTML code and web content dynamically.
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.
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.
A seminar on j2ee by saritha. s. What is J2EE J2EE (Java 2 Platform, Enterprise Edition) is a Java platform designed for the mainframe-scale computing.
@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.
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.
HTTP protocol Java Servlets. HTTP protocol Web system communicates with end-user via HTTP protocol HTTP protocol methods: GET, POST, HEAD, PUT, OPTIONS,
Introduction To HTML Dr. Magdi AMER. HTML elements.
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.
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 Web Programming with Servlets & JSPs WEB APPLICATIONS – AN OVERVIEW.
Distributed Web Systems Java Servlets Lecturer Department University.
Programming with Java Lecture 6 Elements of a Java Servlet
Introduction to Servlets
Building Web Apps with Servlets
Servlet Fudamentals.
Java Servlets By: Tejashri Udavant..
CSE 403: Servlet Technology
Net-centric Computing
Course Outcomes of Advanced Java Programming AJP (17625, C603)
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.
V. Gyurjyan Clas12 Software Meeting
Java Servlets II: Session Tracking
SERVLETS AND JDBC.
Servlet.
Servlets and JSP 20-Nov-18 servletsJSP.ppt.
COP 4610L: Applications in the Enterprise Spring 2005
Java Servlets and JSP.
Servlets.
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
Introduction to Java Servlets
Servlets Servlets are modules that extend the functionality of a “java-enabled” web-server They normally generate HTML code and web content dynamically.
Directories and DDs 25-Apr-19.
Basic servlet structure
Directories and DDs 21-Jul-19.
Directories and DDs 14-Sep-19.
Java Chapter 7 (Estifanos Tilahun Mihret--Tech with Estif)
Presentation transcript:

Jagdish Gangolly State University of New York at Albany Java Servlets Jagdish Gangolly State University of New York at Albany Acc 683, Spring2001 (Jagdish S. Gangolly) 11/17/2018

Java Servlets Types of web containers Structure of web applications An Example Servlet Life cycle Acc 683, Spring2001 (Jagdish S. Gangolly) 11/17/2018

Types of web containers Web containers built-in to a J2EE application server: Weblogic, Inprise, iPlanet, WebSphere Web containers built into a webserver: Sun’s Java Webserver, Jakarta Tomcat Web container in a separate runtime: Servlet engines as plug-ins to web servers such as IIS, Apache, etc. (Jrun, ServletExec, …) Acc 683, Spring2001 (Jagdish S. Gangolly) 11/17/2018

Structure of web applications Public directory: where all html files are kept WEB-INF/web.xml file: Deployment descriptor WEB-INF/classes directory: compiled classes of servlets and their utility classes WEB-INF/lib directory: packaged JAR files Acc 683, Spring2001 (Jagdish S. Gangolly) 11/17/2018

An Example What the application does: Steps: Prompts the user for a name and an e-mail address Prints a welcome greeting based on the time of the day Steps: First Contact html page (requesting info) Servlet to process request and generate html page Deployment descriptor Acc 683, Spring2001 (Jagdish S. Gangolly) 11/17/2018

1. Contact html page <HTML> <HEAD> <TITLE>ProJava Registration</TITLE> </HEAD> <BODY> <H1>Welcome</H1> <FORM ACTION="/greeting/servlet/GreetingServlet" METHOD="POST"> <P>Your Name <INPUT TYPE="text" SIZE="40" NAME="name"></P> <P>Your Email <INPUT TYPE="text" SIZE="40" NAME="email"> <INPUT TYPE="submit" VALUE="Submit"></P> </FORM> </BODY> </HTML> Acc 683, Spring2001 (Jagdish S. Gangolly) 11/17/2018

The Servlet I Importing packages // Import Servlet packages import javax.servlet.*; import javax.servlet.http.*; // Import other Java packages import java.io.*; import java.util.*; Acc 683, Spring2001 (Jagdish S. Gangolly) 11/17/2018

The Servlet II The Servlet class public class GreetingServlet extends HttpServlet { } Acc 683, Spring2001 (Jagdish S. Gangolly) 11/17/2018

The Servlet II 3. Implement the doPost method protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } Acc 683, Spring2001 (Jagdish S. Gangolly) 11/17/2018

The Servlet III Implement the doPost method: Details 1. Get parameters from request 2. Compute greeting message 3. Set MIMI type for response & obtain printwriter object for generating response 4. Write response html content Acc 683, Spring2001 (Jagdish S. Gangolly) 11/17/2018

The Servlet IV 1. Get parameters from request String name = request.getParameter("name"); String email = request.getParameter("email"); Acc 683, Spring2001 (Jagdish S. Gangolly) 11/17/2018

The Servlet V 2. Compute greeting message String message = null; GregorianCalendar calendar = new GregorianCalendar(); if(calendar.get(Calendar.AM_PM) == Calendar.AM) { message = "Good Morning"; } else { message = "Good Afternoon"; Acc 683, Spring2001 (Jagdish S. Gangolly) 11/17/2018

The Servlet VI 3. Set MIMI type for response & obtain printwriter object for generating response response.setContentType("text/html"); PrintWriter out = response.getWriter(); Acc 683, Spring2001 (Jagdish S. Gangolly) 11/17/2018

The Servlet VI 4. Write response html content out.println("<HTML>"); out.println("<BODY>"); out.println("<P>" + message + ", " + name + "</P>"); out.println("<P> Thanks for registering your email (" + email + ") with us.</P>"); out.println("<P> - The Pro Java Team. </P>"); out.println("</BODY>"); out.println("</HTML>"); out.close(); Acc 683, Spring2001 (Jagdish S. Gangolly) 11/17/2018

The Servlet VII: The Whole Program import javax.servlet.*; import javax.servlet.http.*; import java.io.*; import java.util.*; public class GreetingServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String name = request.getParameter("name"); String email = request.getParameter("email"); String message = null; GregorianCalendar calendar = new GregorianCalendar(); if(calendar.get(Calendar.AM_PM) == Calendar.AM) { message = "Good Morning"; } else {message = "Good Afternoon"; } response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println("<HTML>"); out.println("<BODY>"); out.println("<P>" + message + ", " + name + "</P>"); out.println("<P> Thanks for registering your email (" + email + ") with us.</P>"); out.println("<P> - The Pro Java Team. </P>"); out.println("</BODY>"); out.println("</HTML>"); out.close(); Acc 683, Spring2001 (Jagdish S. Gangolly) 11/17/2018

Deployment descriptor <?xml version="1.0" encoding="ISO-8859-1"?> <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN" "http://java.sun.com/j2ee/dtds/web-app_2.2.dtd"> <web-app> <servlet> <!-- Servlet alias --> <servlet-name>Registration</servlet-name> <!-- Fully qualified Servlet class --> <servlet-class>GreetingServlet</servlet-class> </servlet> </web-app> Acc 683, Spring2001 (Jagdish S. Gangolly) 11/17/2018

Servlet Lifecycle (Fig on p.356) Creation of thread pool Instantiation of servlet Call to init() method (initialisation) Allocation of requests to threads Call to service() method Terminate threadpool Call to destroy() method Termination of servlet (garbage collection) Container shutdown Acc 683, Spring2001 (Jagdish S. Gangolly) 11/17/2018

Servlet Lifecycle: An example I Freak servlet: Life cycle Steps: First unavailable html page (UnavailableException()) Servlet to process request and generate response Deployment descriptor Acc 683, Spring2001 (Jagdish S. Gangolly) 11/17/2018

Servlet Lifecycle: An example II Unavailable html page: <HTML> <META HTTP-EQUIV="Pragma" CONTENT="no-cache"> <HEAD> <TITLE>FreakServlet Unavailable</TITLE> </HEAD> <BODY> <H1>FreakServlet Unavailable</H1> <P>FreakServlet is temporarily unavailable. Please try <A HREF="/lifeCycle/servlet/freak">again</A>.</P> </BODY> </HTML> Acc 683, Spring2001 (Jagdish S. Gangolly) 11/17/2018

Servlet request and generate response I 2. The FreakServlet Importing packages The Servlet class Instantiation of servlet (constructor) createState() method Init() method to initialise (ready for requests) doGet() method to build response to queries (service) Destroy() method Acc 683, Spring2001 (Jagdish S. Gangolly) 11/17/2018

Importing packages // Import servlet packages import javax.servlet.*; import javax.servlet.http.*; // Import other Java packages import java.io.*; import java.util.*; Acc 683, Spring2001 (Jagdish S. Gangolly) 11/17/2018

The Servlet class public class FreakServlet extends HttpServlet { java.util.Vector states; java.util.Random random; int waitInterval; public static final int DEFAULT_WAIT_INTERVAL = 10; … } Acc 683, Spring2001 (Jagdish S. Gangolly) 11/17/2018

Instantiation of servlet (constructor) public FreakServlet() { states = new java.util.Vector(); random = new java.util.Random(); waitInterval = DEFAULT_WAIT_INTERVAL; states.add(createState("Instantiation")); } Acc 683, Spring2001 (Jagdish S. Gangolly) 11/17/2018

createState() method private String createState(String message) { return "[" + (new java.util.Date()).toString() + "] " + message; } Acc 683, Spring2001 (Jagdish S. Gangolly) 11/17/2018

Init() method to initialise (ready for requests) public void init() throws ServletException { states.add(createState("Initialization")); String waitIntervalString = getServletConfig().getInitParameter("waitInterval"); if (waitIntervalString != null) { waitInterval = new Integer(waitIntervalString).intValue(); } Acc 683, Spring2001 (Jagdish S. Gangolly) 11/17/2018

doGet() method to build response to queries (service) protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { if(random.nextBoolean()) { // Not available for waitInterval seconds. states.add(createState("Unavailable from doGet")); throw new UnavailableException("Unavailable from doGet", waitInterval); } states.add(createState("Service")); //see HTML Generation on next slide Acc 683, Spring2001 (Jagdish S. Gangolly) 11/17/2018

HTML Page generation response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println("<HTML>"); out.println("<META HTTP-EQUIV=\"Pragma\" CONTENT=\"no-cache\">"); out.println("<HEAD><TITLE>"); out.println("FreakServlet: State History"); out.println("</TITLE></HEAD>"); out.println("<BODY>"); out.println("<H1>FreakServlet: State History</H1>"); out.println("<a href=\"/lifeCycle/servlet/freak\">Reload</a></p>"); for(int i = 0; i < states.size(); i++) { out.println("<p> " + states.elementAt(i) + "</p>"); } out.println("</BODY></HTML>"); out.close(); Acc 683, Spring2001 (Jagdish S. Gangolly) 11/17/2018

Destroy() method public void destroy() { states.add(createState("Destroy")); log("Flushing state history of LifeCycleTest servlet."); for(int i = 0; i < states.size(); i++) { log(states.elementAt(i).toString()); } Acc 683, Spring2001 (Jagdish S. Gangolly) 11/17/2018

Deployment descriptor <?xml version="1.0" encoding="ISO-8859-1"?> <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN" "http://java.sun.com/j2ee/dtds/web-app_2.2.dtd"> <web-app> <servlet> <servlet-name>freak</servlet-name> <servlet-class>FreakServlet</servlet-class> <init-param> <param-name>waitInterval</param-name> <param-value>5</param-value> </init-param> </servlet> <error-page> <exception-type>javax.servlet.UnavailableException</exception-type> <location>/unavailable.html</location> </error-page> </web-app> Acc 683, Spring2001 (Jagdish S. Gangolly) 11/17/2018