Servlets.

Slides:



Advertisements
Similar presentations
Java Server Pages (JSP)
Advertisements

 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.
Video, audio, embed, iframe, HTML Form
1 CS6320 – Servlet Request Dispatcher L. Grewe 2 What is the purpose Forward a request from one servlet to another (or jsp). Forward a request from one.
Forms Review. 2 Using Forms tag  Contains the form elements on a web page  Container tag tag  Configures a variety of form elements including text.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 34 Servlets.
June 1, 2000 Object Oriented Programming in Java (95-707) Advanced Topics 1 Lecture 10 Object Oriented Programming in Java Advanced Topics Servlets.
1 Static Web Pages Websites on Servers (The Big Picture) –Apache Tomcat can support static web pages –Primarily intended to support servlets and JSP –Some.
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.
Michael Brockway Application Integration Servlets l Introduction & Overview l HTTP Servlets l HTTP get Requests l HTTP post Requests l Multi-tier Applications.
Web Development & Design Foundations with XHTML Chapter 9 Key Concepts.
Chapter 10 Form Basics Key Concepts Copyright © 2013 Terry Ann Morris, Ed.D 1.
Java Server Pages B.Ramamurthy. Topics for Discussion 8/20/20152 Inheritance and Polymorphism Develop an example for inheritance and polymorphism JSP.
1 Web Developer & Design Foundations with XHTML Chapter 6 Key Concepts.
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.
1 HTML References: A HTML Tutorial: /HTMLPrimer.html
CSC 2720 Building Web Applications
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.
CSC 2720 Building Web Applications HTML Forms. Introduction  HTML forms are used to collect user input.  The collected input is typically sent to a.
Web Server Programming 1. Nuts and Bolts. Premises of Course Provides general introduction, no in-depth training Assumes some HTML knowledge Assumes some.
Week 9 - Form Basics Key Concepts 1. 1.Describe common uses of forms on web pages 2.Create forms on web pages using the form, input, textarea, and select.
Copyright © Terry Felke-Morris WEB DEVELOPMENT & DESIGN FOUNDATIONS WITH HTML5 7 TH EDITION Chapter 9 Key Concepts 1 Copyright © Terry Felke-Morris.
20-Nov-15introServlets.ppt Intro to servlets. 20-Nov-15introServlets.ppt typical web page – source Hello Hello.
HTML Forms a form is a container for input elements on a web page input elements contain data that is sent to the web server for processing.
Introduction to Server-Side Web Development Introduction to Server-Side Web Development Session II: Introduction to Server-Side Web Development with Servlets.
Copyright © Terry Felke-Morris WEB DEVELOPMENT & DESIGN FOUNDATIONS WITH HTML5 Chapter 9 Key Concepts 1 Copyright © Terry Felke-Morris.
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.
1 Introduction to Servlets. Topics Web Applications and the Java Server. HTTP protocol. Servlets 2.
+ FORMS HTML forms are used to pass data to a server. begins and ends a form Forms are made up of input elements Every input element has a name and value.
©SoftMooreSlide 1 Introduction to HTML: Forms ©SoftMooreSlide 2 Forms Forms provide a simple mechanism for collecting user data and submitting it 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,
Introduction To HTML Dr. Magdi AMER. HTML elements.
Internet & World Wide Web How to Program, 5/e Copyright © Pearson, Inc All Rights Reserved.
HTML III (Forms) Robin Burke ECT 270. Outline Where we are in this class Web applications HTML Forms Break Forms lab.
Lesson 5 Introduction to HTML Forms. Lesson 5 Forms A form is an area that can contain form elements. Form elements are elements that allow the user to.
Java Servlets References: Karen Anewalt, Mary Washington College.
Introduction to Servlets
CHAPTER 2 MARKUP LANGUAGE
How to Write Web Forms By Mimi Opkins.
Servlet Fudamentals.
Introduction to HTML Forms and Servlets
JDBC & Servlet CSE 4504/6504 Lab.
CS3220 Web and Internet Programming HTML Tables and Forms
Java Servlets.
Introduction to Web programming
FORMS Explained By: Sarbjit Kaur.
HTML IV (Forms) Robin Burke ECT 270.
Pre-assessment Questions
HTML/XHTML Forms 18-Sep-18.
Sessions.
Introducing Forms.
HTML: Basic Tags & Form Tags
Chapter 26 Servlets.
Distributed Computing, M. L. Liu
Java Server Pages.
Web Development & Design Foundations with H T M L 5
Forms, cont’d.
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.
CS3220 Web and Internet Programming HTML Tables and Forms
Web Search Interfaces.
J2EE Lecture 1:Servlet and JSP
HTML Forms 18-Apr-19.
Java Servlets Servlet Overview Servlets and HTML Forms Servlet Basics
Lesson 6: Web Forms.
HTML: Basic Tags & Form Tags
Presentation transcript:

Servlets

Contents Introduction to HTML HTML Tables and Forms HttpServlet Class doGet method doPost method Generating and Processing Dynamic Forms with Java Servlets

Hypertext Markup Language (HTML) HTML documents consist of ordinary text and “tags”. Tags were originally designed to specify the structure of a document -- not its format. A tag might, for example, specify that some text is a “level 1 heading” rather than specify the font and style.

HTML Document Structure <head> <title>Example 1</title> </head> <body> <h1><center>Example 1</center></h1> <p>The first line of the first paragraph. <br /> The second line.</p> <p>The second paragraph.</p> <a href=‘some_url’>click this link</a> </body> </html>

HTML Tables <table border=‘1’> <tr> <th>Name</th> <th>Phone Number</th> </tr> <td>Bergstein</td> <td>999-9184</td> <td>Bergandy</td> <td>999-8293</td> </table>

HTML Forms <form action=‘url’ method=‘POST’> ⋮ </form> Forms may contain input elements: <input type=‘xxx’ name=‘yyy’ value=‘zzz’> When a form is submitted, name and value pairs are sent to the web server. The names are used to retrieve values on the server side. The exact meaning of name and value depends on the type of the control.

type=‘text’ Text field where users can edit text. The value, if specified, is the initial text to display. The name can be used to retrieve the text entered by the user.

type=‘password’ Same as type=‘text’, except that * characters are displayed in the text field instead of the actual text.

type=‘checkbox’ Only name/value pairs of checkboxes that are checked are submitted with the form. Several checkboxes with different values may have the same name. On the server, the result may be a parameter with multiple values.

type=‘radio’ Radio buttons with the same name are grouped, such that only one per group may be selected. When the form is submitted, there will only be one value for the parameter name.

type=‘submit’ A button that causes the form to be submitted. The value is the label on the button. If a submit button is clicked, the name/value pair for the button is submitted with the form. We might give several submit buttons the same name and test the value on the server side to determine which button was clicked.

Other input types Reset File Hidden Image Button

Form Elements Form elements may contain text and markup in addition to input elements. Forms specify an action and a method to be used for submitting the form. The action is a url, where the form should be sent. If the method is “GET”, the form data is sent as part of the url. If it is “POST” the data is sent as part of the HTTP request.

Order Form (1)

Order Form (2) <html> <head> <title>Form Example</title> </head> <body bgcolor='#FFFFFF' text='#000000'> <form action='myFormServlet' method='POST'> <b>Select Items</b><br> <input type='checkbox' name='items' value='monitor'>Monitor <br> <input type='checkbox' name='items' value='keyboard'>Keyboard <br> <input type='checkbox' name='items' value='mouse'>Mouse <br> <input type='checkbox' name='items' value='speakers'>Speakers <br>

Order Form (3) <p><b>Choose Color</b><br> <input type='radio' name='color' value='red' checked>Red <br> <input type='radio' name='color' value='orange'>Orange <br> <input type='radio' name='color' value='blue'>Blue <br> <input type='radio' name='color' value='yellow'>Yellow <br> <input type='radio' name='color' value='purple'>Purple <br> <p><b>Credit Card Number</b> <input type='text' name='cc_num' size='16' maxlength='16'><br> <p><input type='submit' name='submit' value='Submit Order'> </form> </body> </html> Order Form (3)

HttpServlet Class Servlets generally extend HttpServlet and override the doGet and/or doPost methods. A web server may be configured so that certain url’s map to servlets instead of static files. When a web server receives a GET or POST request for a url that maps to a servlet, it invokes the servlets doGet or doPost method, respectively.

doGet and doPost Parameters public void doGet(HttpServletRequest req, HttpServletResponse res) public void doPost(HttpServletRequest req, HttpServletResponse res) The HttpServletRequest can be used to get information that was sent from the browser to the server. The HttpServletResponse is used to send information back to the browser.

HttpServletRequest Methods To get the current session, or create a new one public HttpSession getSession(); To get the value of a parameter public String getParameter(String name); To get the values of a multivalued parameter, or null if the parameter does not exist. public String[ ] getParameterValues(String name); To get a RequestDispatcher for forwarding or including. (If path starts with “/” it is relative to context root, otherwise relative to the request of calling servlet.) public RequestDispatcher getRequestDispatcher(String path)

More HttpServletRequest Methods (also in HttpSession) public void setAttribute (String name, Object value) public Object getAttribute (String name) public void removeAttribute (String name)

RequestDispatcher Methods public void forward(ServletRequest req, ServletResponse res) public void include(ServletRequest req, ServletResponse res) If the resource is dynamic, its output is included in the response.

HttpServletResponse Methods public void setContentType(String type); public ServletOutputStream getOutputStream(); public PrintWriter getWriter(); public String encodeRedirectURL(String url); public void sendRedirect(String url);

Form Generation Example public void doGet(HttpServletRequest req, HttpServletResponse res) { res.setContentType(“text/html”); PrintWriter out = res.getWriter(); out.println(“<html><body>”); out.println(“<form action=‘myServlet’, “ + “method=‘POST’>”); out.println(“<p><b>Choose a Color</b> <br>”); // Query db for colors currently in stock ResultSet results = stmt.executeQuery(“select * from colors”);

Form Generation Example (2) String prefix = “<input type=‘radio’, “ + “name=‘color’, value=‘”; while (results.next()) { String color = results.getString(1); out.println(prefix + color + “’>” + color + “ <br>”); } // … out.println(“</form> </body> </html>”);

Form Processing Example public void doPost(HttpServletRequest req, HttpServletResponse res) { String credit_card = req.getParameter(“cc_num”); String color = req.getParameter(“color”); String[] items = req.getParameterValues(“items”); // Store the form data in a database, // or whatever … // Redirect the browser to the next page String url = res.encodeRedirectURL(“thankYou.html”); res.sendRedirect(url); }