Multivalued parameters Some type of parameters may have more than one value. This is the case for the checkbox. What IDEs do you use? NetBeans Eclipse.

Slides:



Advertisements
Similar presentations
Chapter 6 Server-side Programming: Java Servlets
Advertisements

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.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 10 Servlets and Java Server Pages.
4 Copyright © 2005, Oracle. All rights reserved. Creating the Web Tier: Servlets.
8 Copyright © 2005, Oracle. All rights reserved. Creating the Web Tier: JavaServer Pages.
JSP and Servelets.
CGI programming. Common Gateway Interface interface between web server and other programs (cgi scripts) information passed as environment variables passed.
M-V-C for web applications. Model for Web Applications model consists of data and system state database tables –persistent data session information –current.
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.
Servlets, JSP and JavaBeans Joshua Scotton.  Getting Started  Servlets  JSP  JavaBeans  MVC  Conclusion.
Chapter 61 Processing the Client Request JavaServer Pages By Xue Bai.
Exercises of the Tutorial on Advanced Web Programming Authors: Miroslava Mitrovic Dragan Milicev Nino.
MOD III. Input / Output Streams Byte streams Programs use byte streams to perform input and output of 8-bit bytes. This Stream handles the 8-bit.
Cookie in a servlet. Cookies are small bits of textual information that a Web server sends to a browser and that the browser returns unchanged when visiting.
1 Servlets Based on Notes by Dave Hollinger & Ethan Cerami Also, the Online Java Tutorial by Sun.
Apache Tomcat as a container for Servlets and JSP
Java Server Pages (JSP)
Java Servlets Java Server Pages (JSP)
Chapter 25 JavaServer Pages and Servlets. CHAPTER GOALS To implement dynamic web pages with JavaServer Pages technology To learn the syntactical elements.
 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.
Data Structures Lecture 2 Fang Yu Department of Management Information Systems National Chengchi University Fall 2011.
1 Servlets: Leftover Odds and Ends (Most apply to JSPs as well, duh….) Representation and Management of Data on the Internet, 2007 CS Department, HUJI.
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.
CS320 Web and Internet Programming Handling HTTP Requests Chengyu Sun California State University, Los Angeles.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 34 Servlets.
Java Server Pages Russell Beale. What are Java Server Pages? Separates content from presentation Good to use when lots of HTML to be presented to user,
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.
CSCI 6962: Server-side Design and Programming History and Background.
Chapter 10 Servlets and Java Server Pages. A servlet is a Java class designed to be run in the context of a special servlet container An instance of the.
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.
Advance Database Management Systems Lab no. 5 PHP Web Pages.
Server-side Scripting Powering the webs favourite services.
Copyright © 2003 Pearson Education, Inc. Slide 8-1 The Web Wizard’s Guide to PHP by David Lash.
CSCI 6962: Server-side Design and Programming Support Classes and Shopping Carts.
Servlets. - Java technology for Common Gateway Interface (CGI) programming. - It is a Java class that dynamically extends the function of a web server.
Java Servlets and Java Server Pages Carol Wolf Computer Science.
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.
Li Tak Sing COMPS311F. Static attributes in Servlets Since Servlets are also Java classes, you can also use static attributes to store values that can.
Java Servlets example using NetBeans 6. Pre-requirements: Install Java JDK 1.6 Install NetBeans IDE 6 (we will use version NetBeans IDE update 16)
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.
Chapter 6 Server-side Programming: Java Servlets
HTML FORMS GET/POST METHODS. HTML FORMS HTML Forms HTML forms are used to pass data to a server. A form can contain input elements like text fields, checkboxes,
Server-side Programming The combination of –HTML –JavaScript –DOM is sometimes referred to as Dynamic HTML (DHTML) Web pages that include scripting are.
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.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 21 Java Servlets Wed. 11/22/00 based on material.
HTTP protocol Java Servlets. HTTP protocol Web system communicates with end-user via HTTP protocol HTTP protocol methods: GET, POST, HEAD, PUT, OPTIONS,
Bayu Priyambadha, S.Kom. Static content  Web Server delivers contents of a file (html) 1. Browser sends request to Web Server 3. Web Server sends HTML.
HTML FORM AND PHP IST 210: Organization of Data IST210 1.
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.
HTML Tables Tables are defined with the tag. A table is divided into rows (with the tag), and each row is divided into data cells (with the tag). td stands.
Java Servlets References: Karen Anewalt, Mary Washington College.
Programming with Java Lecture 6 Elements of a Java Servlet
Servlets.
JDBC & Servlet CSE 4504/6504 Lab.
Chapter 6 Server-side Programming: Java Servlets
Pre-assessment Questions
Servlets.
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.
Servlets Servlets are modules that extend the functionality of a “java-enabled” web-server They normally generate HTML code and web content dynamically.
Multivalued parameters
Multivalued parameters
Servlets Servlets are modules that extend the functionality of a “java-enabled” web-server They normally generate HTML code and web content dynamically.
Retrieving All parameters when names are not known
Presentation transcript:

Multivalued parameters Some type of parameters may have more than one value. This is the case for the checkbox. What IDEs do you use? NetBeans Eclipse JavaWorkShop J++ Cafe' This HTML code will genrate this output on the web-browser

“ide” will be a mutivalued parameter The parameter “ide” will receive as many values as options are selected (from 0 to 5 in this case) What IDEs do you use? NetBeans Eclipse JavaWorkShop J++ Cafe' These are the values the Parameter “ide” will receive If the option is selected

Retrieving the selected values The selected values are retrieved in a String array with the following method getParameterValues( ) applied on the request parameter of the get or put method. The signature is: String[] getParamterValues(String) String[] values = request.getParameterValues(“ide”); for (int i = 0; i < values.length; i++) out.println(i+” “+values[i]); This will print 1- NB 2- EX and 3- JP in this case

Retrieving All parameters when names are not known Sometimes the programmer may not know the names of all parameters which will be passed to the Servlet For example when the page was dynamically generated by another servlet according to the results of a query in database or the content of a file In these cases we can use the getPatameterNames() method applied to the request parameter, which will return an object of the Enumeration class Enumeration en = request.getParameterNames()

Using the Enumeration Object To retrieve the value of the unknown we can iterate over the Enumeration object using the nextElement() and hasMoreElement() methods Enumeration en = request.getParameterNames(); while(en.hasMoreElements()) { String parameterName = (String)en.nextElement(); String parameterValue = request.getParameter(parameterName); out.println(“parametro “+parameterName+ “tiene valor “+parameterValue); }

Example: products in a file The information about products to buy is stored in a file. In each line there is a product code, price and description (separated with a space). A first servlet should show this and allow users to specify a quantity to buy After the user specifies the quantities, presses a button to see the total After pressing the button, a second servlet will receive the information and process it to show the total amount the user has to pay

Processing workflow Products.txt motores escoba lapiz goma papel OrderPage ProcessPage

Auxiliary classes: Product public class Product { public String code; public int price; public String desc; Product(String x, String y, int z){ code = x; desc = y; price = z; } First, a class for storing th information about the products

Auxiliary classes: Item import java.util.Hashtable; import java.io.*; import java.util.*; public class Item { static public Hashtable getItems() throws Exception { Hashtable v = new Hashtable(); String l; Product p; p = new Product("1111","Honda",250); v.put("1111",p); p = new Product("2222","Mitsubishi",340); v.put("2222",p); p = new Product("4444","BMW",280); v.put("4444",p); p = new Product("5555","Mercedes",400); v.put("5555",p); p = new Product("6666","Ferrari",405); v.put("6666",p); return v; }

doGet of OrderPage protected void processRequest(HttpServletRequest request, …. PrintWriter out = response.getWriter(); Hashtable h = Item.getItems(); response.setContentType("text/html"); out.print(" Make your order \n" + " " + " Product Number Product Name Price Number"); Enumeration keys = h.keys(); out.print(" "); while(keys.hasMoreElements()) { String k = (String)keys.nextElement(); Product e = (Product)h.get(k); out.print(" "); out.print(" " + e.code); out.print(" " + e.desc ); out.print(" " + e.price+" "); out.print(" "); } out.println(" "); out.close(); } Gets the hashtable with the products by calling the getItems method of the Item class

doGet of OrderPage protected void processRequest(HttpServletRequest request, …. PrintWriter out = response.getWriter(); Hashtable h = Item.getItems(); response.setContentType("text/html"); out.print(" Make your order \n" + " " + " Product Number Product Name Price Number"); Enumeration keys = h.keys(); out.print(" "); while(keys.hasMoreElements()) { String k = (String)keys.nextElement(); Product e = (Product)h.get(k); out.print(" "); out.print(" " + e.code); out.print(" " + e.desc ); out.print(" " + e.price+" "); out.print(" "); } out.println(" "); out.close(); } Title and header of the table

doGet of OrderPage protected void processRequest(HttpServletRequest request, …. PrintWriter out = response.getWriter(); Hashtable h = Item.getItems(); response.setContentType("text/html"); out.print(" Make your order \n" + " " + " Product Number Product Name Price Number"); Enumeration keys = h.keys(); out.print(" "); while(keys.hasMoreElements()) { String k = (String)keys.nextElement(); Product e = (Product)h.get(k); out.print(" "); out.print(" " + e.code); out.print(" " + e.desc ); out.print(" " + e.price+" "); out.print(" "); } out.println(" "); out.close(); } Get the keys from the hashtable (codes of products)

doGet of OrderPage public void doGet(.. request,... response) throws... { protected void processRequest(HttpServletRequest request, …. PrintWriter out = response.getWriter(); Hashtable h = Item.getItems(); response.setContentType("text/html"); out.print(" Make your order \n" + " " + " Product Number Product Name Price Number"); Enumeration keys = h.keys(); out.print(" "); while(keys.hasMoreElements()) { String k = (String)keys.nextElement(); Product e = (Product)h.get(k); out.print(" "); out.print(" " + e.code); out.print(" " + e.desc ); out.print(" " + e.price+" "); out.print(" "); } out.println(" "); out.close(); } Iterate over the Enumeration object to obtain each element (a Product object)

doGet of OrderPage protected void processRequest(HttpServletRequest request, …. PrintWriter out = response.getWriter(); Hashtable h = Item.getItems(); response.setContentType("text/html"); out.print(" Make your order \n" + " " + " Product Number Product Name Price Number"); Enumeration keys = h.keys(); out.print(" "); while(keys.hasMoreElements()) { String k = (String)keys.nextElement(); Product e = (Product)h.get(k); out.print(" "); out.print(" " + e.code); out.print(" " + e.desc ); out.print(" " + e.price+" "); out.print(" "); } out.println(" "); out.close(); } Show each field (number, name, price) Of the product in a table row

doGet of OrderPage public void doGet(.. request,... response) throws... { out.print("<input type=text name="+e.code+" value=0 >"); } This puts at the end of the row a text input Its name will be the number (code) of the product

The name of the input field is the product’s code

Now lets generate the following page with ProcessOrder servlet

doGet of ProcessOrder public void doGet(.. request,... response) throws... { PrintWriter out = response.getWriter(); Hashtable h = Item.getItems(); response.setContentType("text/html"); out.print(" Your choice was \n" + " " + " Product Number Product Name Price Total"); Enumeration en = request.getParameterNames(); int total = 0; out.print(" "); while(en.hasMoreElements()) { String number = (String)en.nextElement(); String qtty = request.getParameter(number); int nqtty = Integer.parseInt(qtty); Product e = (Product)h.get(number); out.print(" " + e.code+" " + e.desc ); out.print(" " + e.price+" "+e.price*nqtty); total = total+e.price*nqtty; } out.println(" FINAL TOTAL "+total); out.println(" "); }

The same with checkbox public void doGet(.. request,... response) throws... { Hashtable items = Item.getItems(); response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.print(" Make your order \n" + " " + " Product Number Product Name Price Number"); Enumeration enum = h.getKeys(); out.print(" "); while(enum.hasMoreElements()) { Product e = (Product)enum.nextElement(); out.print(" "); out.print(" " + e.number); out.print(" " + e.name ); out.print(" " + e.price+" "); out.print("<input type=checkbox SIZE=3 "+ out.print(" name=selection value="+e.number+" >"); } out.println(" "); }

Selection means buy only 1 item Make your order Product Number Product Name Price Number 1111 motores escoba lapiz goma papel 322

ProcessOrder for checkbox public void doGet(.. request,... response) throws... { Hashtable items = Item.getItems(); response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.print(" Your choice was \n" + " " + " Product Number Product Name Price Total"); String[] values = request.getParameterValues(“selection”); out.print(" "); for (int i = 0; i < values.length; i++){ String number = values[i]; Product e = (Product)item.get(number); out.print(" " + e.number+" " + e.name ); out.print(" " + e.price+" “+e.price*nqtty); total = total*e.price*nqtty; } out.println(“ ”+total); out.println(" "); }