Retrieving All parameters when names are not known

Slides:



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

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.
Session Tracking Honjo-Waseda Lets try this servlet (Get) public class Compute extends HttpServlet { int op1, op2; protected void doGet(request,
Video, audio, embed, iframe, HTML Form
Introduction to Servlets Based on: Hall, Brown, Core Servlets and JavaServer Pages.
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.
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. 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.
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 an array.
Servlets. Our Project 3-tier application Develop our own multi-threaded server Socket level communication.
CSCI 6962: Server-side Design and Programming History and Background.
Gayle J Yaverbaum, PhD Professor of Information Systems Penn State Harrisburg.
XML for E-commerce IV Helena Ahonen-Myka. In this part... n Some solutions for delivering dynamic content n Example of using XML.
Advance Database Management Systems Lab no. 5 PHP Web Pages.
Examples of Using Servlets and JSP Representation and Management of Data on the Internet.
Servlets. - Java technology for Common Gateway Interface (CGI) programming. - It is a Java class that dynamically extends the function of a web server.
111 Java Servlets Dynamic Web Pages (Program Files) Servlets versus Java Server Pages Implementing Servlets Example: F15 Warranty Registration Tomcat Configuration.
CGS – 4854 Summer 2012 Web Site Construction and Management Instructor: Francisco R. Ortega Chapter 2.
Mark Dixon 1 09 – Java Servlets. Mark Dixon 2 Session Aims & Objectives Aims –To cover a range of web-application design techniques Objectives, by end.
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.
Server-side Programming The combination of –HTML –JavaScript –DOM is sometimes referred to as Dynamic HTML (DHTML) Web pages that include scripting are.
JSP Pages. What and Why of JSP? JSP = Java code imbedded in HTML or XML –Static portion of the page is HTML –Dynamic portion is Java Easy way to develop.
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.
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.
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.
Programming with Java Lecture 6 Elements of a Java Servlet
JSP java server pages.
Introduction to Servlets
Java Class and Servlet ISYS 350.
Web Programming: If Statement and Servlet
Principles of Software Development
How to Write Web Forms By Mimi Opkins.
Developing JavaServer Pages
Net-centric Computing
JDBC & Servlet CSE 4504/6504 Lab.
CS3220 Web and Internet Programming HTML Tables and Forms
Servlets Hits Counter 20-Jul-18.
Session Tracking in Servlets
Session Tracking Honjo-Waseda 2008.
HTML: Basic Tags & Form Tags
CGI Programming Part II UNIX Security
SERVLETS AND JDBC.
Servlets.
Java Server Pages.
Jagdish Gangolly State University of New York at Albany
Servlets and Java Server Pages
Handling FORM Data using Servlets
CS3220 Web and Internet Programming HTML Tables and Forms
Web Search Interfaces.
Java Servlet ISYS 350.
Web Search Interfaces by Ray Mooney
Servlets.
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.
Java Servlets Servlet Overview Servlets and HTML Forms Servlet Basics
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.
Parameters passed by client
Directories and DDs 25-Apr-19.
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.
Directories and DDs 21-Jul-19.
Servlets: Servlet / Web Browser Communication I
Directories and DDs 14-Sep-19.
HTML: Basic Tags & Form Tags
Presentation transcript:

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 111 34347 motores 222 760 escoba 333 123 lapiz 444 224 goma 555 322 papel OrderPage ProcessPage

Auxiliary classes: Product First, a class for storing th information about the products 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; }

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 Gets the hashtable with the products protected void processRequest(HttpServletRequest request, …. PrintWriter out = response.getWriter(); Hashtable h = Item.getItems(); response.setContentType("text/html"); out.print("<H2 ALIGN=CENTER> Make your order</H2>\n" + "<TABLE BORDER=1 ALIGN=CENTER><TR BGCOLOR=#FFAD00>" + "<TH>Product Number <TH>Product Name<TH>Price <TH> Number"); Enumeration keys = h.keys(); out.print("<form action=ProcessPage method='POST'>"); while(keys.hasMoreElements()) { String k = (String)keys.nextElement(); Product e = (Product)h.get(k); out.print("<TR>"); out.print("<TD>" + e.code); out.print("<TD>" + e.desc ); out.print("<TD>" + e.price+"<TD>"); out.print("<input type=text name="+e.code+" value=0 >"); } out.println("</TABLE>"); out.println("<INPUT TYPE='SUBMIT' VALUE='Process'>"); out.close(); Gets the hashtable with the products by calling the getItems method of the Item class

doGet of OrderPage Title and header of the table protected void processRequest(HttpServletRequest request, …. PrintWriter out = response.getWriter(); Hashtable h = Item.getItems(); response.setContentType("text/html"); out.print("<H2 ALIGN=CENTER> Make your order</H2>\n" + "<TABLE BORDER=1 ALIGN=CENTER><TR BGCOLOR=#FFAD00>" + "<TH>Product Number <TH>Product Name<TH>Price <TH> Number"); Enumeration keys = h.keys(); out.print("<form action=ProcessPage method='POST'>"); while(keys.hasMoreElements()) { String k = (String)keys.nextElement(); Product e = (Product)h.get(k); out.print("<TR>"); out.print("<TD>" + e.code); out.print("<TD>" + e.desc ); out.print("<TD>" + e.price+"<TD>"); out.print("<input type=text name="+e.code+" value=0 >"); } out.println("</TABLE>"); out.println("<INPUT TYPE='SUBMIT' VALUE='Process'>"); out.close(); Title and header of the table

doGet of OrderPage Get the keys from the hashtable (codes of products) protected void processRequest(HttpServletRequest request, …. PrintWriter out = response.getWriter(); Hashtable h = Item.getItems(); response.setContentType("text/html"); out.print("<H2 ALIGN=CENTER> Make your order</H2>\n" + "<TABLE BORDER=1 ALIGN=CENTER><TR BGCOLOR=#FFAD00>" + "<TH>Product Number <TH>Product Name<TH>Price <TH> Number"); Enumeration keys = h.keys(); out.print("<form action=ProcessPage method='POST'>"); while(keys.hasMoreElements()) { String k = (String)keys.nextElement(); Product e = (Product)h.get(k); out.print("<TR>"); out.print("<TD>" + e.code); out.print("<TD>" + e.desc ); out.print("<TD>" + e.price+"<TD>"); out.print("<input type=text name="+e.code+" value=0 >"); } out.println("</TABLE>"); out.println("<INPUT TYPE='SUBMIT' VALUE='Process'>"); out.close(); Get the keys from the hashtable (codes of products)

doGet of OrderPage Iterate over the Enumeration object to 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("<H2 ALIGN=CENTER> Make your order</H2>\n" + "<TABLE BORDER=1 ALIGN=CENTER><TR BGCOLOR=#FFAD00>" + "<TH>Product Number <TH>Product Name<TH>Price <TH> Number"); Enumeration keys = h.keys(); out.print("<form action=ProcessPage method='POST'>"); while(keys.hasMoreElements()) { String k = (String)keys.nextElement(); Product e = (Product)h.get(k); out.print("<TR>"); out.print("<TD>" + e.code); out.print("<TD>" + e.desc ); out.print("<TD>" + e.price+"<TD>"); out.print("<input type=text name="+e.code+" value=0 >"); } out.println("</TABLE>"); out.println("<INPUT TYPE='SUBMIT' VALUE='Process'>"); out.close(); Iterate over the Enumeration object to obtain each element (a Product object)

doGet of OrderPage Show each field (number, name, price) protected void processRequest(HttpServletRequest request, …. PrintWriter out = response.getWriter(); Hashtable h = Item.getItems(); response.setContentType("text/html"); out.print("<H2 ALIGN=CENTER> Make your order</H2>\n" + "<TABLE BORDER=1 ALIGN=CENTER><TR BGCOLOR=#FFAD00>" + "<TH>Product Number <TH>Product Name<TH>Price <TH> Number"); Enumeration keys = h.keys(); out.print("<form action=ProcessPage method='POST'>"); while(keys.hasMoreElements()) { String k = (String)keys.nextElement(); Product e = (Product)h.get(k); out.print("<TR>"); out.print("<TD>" + e.code); out.print("<TD>" + e.desc ); out.print("<TD>" + e.price+"<TD>"); out.print("<input type=text name="+e.code+" value=0 >"); } out.println("</TABLE>"); out.println("<INPUT TYPE='SUBMIT' VALUE='Process'>"); out.close(); Show each field (number, name, price) Of the product in a table row

doGet of OrderPage name="+e.code+" value=0 >"); 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("<H2 ALIGN=CENTER> Your choice was</H2>\n" + "<TABLE BORDER=1 ALIGN=CENTER><TR BGCOLOR=#FFAD00>" + "<TH>Product Number <TH>Product Name<TH>Price <TH> Total"); Enumeration en = request.getParameterNames(); int total = 0; out.print("<form action=ProcessPayment method='POST'>"); 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("<TR> <TD>" + e.code+"<TD>" + e.desc ); out.print("<TD>" + e.price+"<TD>"+e.price*nqtty); total = total+e.price*nqtty; } out.println("<TR> <TD> FINAL TOTAL<TD> <TD> <TD>"+total); out.println("</TABLE>"); out.println("<INPUT TYPE='SUBMIT' VALUE='Process'>");

The same with checkbox public void doGet( .. request, ... response) throws . . . { Hashtable items = Item.getItems(); response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.print("<H2 ALIGN=CENTER> Make your order</H2>\n" + "<TABLE BORDER=1 ALIGN=CENTER><TR BGCOLOR=#FFAD00>" + "<TH>Product Number <TH>Product Name<TH>Price <TH> Number"); Enumeration enum = h.getKeys(); out.print("<form action=ProcessPage method='POST'>"); while(enum.hasMoreElements()) { Product e = (Product)enum.nextElement(); out.print("<TR>"); out.print("<TD>" + e.number); out.print("<TD>" + e.name ); out.print("<TD>" + e.price+"<TD>"); out.print("<input type=chackbox SIZE=3 "+ out.print(" name=selection value="+e.number+" >"); } out.println("</TABLE>"); out.println("<INPUT TYPE='SUBMIT' VALUE='Process'>");

Selection means buy only 1 item <H2 ALIGN=CENTER> Make your order</H2> <TABLE BORDER=1 ALIGN=CENTER><TR BGCOLOR=#FFAD00> <TH>Product Number <TH>Product Name<TH>Price <TH> Number <TR><TD> 1111 <TD> motores <TD> 34347 <TD> <input type=checkbox name=selection value=1111 > <TR><TD> 2222 <TD> escoba <TD> 760 <TD> <input type=checkbox name=selection value=2222 > <TR><TD> 3333 <TD> lapiz <TD> 1237 <TD> <input type=checkbox name=selection value=3333 > <TR><TD> 4444 <TD> goma <TD> 224 <TD> <input type=checkbox name=selection value=4444 > <TR><TD> 5555 <TD> papel <TD> 322 <TD> <input type=checkbox name=selection value=5555 >

ProcessOrder for checkbox public void doGet( .. request, ... response) throws . . . { Hashtable items = Item.getItems(); response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.print("<H2 ALIGN=CENTER> Your choice was</H2>\n" + "<TABLE BORDER=1 ALIGN=CENTER><TR BGCOLOR=#FFAD00>" + "<TH>Product Number <TH>Product Name<TH>Price <TH> Total"); String[] values = request.getParameterValues(“ide”); out.print("<form action=ProcessPayment method='POST'>"); for (int i = 0; i < values.length; i++){ String number = values[i]; Product e = (Product)item.get(number); out.print("<TR> <TD>" + e.number+"<TD>" + e.name ); out.print("<TD>" + e.price+"<TD>“+e.price*nqtty); total = total*e.price*nqtty; } out.println(“<TR> <TD> <TD> <TD> <TD>”+total); out.println("</TABLE>"); out.println("<INPUT TYPE='SUBMIT' VALUE='Process'>");