J.Sant Servlets Joseph Sant Sheridan Institute of Technology.

Slides:



Advertisements
Similar presentations
1 Servlets Based on Notes by Dave Hollinger & Ethan Cerami Also, the Online Java Tutorial by Sun.
Advertisements

 2002 Prentice Hall. All rights reserved. Chapter 9: Servlets Outline 9.1 Introduction 9.2 Servlet Overview and Architecture Interface Servlet and.
Technology Overview JAVA Servlets CS-611 S. Witherspoon.
Introduction to Servlets Based on: Hall, Brown, Core Servlets and JavaServer Pages.
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
Servlets and a little bit of Web Services Russell Beale.
An introduction to Java Servlet Programming
18-Jun-15 JSP Java Server Pages Reference: Tutorial/Servlet-Tutorial-JSP.html.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 34 Servlets.
JSP Java Server Pages Reference:
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 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.
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.
1 Servlet How can a HTML page, displayed using a browser, cause a program on a server to be executed?
Servlets, Sessions, and Cookies Lecture 8 cs193i – Internet Technologies Summer 2004 Kelly Shaw, Stanford University.
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. - Java technology for Common Gateway Interface (CGI) programming. - It is a Java class that dynamically extends the function of a web server.
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.
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.
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.
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 Database Access. Agenda:  Setup Java Environment  Install Database  Install Database Drivers  Create Table and add records  Accessing a.
Java Servlets & Java Server Pages Lecture July 2013.
Java Servlets Lec 27. Creating a Simple Web Application in Tomcat.
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.
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.
Java Servlets and Java Server Pages Norman White Stern School of Business.
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.
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.
Field Trip #24 Setting Up a Web Server. Apache Apache is one of the most successful open source web servers In 1995 the most popular web server was the.
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
HTTP protocol Java Servlets. HTTP protocol Web system communicates with end-user via HTTP protocol HTTP protocol methods: GET, POST, HEAD, PUT, OPTIONS,
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.
1 Web Programming with Servlets & JSPs WEB APPLICATIONS – AN OVERVIEW.
Java Servlets References: Karen Anewalt, Mary Washington College.
Distributed Web Systems Java Servlets Lecturer Department University.
Servlets.
Servlet Fudamentals.
Java Servlets By: Tejashri Udavant..
Net-centric Computing
Course Outcomes of Advanced Java Programming AJP (17625, C603)
Introduction to Java Servlets on Jakarta Tomcat
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.
Java Servlets 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.
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:

J.Sant Servlets Joseph Sant Sheridan Institute of Technology

J.Sant Servlets - What Java classes launched by a web-server to respond to HTML form submissions. Programmer developed servlets are subclasses of HttpServlet Programs system output automatically redirected to the browser. inherently multi-threaded. Form input from client is automatically parsed. Each request launches a new thread.

J.Sant Servlets - Why? Provide convenient utilities for programmer to handle form input and HTML output. URL-encoded data from the form is automatically parsed. More efficient than some alternatives because lightweight threads are launched to service a request whereas other techniques launch entire processes (e.g. ASP’s, CGI). Portability. JVM security.

J.Sant Servlets – How to? download the servlet classes for your development environment and update the CLASSPATH variable. download application server. write and upload forms to the directories specified in the app server documentation. write and upload servlet java or class files to the directories specified in the app server documentation. run the server.

J.Sant Servlets – How? Install a web server capable of launching and managing servlet programs. Install the javax.servlet package to enable programmers to write servlets. Ensure CLASSPATH is changed to reference jar file containing java.servlet package. Define a servlet by subclassing the HttpServlet class and adding any necessary code to the doGet() and/or doPost() and if necessary the init() functions.

J.Sant HTML Forms FORM USING GET FORM using GET Diameter: Form interacts with circle Servlet Use textbox to get a diameter. Submit Button Sends form input to server.

J.Sant Servlets - Why Not? Need a programmer to change web site output. Ideally graphic designers and web masters should be able to change look-and-feel of site without programmer intervention. Requires Java expertise.

J.Sant Servlets - Alternatives? Many approaches possible for internet software development.  Java servlets (server) and HTML+Javascript (client). OR  CGI programs (server) and HTML+Javascript (client).  Java socket-based server application ( server ) and Java Applet client.  Java socket-based server application ( server ) and Java Application client.  EJB’s, Remote Method Calls, etc.  Java Server Pages ( server ).  Active Server Pages ( server ).

J.Sant Servlets – Coding import javax.servlet.* Subclass HttpServlet Provide any one time initialization code in the init() function. Provide code to respond to an http GET in the doGet(). Provide code to respond to an http POST in the doPost(). doPost() is commonly encoded by simply calling the doGet() public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); }

J.Sant Servlets – Coding Use getParameter() to retrieve the parameters from the form by name. String sdiam = request.getParameter("diameter"); Named Field values HTML FORM In JAVA Servlet

Servlet Code import java.io.*; import javax.servlet.*; import javax.servlet.http.*; import java.util.*; public class circle extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println( “ Circle Info \n"); try{ String sdiam = request.getParameter("diameter"); double diam = Double.parseDouble(sdiam); out.println(" Diam: " + diam + " Area: " + diam/2.0 * diam/2.0 * " Perimeter: " * diam/2.0 * ); } catch ( NumberFormatException e ){ out.println("Please enter a valid number"); } out.println(" "); } Attach a PrintWriter to Response Object Specify HTML output. Subclass HttpServlet.

J.Sant Servlet Lifecycle. Taken from IBM Website.

J.Sant Servlet Lifecycle Understanding the lifecycle might help develop more efficient and scalable applications. Servlets have functions which are executed with every request and others that are executed only once.

J.Sant Servlet Lifecycle init() executed only once when servlet is started.  Use this to load collections, open database connections etc. doPost(), doGet() executed with every request depending on whether request was a post or a get.  Responding to form submission. Destroy() executed only once when servlet is terminated.  clean-up routines

J.Sant Servlets, Servlet Containers and Application Servers Servlets require a special type of web server. These servers are sometimes called application servers. Since servlets aren’t programs they must be run within a container. Well-known servlet containers are Apache Resin and Caucho Resin. When you develop servlets or jsps you must deploy them on a server. That means you must place them in an appropriate location on the server’s directory and update the server configuration. The first step in developing and running servlets/jsps is to install an application server.

J.Sant Setting up Tomcat In Windows: Key Tasks you must download and install the Tomcat server. You must change your system environment so that you can compile servlet classes in addition to running them. you must set the JAVA_HOME environment variable. HTML files and java class files for servlets must be placed in the appropriate directories.

J.Sant Setting The CLASSPATH CLASSPATH must include servlet-api.jar in Windows XP.  Select Control Panel from the Start Menu.  Select Switch to Classic View.  Select System then the Advanced Tab.  select Environment Variables.  The environment variable you want to change is CLASSPATH. You want to add to the path not replace it. Find the end of the list of classes on the classpath and append the following.  a semi-colon.  fullinstallpathforTomcat\common\lib\servlet-api.jar.

J.Sant CLASSPATH Examples Before %CLASSPATH%;c:\jdk1.4.2_09\lib\tools.jar;. After %CLASSPATH%;c:\jdk1.4.2_09\lib\tools.jar;.; common\lib\servlet-api.jar  Select Control Panel from the Start Menu.  Select Switch to Classic View.  Select System then the Advanced Tab.  select Environment Variables.  The environment variable you want to change is CLASSPATH. You want to add to the path not replace it. Find the end of the list of classes on the classpath and append the following.  a semi-colon.  fullinstallpathforTomcat\common\lib\servlet-api.jar.

J.Sant Installing Tomcat Download the Tomcat installer into a known directory. Run the installer using all defaults for installation. You may be asked for an admin password, choose one that is easy to remember. Test the installation.  Right-click on the Tomcat icon on the right side of the application bar.  Open a browser and type in the address  The tomcat homepage should appear. Select the Servlet Examples link from the left side bar and attempt to execute some of the examples.