Servlets Servlets are modules that extend the functionality of a “java-enabled” web-server They normally generate HTML code and web content dynamically.

Slides:



Advertisements
Similar presentations
Java Server Pages (JSP)
Advertisements

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
18-Jun-15 JSP Java Server Pages Reference: Tutorial/Servlet-Tutorial-JSP.html.
JSP Java Server Pages Reference:
Servlets Servlets are modules that extend the functionality of a “java-enabled” web-server They normally generate HTML code and web content dynamically.
June 1, 2000 Object Oriented Programming in Java (95-707) Advanced Topics 1 Lecture 10 Object Oriented Programming in Java Advanced Topics 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. A form The HTML source Chapter 1 Please enter your name and password then press start Name: Password: In Netbeans you can graphically create.
2/16/2004 Dynamic Content February 16, /16/2004 Assignments Due – Message of the Day Part 1 Due – Reading and Warmup Work on Message of the Day.
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.
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.
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.
Java support for WWW Babak Esfandiari (sources: Qusay Mahmoud, Roger Impey, textbook)
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.
SKT-SSU IT Training Center Servlet and JSP. Chapter Three: Servlet Basics.
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.
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.
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.
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.
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.
CS320 Web and Internet Programming Introduction to Java Servlets Chengyu Sun California State University, Los Angeles.
Java and the Web CSE 3330 Southern Methodist University.
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.
 Java Server Pages (JSP) By Offir Golan. What is JSP?  A technology that allows for the creation of dynamically generated web pages based on HTML, XML,
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.
Introduction to Servlets
Servlet Fudamentals.
Java Servlets By: Tejashri Udavant..
Net-centric Computing
JDBC & Servlet CSE 4504/6504 Lab.
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.
Java Servlets 9/21/2018.
Servlet.
Chapter 26 Servlets.
Distributed Computing, M. L. Liu
Jagdish Gangolly State University of New York at Albany
Servlets and Java Server Pages
Servlets and JSP 20-Nov-18 servletsJSP.ppt.
Java Servlets and JSP.
Servlets.
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.
Proposed 1 Modify the count servlet in order to generate an answer showing the IP addresses of ALL computer that have accessed it Use a vector or.
Basic servlet structure
Presentation transcript:

Servlets Servlets are modules that extend the functionality of a “java-enabled” web-server They normally generate HTML code and web content dynamically. This is sent to the browser which displays it. For example, they send a query to a database based on parameters sent by the browser and send the results to the browser in html format

Development Environments There are many good development environments which help to write and test the servlets They include an editor and a java-enabled sever They also include all the necessary jar files an import statements Some of them are Eclipse (need to download plugins) and Netbeans (which also has full j2ee support

Anatomy of a Servlet A new servlet can be written by extending the HttpServlet class which has the following pre-defined methods init() is called when the servlet is “uploaded” the first time (this can vary depending on the server) doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException is called every time the servlet is contacted by a GET request (which is the default way) doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException is called when the client contacted the servlet with a POST request

import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class MyServlet extends HttpServlet { public void init() { //Overwrite } public void doGet ( HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // Overwrite public void doPost( HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

The HttpServletRequest Parameter HttpServletRequest is the class of the first parameter the server uses to calls doGet and doPost. It gives access to: Information about the client, for example, parameters passed, protocol used, client’s host, etc. The input stream, ServletInputStream is used by the servlet to receive data from the client when the method POST or PUT has been used.

The HttpServletResponse parameter HttpServletResponse is the class of the second argument. Provides methods for : Declaring the MIME type of the answer that will be sent to the client Getting the output stream ServletOutputStream and a Writer through which the servlet can send dinamically generated html code to the browser. Sending other information to the browser (cookies, refreshment time, etc…)

Example 1 import java.io.*; import javax.servlet.*; import javax.servlet.http.*; import java.util.Date; public class SimpleServlet extends HttpServlet { public void doGet ( HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // set content type response.setContentType("text/html"); // open print writer to browser PrintWriter out = response.getWriter(); //send data out.println("<HTML>") out.println("<H1> Mi Primer Servlet </H1>"); out.println("<BR> <H2>Fecha y hora: "+(new Date())+"<H2>"); out.println("</HTML>"); out.close(); }

Example 1 Imports necessary classes This is for the Date class import java.io.*; import javax.servlet.*; import javax.servlet.http.*; import java.util.Date; public class SimpleServlet extends HttpServlet { public void doGet ( HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // set content type response.setContentType("text/html"); // open print writer to browser PrintWriter out = response.getWriter(); //send data out.println("<HTML>") out.println("<H1> Mi Primer Servlet </H1>"); out.println("<BR> <H2>Fecha y hora: "+(new Date())+"<H2>"); out.println("</HTML>"); out.close(); } Imports necessary classes This is for the Date class

Example 1 Every servlet extends HttpServlet Overwrites doGet method import java.io.*; import javax.servlet.*; import javax.servlet.http.*; import java.util.Date; public class SimpleServlet extends HttpServlet { public void doGet ( HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // set content type response.setContentType("text/html"); // open print writer to browser PrintWriter out = response.getWriter(); //send data out.println("<HTML>") out.println("<H1> Mi Primer Servlet </H1>"); out.println("<BR> <H2>Fecha y hora: "+(new Date())+"<H2>"); out.println("</HTML>"); out.close(); } Every servlet extends HttpServlet Overwrites doGet method

Example 1 import java.io.*; import javax.servlet.*; import javax.servlet.http.*; import java.util.Date; public class SimpleServlet extends HttpServlet { public void doGet ( HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // set content type response.setContentType("text/html"); // open print writer to browser PrintWriter out = response.getWriter(); //send data out.println("<HTML>") out.println("<H1> Mi Primer Servlet </H1>"); out.println("<BR> <H2>Fecha y hora: "+(new Date())+"<H2>"); out.println("</HTML>"); out.close(); } Tells the browser the content type of the answer Gets writer to browser from response parameter

Example 1 import java.io.*; import javax.servlet.*; import javax.servlet.http.*; import java.util.Date; public class SimpleServlet extends HttpServlet { public void doGet ( HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // set content type response.setContentType("text/html"); // open print writer to browser PrintWriter out = response.getWriter(); //send data out.println("<HTML>") out.println("<H1> Mi Primer Servlet </H1>"); out.println("<BR> <H2>Fecha y hora: "+(new Date())+"<H2>"); out.println("</HTML>"); out.close(); } Print data to browser Get date and time from system Close connection to browser

Running the first example Writing a servlet with Netbeans is very easy Also the deployment is done automatically Open netbeans Create a web project (this will create a lot of directories for putting the different kind of files) Create a servlet Copy the code of SimpleServlet.java Run the file

A second example Implementing a web counter It will count how many times an object of this class has been creates It will show the Address of the computer that contacted the servlet It will show a

import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class Count extends HttpServlet { int count = 0; // a counter starts in 0 public void doGet ( HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { count++; PrintWriter out = res.getWriter(); res.setContentType("text/html"); out.println("<H1> A web page counter </H1>"); out.println("<HR>"); out.println("This servlet was accessed "+count+" time(s)"); out.println("Your computer is "+req.getRemoteHost()); out.close(); }

import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class Count extends HttpServlet { int count = 0; // a counter starts in 0 public void doGet ( HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { count++; PrintWriter out = res.getWriter(); res.setContentType("text/html"); out.println("<H1> A web page counter </H1>"); out.println("<HR>"); out.println("This servlet was accessed "+count+" time(s)"); out.println("Your computer is "+req.getRemoteHost()); out.close(); }

Increments counter every time doGet is called by the web server import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class Count extends HttpServlet { int count = 0; // a counter starts in 0 public void doGet ( HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { count++; PrintWriter out = res.getWriter(); res.setContentType("text/html"); out.println("<H1> A web page counter </H1>"); out.println("<HR>"); out.println("This servlet was accessed "+count+" time(s)"); out.println("Your computer is "+req.getRemoteHost()); out.close(); } Increments counter every time doGet is called by the web server

Sine Qua Non import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class Count extends HttpServlet { int count = 0; // a counter starts in 0 public void doGet ( HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { count++; PrintWriter out = res.getWriter(); res.setContentType("text/html"); out.println("<H1> A web page counter </H1>"); out.println("<HR>"); out.println("This servlet was accessed "+count+" time(s)"); out.println("Your computer is "+req.getRemoteHost()); out.close(); } Sine Qua Non

import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class Count extends HttpServlet { int count = 0; // a counter starts in 0 public void doGet ( HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { count++; PrintWriter out = res.getWriter(); res.setContentType("text/html"); out.println("<H1> A web page counter </H1>"); out.println("<HR>"); out.println("This servlet was accessed "+count+" time(s)"); out.println("Your computer is "+req.getRemoteHost()); out.close(); } Print data to browser

What happens if the server crashes and starts again ? The counter will start from 0 again To “remember” the value of the counter in cast of an unexpected crash, we will write the value of the variable in a file every time it changes. At the beginning, the servlet reads the initial value from a file, if it exists, or creates the file with the initial value = 0

public class Count extends HttpServlet { int count = 0; // a counter for the object public void init() { try { BufferedReader in = new BufferedReader( newFileReader(„count.txt“)); String l = in.readLine(); count = Integer.parseInt(l); } catch (FileNotFoundException e) { //no need to do anything here } public void doGet ( HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { count++; PrintWriter outFile = new PrintWriter( new Filereader(„count.txt“)); outFile.println(count); outFile.close(); PrintWriter outBrowser = res.getWriter(); res.setContentType("text/html"); outBrowser.println("<H1> A web page counter </H1>"); outBrowser.println("<HR>"); .....

Try to open the file when the servlet is called the first time public class Count extends HttpServlet { int count = 0; // a counter for the object public void init() { try { BufferedReader in = new BufferedReader( newFileReader(„count.txt“)); String l = in.readLine(); count = Integer.parseInt(l); } catch (FileNotFoundException e) { //no need to do anything here } public void doGet ( HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { count++; PrintWriter outFile = new PrintWriter( new Filereader(„count.txt“)); outFile.println(count); outFile.close(); PrintWriter outBrowser = res.getWriter(); res.setContentType("text/html"); outBrowser.println("<H1> A web page counter </H1>"); outBrowser.println("<HR>"); ..... Try to open the file when the servlet is called the first time

Read the line and convert the content to its integer value public class Count extends HttpServlet { int count = 0; // a counter for the object public void init() { try { BufferedReader in = new BufferedReader( newFileReader(„count.txt“)); String l = in.readLine(); count = Integer.parseInt(l); } catch (FileNotFoundException e) { //no need to do anything here } public void doGet ( HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { count++; PrintWriter outFile = new PrintWriter( new Filereader(„count.txt“)); outFile.println(count); outFile.close(); PrintWriter outBrowser = res.getWriter(); res.setContentType("text/html"); outBrowser.println("<H1> A web page counter </H1>"); outBrowser.println("<HR>"); ..... Read the line and convert the content to its integer value

public class Count extends HttpServlet { int count = 0; // a counter for the object public void init() { try { BufferedReader in = new BufferedReader( newFileReader(„count.txt“)); String l = in.readLine(); count = Integer.parseInt(l); } catch (FileNotFoundException e) { //no need to do anything here } public void doGet ( HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { count++; PrintWriter outFile = new PrintWriter( new Filereader(„count.txt“)); outFile.println(count); outFile.close(); PrintWriter outBrowser = res.getWriter(); res.setContentType("text/html"); outBrowser.println("<H1> A web page counter </H1>"); outBrowser.println("<HR>"); .....

After count is incremented, open the file to write (overwrite), public class Count extends HttpServlet { int count = 0; // a counter for the object public void init() { try { BufferedReader in = new BufferedReader( newFileReader(„count.txt“)); String l = in.readLine(); count = Integer.parseInt(l); } catch (FileNotFoundException e) { //no need to do anything here } public void doGet ( HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { count++; PrintWriter outFile = new PrintWriter( new Filereader(„count.txt“)); outFile.println(count); outFile.close(); PrintWriter outBrowser = res.getWriter(); res.setContentType("text/html"); outBrowser.println("<H1> A web page counter </H1>"); outBrowser.println("<HR>"); ..... After count is incremented, open the file to write (overwrite), write the new number and close file

public class Count extends HttpServlet { int count = 0; // a counter for the object public void init() { try { BufferedReader in = new BufferedReader( newFileReader(„count.txt“)); String l = in.readLine(); count = Integer.parseInt(l); } catch (FileNotFoundException e) { //no need to do anything here } public void doGet ( HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { count++; PrintWriter outFile = new PrintWriter( new Filereader(„count.txt“)); outFile.println(count); outFile.close(); PrintWriter outBrowser = res.getWriter(); res.setContentType("text/html"); outBrowser.println("<H1> A web page counter </H1>"); outBrowser.println("<HR>"); ..... The rest is the same