Servlets: Servlet / Web Browser Communication I

Slides:



Advertisements
Similar presentations
Servlets. Servlets are modules that extend Java-enabled web servers. For example, a servlet might be responsible for taking data in an HTML order-entry.
Advertisements

1 Web Search Interfaces. 2 Web Search Interface Web search engines of course need a web-based interface. Search page must accept a query string and submit.
1 Servlets Based on Notes by Dave Hollinger & Ethan Cerami Also, the Online Java Tutorial by Sun.
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.
Server Side Programming Common Gateway Interface (CGI): Scripts generate Web pages or other files dynamically by processing form data and returning documents.
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.
An introduction to Java Servlet Programming
June 1, 2000 Object Oriented Programming in Java (95-707) Advanced Topics 1 Lecture 10 Object Oriented Programming in Java Advanced Topics Servlets.
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.
Servlets Replace Common Gateway Interface Scripts Extend Server Functionality Modules (software components) Like applets to browsers No GUI.
Comp2513 Java Servlet Basics Daniel L. Silver, Ph.D.
1 Servlets and HTML Form Data Parts of this presentation was provided by Vijayan Sugumaran Department of DIS Oakland University Rochester,
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.
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.
Gayle J Yaverbaum, PhD Professor of Information Systems Penn State Harrisburg.
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)
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.
J2EE training: 1 Course Material Usage Rules PowerPoint slides for use only in full-semester, for-credit courses at degree-granting.
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 Lec 27. Creating a Simple Web Application in Tomcat.
Slides © Marty Hall, book © Sun Microsystems Press 1 Handling the Client Request: Form Data Core Servlets & JSP book:
Saving Client State Session Tracking: Maintain state about series of requests from same client over time Using Cookies: Clients hold small amount of their.
Introduction to Server-Side Web Development Introduction to Server-Side Web Development Session II: Introduction to Server-Side Web Development with 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.
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,
Introduction To HTML Dr. Magdi AMER. HTML elements.
How CGI and Java Servlets are Run By David Stein 14 November 2006.
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.
Java Servlets References: Karen Anewalt, Mary Washington College.
Programming with Java Lecture 6 Elements of a Java Servlet
Introduction to Servlets
LECTURE 8 (ETCS-308) Subject teacher : Ms. Gunjan Beniwal
Servlets.
Net-centric Computing
JDBC & Servlet CSE 4504/6504 Lab.
Servlets Hits Counter 20-Jul-18.
Session Tracking in Servlets
Pre-assessment Questions
HTTP request message two types of HTTP messages: request, response
Sessions.
Servlets.
Servlet.
Chapter 26 Servlets.
Java Server Pages.
Jagdish Gangolly State University of New York at Albany
Servlets and Java Server Pages
Servlets and JSP 20-Nov-18 servletsJSP.ppt.
CS134 Web Design & Development
Handling FORM Data using 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.
Web Search Interfaces.
Web Search Interfaces by Ray Mooney
Parameters passed by client
Servlets Servlets are modules that extend the functionality of a “java-enabled” web-server They normally generate HTML code and web content dynamically.
Web Design & Development
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.
Parameters passed by client
Basic servlet structure
Presentation transcript:

Servlets: Servlet / Web Browser Communication I Ethan Cerami New York University 8/27/2019 Servlet / Web Browser Communication I

Servlet / Web Browser Communication I Road Map Overview of Browser/Servlet Communication Reading Form Data from Servlets Example 1: Reading three parameters Example 2: Reading all parameters Case Study: Resume Posting Service 8/27/2019 Servlet / Web Browser Communication I

Overview of Browser/Servlet Communication 8/27/2019 Servlet / Web Browser Communication I

Servlet / Web Browser Communication I Overview This lecture is the first in two lectures that discuss the interaction between web browsers and servlets. Web Browser Request Web Server Response 8/27/2019 Servlet / Web Browser Communication I

Servlet / Web Browser Communication I Client Request Data When a user submits a browser request to a web server, it sends two categories of data: Form Data: Data that the user explicitly typed into an HTML form. For example: registration information. HTTP Request Header Data: Data that is automatically appended to the HTTP Request from the client. For example: cookies, browser type, browser IP address. This lecture examines Form Data; the next lecture examines HTTP Data. 8/27/2019 Servlet / Web Browser Communication I

Servlet / Web Browser Communication I Form Data 8/27/2019 Servlet / Web Browser Communication I

Servlet / Web Browser Communication I Form Data Based on our understanding of HTML, we now know how to create user forms. We also know how to gather user data via all the form controls: text, password, select, checkbox, radio buttons, etc. Now, the question is: if I submit form data to a servlet, how do I extract this form data? Figuring this out forms the basis of creating interactive web applications that respond to user requests. 8/27/2019 Servlet / Web Browser Communication I

Reading Form Data from Servlets The HttpServletRequest object contains three main methods for extracting form data: getParameter(): used to retrieve a single form parameter. getParameterValues(): used to retrieve a list of form values, e.g. a list of selected checkboxes. getParameterNames(): used to retrieve a full list of all parameter names submitted by the user. We will examine each of these and then explore several examples. 8/27/2019 Servlet / Web Browser Communication I

Servlet / Web Browser Communication I Reading Form Data All these methods work the same way regardless of whether the browser uses HTTP GET or HTTP POST. Remember that form elements are case sensitive. Therefore, “userName” is not the same as “username.” 8/27/2019 Servlet / Web Browser Communication I

getParameter() Method Used to retrieve a single form parameter. Possible return values: String: corresponds to the form parameter. Empty String: parameter exists, but has no value. null: parameter does not exist. 8/27/2019 Servlet / Web Browser Communication I

getParameterValues() Method Used to retrieve multiple form parameters with the same name. For example, a series of checkboxes all have the same name, and you want to determine which ones have been selected. Returns an Array of Strings. An array with a single empty string indicates that the form parameter exists, but has no values. null: indicates that the parameter does not exist. 8/27/2019 Servlet / Web Browser Communication I

getParameterNames() method Returns an Enumeration object. By cycling through the enumeration object, you can obtain the names of all parameters submitted to the servlet. Note that the Servlet API does not specify the order in which parameter names appear. 8/27/2019 Servlet / Web Browser Communication I

Example 1: Reading three explicit parameters 8/27/2019 Servlet / Web Browser Communication I

Servlet / Web Browser Communication I Example 1 Our first example consists of one HTML page, and one servlet. The HTML page contains three form parameters: param1, param2, and param3. The Servlet extracts these specific parameters and echoes them back to the browser. Before we examine the code, let’s try it out: http://ecerami.com/applied_fall_2000/examples/servlets/ThreeParamsForm.html 8/27/2019 Servlet / Web Browser Communication I

Servlet / Web Browser Communication I <HTML> <HEAD> <TITLE>Collecting Three Parameters</TITLE> </HEAD> <BODY BGCOLOR="#FDF5E6"> <H1 ALIGN="CENTER">Collecting Three Parameters</H1> <FORM ACTION="/servlet/coreservlets.ThreeParams"> First Parameter: <INPUT TYPE="TEXT" NAME="param1"><BR> Second Parameter: <INPUT TYPE="TEXT" NAME="param2"><BR> Third Parameter: <INPUT TYPE="TEXT" NAME="param3"><BR> <CENTER> <INPUT TYPE="SUBMIT"> </CENTER> </FORM> </BODY> </HTML> 8/27/2019 Servlet / Web Browser Communication I

Servlet / Web Browser Communication I package coreservlets; import java.io.*; import javax.servlet.*; import javax.servlet.http.*; /** Simple servlet that reads three parameters from the * form data. */ public class ThreeParams extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); String title = "Reading Three Request Parameters"; Continued…. 8/27/2019 Servlet / Web Browser Communication I

Servlet / Web Browser Communication I out.println(ServletUtilities.headWithTitle(title) + "<BODY BGCOLOR=\"#FDF5E6\">\n" + "<H1 ALIGN=CENTER>" + title + "</H1>\n" + "<UL>\n" + " <LI><B>param1</B>: " + request.getParameter("param1") + "\n" + " <LI><B>param2</B>: " + request.getParameter("param2") + "\n" + " <LI><B>param3</B>: " + request.getParameter("param3") + "\n" + "</UL>\n" + "</BODY></HTML>"); } 8/27/2019 Servlet / Web Browser Communication I

Example 2: Reading all Parameters 8/27/2019 Servlet / Web Browser Communication I

Servlet / Web Browser Communication I Example 2 Example 1 will only read explicit parameters. Now, let’s look at a Servlet that echoes back all the form parameters you send it. You will probably remember this servlet from our discussions of HTML forms. 8/27/2019 Servlet / Web Browser Communication I

Servlet / Web Browser Communication I Example 2 The Example works by first calling getParameterNames(). By cycling through the returned Enumeration, the servlet can access all form names. For each form name, we call getParameterValues() to extract the form values. By cycling through the returned array of strings, we then print out all the associated values. 8/27/2019 Servlet / Web Browser Communication I

Output a simple HTML table for displaying the form parameters. package coreservlets; import java.io.*; import javax.servlet.*; import javax.servlet.http.*; import java.util.*; public class ShowParameters extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); String title = "Reading All Request Parameters"; out.println(ServletUtilities.headWithTitle(title) + "<BODY BGCOLOR=\"#FDF5E6\">\n" + "<H1 ALIGN=CENTER>" + title + "</H1>\n" + "<TABLE BORDER=1 ALIGN=CENTER>\n" + "<TR BGCOLOR=\"#FFAD00\">\n" + "<TH>Parameter Name<TH>Parameter Value(s)"); Output a simple HTML table for displaying the form parameters. Continued….

Then, iterate through each element within the Enumeration. Enumeration paramNames = request.getParameterNames(); while(paramNames.hasMoreElements()) { String paramName = (String)paramNames.nextElement(); out.print("<TR><TD>" + paramName + "\n<TD>"); String[] paramValues = request.getParameterValues(paramName); if (paramValues.length == 1) { String paramValue = paramValues[0]; if (paramValue.length() == 0) out.println("<I>No Value</I>"); else out.println(paramValue); } else { out.println("<UL>"); for(int i=0; i<paramValues.length; i++) { out.println("<LI>" + paramValues[i]); } out.println("</UL>"); First, use getParameterNames() to retrieve an Enumeration of all form parameters. Then, iterate through each element within the Enumeration. Continued….

Servlet / Web Browser Communication I out.println("</TABLE>\n</BODY></HTML>"); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); doPost calls doGet(). Therefore the servlet will work just as well for HTTP POSTs or GETs. 8/27/2019 Servlet / Web Browser Communication I

Case Study: Resume Posting Service 8/27/2019 Servlet / Web Browser Communication I

Resume Posting Service Our next servlet receives a series of parameters: Name, title, email address, programming languages. Font, font size, etc. Based on these parameters, the user is able to post his/her resume online. Let’s first try it out… 8/27/2019 Servlet / Web Browser Communication I

Cascading Style Sheets The Resume servlet utilizes Cascading Style Sheets (CSS). We never covered CSS when covering HTML. But, we will cover the very basics right now. Let’s begin with a brief description of CSS. 8/27/2019 Servlet / Web Browser Communication I

Servlet / Web Browser Communication I CSS Defined CSS: a simple mechanism for adding style (e.g. fonts, colors, spacing) to Web documents. Two Step process for using CSS: Step 1: Create your “styles” Step 2: Apply your styles to your HTML document. Let’s look at an example… 8/27/2019 Servlet / Web Browser Communication I

First, you create your styles Within a <STYLE> tag. <HTML> <BODY> <STYLE TYPE="text/css"> <!-- .HEADING1 { color: blue; font-size: 64px; } .HEADING2 { color: gray; font-size: 22px; --> </STYLE> <SPAN CLASS="HEADING1">Resume Posting Service</SPAN> <P> <SPAN CLASS="HEADING2">Provided by hotcomputerjobs.com</SPAN> </BODY> </HTML> First, you create your styles Within a <STYLE> tag. Then, you apply your styles By using the SPAN tag.

Servlet / Web Browser Communication I Defining Styles Each Style has a name, and a set of properties. For example, the heading1 tag is set to blue, 64 pixels big: .HEADING1 { color: blue; font-size: 64px; } Lots of properties exist: color, font-size, text-align, font-family, etc. 8/27/2019 Servlet / Web Browser Communication I

Servlet / Web Browser Communication I Applying Styles Once you have created your styles, you apply a style to your text via the SPAN tag. For example, to apply the heading1 style: <SPAN CLASS="HEADING1">Resume Posting Service</SPAN> 8/27/2019 Servlet / Web Browser Communication I

Servlet / Web Browser Communication I Stylo-Matic Unfortunately, different browsers interpret CSS slightly differently. And, it therefore turns out to be problematic when developing cross-browser web pages. Let’s try a great little tool that exhibits some of the power of CSS: Stylo-Matic: http://www.builder.com/Authoring/CSS/ss12.html 8/27/2019 Servlet / Web Browser Communication I

Servlet / Web Browser Communication I SubmitResume.java Three major sections to SubmitResume.java Retrieve all the form parameters. Make the style sheet Output the HTML for the resume. We will examine each piece. For the full code, see today’s handout. 8/27/2019 Servlet / Web Browser Communication I

1.Retrieving Form Parameters First, the showPreview() method retrieves the form parameters. If a parameter is missing, we supply a default: String fgColor = request.getParameter("fgColor"); fgColor = replaceIfMissing(fgColor, "BLACK"); String bgColor = request.getParameter("bgColor"); bgColor = replaceIfMissing(bgColor, "WHITE"); 8/27/2019 Servlet / Web Browser Communication I

Servlet / Web Browser Communication I 2. Make the Style Sheet Based on the form parameters, we create an appropriate stylesheet via the makeStyleSheet() method: String styleSheet = "<STYLE TYPE=\"text/css\">\n" + "<!--\n" + ".HEADING1 { font-size: " + heading1Size + "px;\n" + " font-weight: bold;\n" + " font-family: " + headingFont + "Arial, Helvetica, sans-serif;\n" + "}\n" + …. 8/27/2019 Servlet / Web Browser Communication I

Servlet / Web Browser Communication I 3. Output the HTML The showPreview() method outputs SPAN tags plus resume data: … "<CENTER>\n"+ "<SPAN CLASS=\"HEADING1\">" + name + "</SPAN><BR>\n" + "<SPAN CLASS=\"HEADING2\">" + title + "<BR>\n" + "<A HREF=\"mailto:" + email + "\">" + email + "</A></SPAN>\n" + "</CENTER><BR><BR>\n" + 8/27/2019 Servlet / Web Browser Communication I