Representation and Management of Data on the Web

Slides:



Advertisements
Similar presentations
4 Copyright © 2005, Oracle. All rights reserved. Creating the Web Tier: Servlets.
Advertisements

1 Servlets Based on Notes by Dave Hollinger & Ethan Cerami Also, the Online Java Tutorial by Sun.
Server Side Programming Common Gateway Interface (CGI): Scripts generate Web pages or other files dynamically by processing form data and returning documents.
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.
An introduction to Java Servlet Programming
1 Servlets Part 1 Representation and Management of Data on the Web.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 34 Servlets.
1 Servlets Representation and Management of Data on the Web.
Core Servlets Chapter 3 Link for Core Servlets code: om/archive/ om/archive/
1 CS6320 – Servlet Structure and Lifecycle L. Grewe.
Comp2513 Java Servlet Basics Daniel L. Silver, Ph.D.
Java Server and Servlet CS616 Team 9 Kim Doyle, Susan Kroha, Arunima Palchowdhury, Wei Xu.
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 Written by Dr. Yaron Kanza, Edited by permission from author by Liron Blecher.
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.
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.
HTTP HyperText Transfer Protocol Stateless request/response client-server protocol Requests: Method: GET, POST, HEAD, TRACE, OPTIONS, PUT, DELETE.
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.
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.
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.
Running Servlets JSDK2.1 default.cfg : Web Server configuration information batch files to start and stop server Servlet properties in /webpages/WEB-INF.
Java Servlets & Java Server Pages Lecture July 2013.
Java Servlets Lec 27. Creating a Simple Web Application in Tomcat.
Threading Servlets Can handle multiple clients concurrently Shared resources must be synchronized or create a servlet that handles one request at a time.
1 Servlets Modified slides from Dr.Sagiv. 2 Introduction.
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.
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.
Servlets.
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.
Server-Side Programming 1cs What is a Servlet A Java application that is being run by a Web server Can receive parameters in an HTTP request Generates.
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.
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 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
Servlets.
Servlet Fudamentals.
HTTP request message two types of HTTP messages: request, response
Chapter 26 Servlets.
Servlet APIs Every servlet must implement javax.servlet.Servlet interface Most servlets implement the interface by extending one of these classes javax.servlet.GenericServlet.
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
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:

Representation and Management of Data on the Web Servlets Representation and Management of Data on the Web

What is a Servlet? Servlets are Java programs that can be run dynamically from a Web Server Servlets are a server-side technology A servlet is an intermediating layer between an HTTP request of a client and the Web server

What do Servlets do? Read data sent by the user (e.g., form data) Look up other information about request in the HTTP request (e.g., headers, cookies, etc.) Generate the results (may do this by talking to a database, file system, etc.) Format the results in a document (e.g., make it into HTML Set the appropriate HTTP response parameters (e.g., cookies, content-type, etc.) Send the document to the user

Why use a Servlet? Servlets are used to create dynamic pages. Why? Page is based on data submitted by the user. Example Scenario: Page is derived from data that changes often. Example Scenario: Page uses information from server-side sources. Example Scenario:

A Java Servlet Servlet Web browser Web server request request response

Supporting Servlets The Web server must support servlets (since it must run the servlets): Apache Tomcat Sun’s JavaServer Web Development Kit (JSWDK) Allaire Jrun – an engine that can be added to IIS, PWS, old Apache Web servers etc… Sun’s Java Web Server … We will talk about installing, configuring and running tomcat next week!

Compiling In order to compile a servlet, you may need to add to your CLASSPATH definition the following: setenv CLASSPATH ${CLASSPATH}: /usr/local/java/apache/jakarta-tomcat/lib/ant.jar: /usr/local/java/apache/jakarta-tomcat/lib/jasper.jar: /usr/local/java/apache/jakarta-tomcat/lib/jaxp.jar: /usr/local/java/apache/jakarta-tomcat/lib/parser.jar: /usr/local/java/apache/jakarta-tomcat/lib/servlet.jar: /usr/local/java/apache/jakarta-tomcat/ lib/webserver.jar

Servlet Package Need to import: javax.servlet.*; The Servlet interface defines methods that manage servlets and their communication with clients When a connection is formed, the servlet receives two objects that implement: ServletRequest ServletResponse

Object Hierarchy Servlet Generic Servlet HttpServlet YourOwnServlet

Creating a Servlet Extend the class HTTPServlet Implement doGet or doPost (or both) Both methods get: ServletRequest: methods for getting form (query) data, HTTP request headers, etc. ServletResponse: methods for setting HTTP status codes, HTTP response headers, and get a PrintWriter used for sending data to the client Usually implement doPost by calling doGet, or vice-versa

import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class TextHelloWorld extends HttpServlet { public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { PrintWriter out = res.getWriter(); out.println("Hello World"); out.close(); } public void doPost(HttpServletRequest req, doGet(req, res);

Returning HTML By default a text response is generated. In order to generate HTML Tell the browser you are sending HTML, by setting the Content-Type header Modify the println to create a legal HTML page You must set all headers before returning the document content. Can you guess why?

import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class HelloWorld extends HttpServlet { public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { res.setContentType("text/html"); PrintWriter out = res.getWriter(); out.println("<HTML><HEAD><TITLE>Hello World</TITLE></HEAD>"); out.println(“<BODY><BIG>Hello World </BIG></BODY></HTML>"); out.close(); }

Servlet Life Cycle No main() method! The server initializes and loads the servlet The servlet handles client requests The server can remove the servlet The servlet can remain loaded to handle additional requests Incur startup costs only once (hopefully)

Servlet Life Cycle Deal with requests: call the service method Load into Web server Destroy the servlet: call the destroy method Calling the init method ServletConfig Garbage Collection Servlet Class

Initializing Servlets Performed when the servlet is loaded into the Web server Servlet’s init(ServletConfig) method is called ServletConfig has methods to get initialization parameters. You put these parameters in a file. We will explain how next week. What would you do in the init method? creating tables in a database (yes / no) create Connection object to communicate with the database (yes / no)

A Glance at Servlet Configuration Parameters <?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>InitExample</servlet-name> <servlet-class>ServletInit</servlet-class> <init-param> <param-name>login</param-name> <param-value>snoopy</param-value> </init-param> </servlet> </web-app>

import java.io.*; import javax.servlet.*; import javax.servlet.http.*; import java.sql.*; public class ServletInit extends HttpServlet { Connection con; public void init(ServletConfig config) throws ServletException { // Always call super.init super.init(config); String login = config.getInitParameter("login"); Class.forName("oracle.jdbc.driver.OracleDriver"); con = DriverManager.getConnection("jdbc:oracle:thin:" + login + "/" + login + "@sol4:1521:stud"); }

public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println("<HTML><BODY><h1>Member List</h1><ul>"); Statement stmt = con.createStatement(); ResultSet rs = stmt.executeQuery("select name from member"); while (rs.next()) out.println("<li>" + rs.getString(1)); out.println("</ul></BODY></HTML>"); }

The service Method Every time the server receives a request for the servlet it: creates a thread for the request calls the service method The service method checks the type of request (GET, POST, PUT, DELETE, TRACE, OPTION) and call the appropriate method: doGet, doPost, doPut, doDelete, doTrace, doOption It is recommended to implement doPost and doGet instead of overriding service. Why?

HTTP Methods POST: Data sent in two steps Browser contacts server Sends data GET: Contacts server and sends data in single step Called when you enter a URL in the browser directly Can be called by a form: appends form data to action URL, separated by question mark

Other Methods HEAD: Client sees only header of response to determine size, etc… PUT: Place documents directly on server DELETE: Opposite of PUT TRACE: Debugging aid returns to client contents of its request OPTIONS: what options available on server

More on Service There is an automatic support for TRACE and OPTIONS by servlet so you do not have to implement them There is no method doHead. Why?

Instance of your Servlet The Standard Model Usually there is a single instance of a servlet and a thread for each user request The doGet and doPost methods must synchronize access to data structures and other resources! thread 1 thread 3 Instance of your Servlet thread 2

The Single Thread Model If you need to prevent concurrent access to a single instance of your thread use the single thread model public class YourServlet extends HttpServlet implements SingleThreadModel { … }

SingleThreadModel SingleThreadModel is a marker interface No methods Tells servlet engines about lifecycle expectations Ensure that no two threads will execute concurrently the service method of the same instance of the servlet

Server implementation of the Single Thread Model Option 1: There is a pool of instances of the servlet. Requests for the servlet are queued get an arbitrary instance, or are queued, if necessary. Option 2: For each request, an instance of the servlet is created.

Instance of your Servlet Instance of your Servlet thread 4 The Two Options thread 1 Pool of Instances Instance of your Servlet thread 3 Instance of your Servlet Queue thread 3 thread 2 Instance of your Servlet thread 1 Instance of your Servlet thread 2 Instance of your Servlet thread 3 Instance of your Servlet thread 4 Instance Per Thread

SingleThreadModel SingleThreadModel let you break servlet functionality into multiple methods Can rely on “instance state” being uncorrupted by other requests Does this prevents the need to synchronize access to all resources? Can’t rely on static members Can't rely on persistent instance state between connections The same client making the same request, can get different instances of your servlet

Destroying Servlets The server may remove a loaded servlet, Why?: asked to do so by administrator Servlet was idle a long time Before removing, calls the destroy() method can perform cleanup, e.g., close database connections Is it possible for the servlet to end without destroy being called?

Example – Counting Threads public ShutdownExample extends HttpServlet { private int serviceCounter = 0; ... //Access methods for serviceCounter protected synchronized void enteringServiceMethod() { serviceCounter++; } protected synchronized void leavingServiceMethod() serviceCounter--; protected synchronized int numServices() { return serviceCounter;

protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { enteringServiceMethod(); try { super.service(req, resp); } finally { leavingServiceMethod(); }

Notifying a Shutdown public ShutdownExample extends HttpServlet { private boolean shuttingDown; //Number of services, from before... //Access methods for shuttingDown protected setShuttingDown(boolean flag) { shuttingDown = flag; } protected boolean isShuttingDown() { return shuttingDown;

public void destroy() { /* Check to see whether there are still * service methods running, * and if there are, tell them to stop. */ if (numServices() > 0) { setShuttingDown(true); } /* Wait for the service methods to stop. */ while(numServices() > 0) { try { Thread.sleep(interval); } catch (InterruptedException e) {}

public void doPost(...) { ... for(i = 0; ((i < numberOfThingsToDo) && !isShuttingDown()); i++) { try { partOfLongRunningOperation(i); } catch (InterruptedException e) {} }

Getting Information From the User

Handling a User Request The servlet can get parameters from an HTML form <form action=… method=…> …</form> comprises a single form action – the address to which the form data is sent method – the HTTP method to use when passing parameters to the server

The <input> Tag Inside a form, INPUT tags define fields for data entry Standard input types include: buttons, checkboxes, password field, radio buttons, text fields, image-buttons, text areas, hidden fields, etc. They all associate a single (string) value with a named parameter

Example http://pita.cs.huji.ac.il:8090/servlet/update?x=19&y=104 <HTML> … <form method=“GET” action=“http://pita.cs.huji.ac.il:8090/servlet/update”> <INPUT name=“x” type=“text”> <INPUT name=“y” type=“text”> <INPUT type=“submit”> <INPUT type=“reset”> </form> </HTML> http://pita.cs.huji.ac.il:8090/servlet/update?x=19&y=104

Getting the Parameters Values To get the value of a parameter named x: req.getParameter("x") where req is HTTPServletRequest object If there can be multiple values for the parameter: req.getParameterValues() To get parameter names: req.getParameterNames()

<HTML> <HEAD> <TITLE>Sending Parameters</TITLE> </HEAD> <BODY BGCOLOR="#CC90E0"> <H1 ALIGN="LEFT">Please enter the parameters</H1> <FORM ACTION=“dbi/servlet/SetColors” METHOD=“GET”> <TABLE> <TR><TD>Background color:</TD> <TD><INPUT TYPE="TEXT" NAME="bgcolor"></TD></TR> <TR><TD>Font color:</TD> <TD><INPUT TYPE="TEXT" NAME="fgcolor"></TD></TR> <TR><TD>Font size:</TD> <TD><INPUT TYPE="TEXT" NAME="size"></TD></TR> </TABLE> <BR> <INPUT TYPE="SUBMIT" VALUE="Show Page"> </FORM> </BODY> </HTML>

http://pita.cs.huji.ac.il:8080/colors.html

http://pita.cs.huji.ac.il:8080/dbi/servlet/ SetColors?bgcolor=wheat&fgcolor=blue&size=5

import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class SetColors extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); String bg = request.getParameter("bgcolor"); String fg = request.getParameter("fgcolor"); String size = request.getParameter("size");

out.println("<HTML><HEAD><TITLE>Set Colors Example" + "</TITLE></HEAD>"); out.println("<BODY text='" + fg + "' bgcolor='" + bg + "'>"); out.println("<H1>Set Colors Example</H1>"); out.println("<FONT size='" + size + "'>"); out.println("You requested a background color " + bg + "<P>"); out.println("You requested a font color " + fg + "<P>"); out.println("You requested a font size " + size + "<P>"); out.println("</FONT></BODY></HTML>"); }

Think About it Could you have implemented this example (or something similar) using Javascript? Should you use Javascript for something like this? When?

<FORM ACTION=“dbi/servlet/SetColors” METHOD=“POST”> Handling Post You don't have to do anything different to read POST data instead of GET data!! <FORM ACTION=“dbi/servlet/SetColors” METHOD=“POST”> public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); }

Getting Information from Client Request Suppose that req is a HTTPServletRequest object Get the value of the header "Accept": req.getHeader("Accept") Get all header names req.getHeaderNames() Methods for special types of headers and other info: getCookies, getContentLength, getContentType, getMethod, getProtocol, etc.

import java.io.*; import javax.servlet.*; import javax.servlet.http.*; import java.util.*; public class ShowRequestHeaders extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); String title = "Servlet Example: Showing Request Headers";

out.println("<HTML><HEAD><TITLE>" + title + "</TITLE></HEAD>" + "<BODY BGCOLOR=\"#AACCAA\" TEXT=\"#990000\">\n" + "<H1 ALIGN=CENTER>" + title + "</H1>\n" + "<B>Request Method: </B>" + request.getMethod() + "<BR>\n" + "<B>Request URI: </B>" + request.getRequestURI() + "<BR>\n" + "<B>Request Protocol: </B>" + request.getProtocol() + "<BR><BR>\n" + "<TABLE BORDER=1 ALIGN=CENTER>\n" + "<TR BGCOLOR=\"#88AA88\">\n" + "<TH>Header Name<TH>Header Value");

Enumeration headerNames = request.getHeaderNames(); while(headerNames.hasMoreElements()) { String headerName =(String)headerNames.nextElement(); out.println("<TR><TD>" + headerName); out.println("<TD>“ + request.getHeader(headerName)); } out.println("</TABLE>\n</BODY></HTML>"); public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response);

Creating the Response of the Servlet

HTTP Response The response includes: Status line: version, status code, status message Response headers Empty line Document HTTP/1.1 200 OK Content-Type: text/plain Bla Bla Bla

Response is Buffered Servlet server Buffer response client request setBufferSize getBufferSize flushBuffer etc. client request

Setting the Response Status The status code of the HTTP response can be set by the setStatus method of HTTPServletResponse The status should be defined before sending content through the PrintWriter of the HTTPServletResponse The status can be defined after setting headers of the HTTPServletResponse The default status is 200

Shortcuts An automatic response is created by sendError(int code, String message) Returns the status code with the message Usually status code 404 (HTTPServletResponse.SC_NOT_FOUND) sendRedirect(String url) Creates a response with status code 302 (HTTPServletResponse.SC_MOVED_TEMPORARILY) with a Location header that includes the given url

Example of Using Redirect Any ideas? (Remember ex2...)

Setting Response Headers Headers can be defined using setHeader(String header, String value) of HTTPServletResponse using setIntHeader(String header, int value) using setDateHeader(String header, long milliseconds) (translate to GMT) The headers must be defined before the first time the buffer content is sent

More Headers Methods containsHeader to check existence of an header in the response setContentType setContentLength addCookie