Java Servlets Servlet Overview Servlets and HTML Forms Servlet Basics

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.
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.
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.
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.
1 CS6320 – Servlet Structure and Lifecycle L. Grewe.
Comp2513 Java Servlet Basics Daniel L. Silver, Ph.D.
Java database Programming JDBC Trademarked name of a Java API that supports Java programs that access relational databases Stand for Java DataBase Connectivity.
Java Server and Servlet CS616 Team 9 Kim Doyle, Susan Kroha, Arunima Palchowdhury, Wei Xu.
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.
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.
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.
Java support for WWW Babak Esfandiari (sources: Qusay Mahmoud, Roger Impey, textbook)
CSC 2720 Building Web Applications
Chapter 5 Java Servlets. Objectives Explain the nature of a servlet and its operation Use the appropriate servlet methods in a web application Code the.
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.
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.
Java Servlets Lec 27. Creating a Simple Web Application in Tomcat.
Servlets Servlets are modules that extend the functionality of a “java-enabled” web-server They normally generate HTML code and web content dynamically.
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.
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.
 2002 Prentice Hall. All rights reserved. 9.8 Multi-Tier Applications: Using JDBC from a Servlet Three-tier distributed applications –User interface –Business.
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 Java Server Pages (JSP)
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 21 Java Servlets Wed. 11/22/00 based on material.
Java Servlets and Java Server Pages
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.
Distributed Web Systems Java Servlets Lecturer Department University.
Programming with Java Lecture 6 Elements of a Java Servlet
Introduction to Servlets
LECTURE 8 (ETCS-308) Subject teacher : Ms. Gunjan Beniwal
Servlets.
Principles of Software Development
PERTEMUAN 3.
Servlet Fudamentals.
Net-centric Computing
JDBC & Servlet CSE 4504/6504 Lab.
Session Tracking in Servlets
HTTP Servlet Overview Servlets are modules that extend request/response-oriented servers, such as Java-enabled web servers. For example, a servlet might.
Servlets.
Servlet.
Chapter 26 Servlets.
Distributed Computing, M. L. Liu
Jagdish Gangolly State University of New York at Albany
Servlets and JSP 20-Nov-18 servletsJSP.ppt.
Servlets CEN /28/2018 Copyright 2001 Ege Consulting, Inc.
Web Search Interfaces.
Servlets.
Servlets Servlets are modules that extend the functionality of a “java-enabled” web-server They normally generate HTML code and web content dynamically.
Servlets Servlets are modules that extend the functionality of a “java-enabled” web-server They normally generate HTML code and web content dynamically.
CS122B: Projects in Databases and Web Applications Winter 2019
Basic servlet structure
Servlets: Servlet / Web Browser Communication I
Presentation transcript:

Java Servlets Servlet Overview Servlets and HTML Forms Servlet Basics Web Server Servlets and HTML Forms Name Address SSN Servlet Basics Using JDBC in Servlets offenders

Servlet Overview | | | | | A Servlet is server side program that services requests from a client and returns a result. Web Browser Servlet Web Server request response | | | | |

Servlet Overview | | | | | Servlet Web Browser Web Server request 1. The client (web browser) makes a request via HTTP. request 2. The Web server receives the request and forwards it the the servlet 3. The servlet will receive the request and perform some processing (database calls, etc) offenders response 4. The servlet will return a response back to the Web server response 5. The Web server will forward the response to the client Servlet | | | | | Web Browser Web Server

Some benefits of Servlets Servlet Overview Some benefits of Servlets Persistance – Servlets are loaded once by the Web server and can maintain services between requests. Platform independence – Because a Servlet is a Java program, it may be ported to a new operating system without changing the source code. Efficient – Since Servlets are loaded once by the Web server, they offer better performance over other server-side technologies (i.e. CGI)

Java Servlets Servlet Overview Servlets and HTML Forms Servlet Basics Web Server Servlets and HTML Forms Name Address SSN Servlet Basics Using JDBC in Servlets offenders

Writing a Servlet HelloWorld.java STEPS: Import the servlet packages import javax.servlet.*; import javax.servlet.http.*; import java.io.*; Create a class that extends HttpServlet public class HelloWorld extends HttpServlet { } Override the init( ) method if necessary Override the doGet( ) and/or the doPost( ) method public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } Open an output stream to the Browser response.setContentType(“text/html”); PrintWriter out = response.getWriter( ); Send the response out.println(“<html>”); out.println(“<head><title>Hello Servlet</title></head>”); out.println(“<body>Hello, World</body>”); out.println(“</html>”); out.close( );

| | | | | Web Browser Web Server import javax.servlet.*; import javax.servlet.http.*; import java.io.*; public class HelloWorld extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContent(“text/html”); PrintWriter out = response.getWriter( ); out.println(“<html>”); out.println(“<head><title>Hello Servlet</head></title>”); out.println(“<body>Hello, World</body>”); out.println(“</html>”); out.close( ); } Web Server | | | | | request response Web Browser

Writing the HTML to Invoke the Servlet testhello.htm <html> <head> <title>Test Hello World</title> </head> <body> <p>Press the button to invoke the Hello World Servlet</p> <form method="GET" action="/servlet/HelloWorld"> <p><input type="submit" value= "Test" name="B1"></p> </form> </body> </html> Sends a GET Request to the Servlet HelloWorld when the Button Test pressed.

Web Server | | | | request request response response import javax.servlet.*; import javax.servlet.http.*; import java.io.*; public class HelloWorld extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContent(“text/html”); PrintWriter out = response.getWriter( ); out.println(“<html>”); out.println(“<head><title>Hello Servlet</title></head>”); out.println(“<body>Hello, World</body>”); out.println(“</html>”); out.close( ); }

Java Servlets Servlet Overview Servlets and HTML Forms Servlet Basics Web Server Servlets and HTML Forms Name Address SSN Servlet Basics Using JDBC in Servlets offenders

Processing HTML Forms * Use the HttpServletRequest object to retrieve the data the user entered into an HTML Form. The HttpServletRequest object is passed as the first parameter to the doGet( ) and doPost( ) methods: public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { * } The HttpServletRequest object has three methods use to retrieve form data. They are: public String getParameter(String name); // returns a string containing the value of the parameter public String[ ] getParameterValues(String name); // returns the values (array) for the parameter public Enumeration getParameterNames( ); // returns the parameters name for the request

Let’s build a servlet that will collect the data in this form and display it in a Web page. clientinfo.htm <html> <head> <title>Collect Client Information</title> </head> <body> <p>Client Information</p> <form method="GET" action="/servlet/ClientInfoServlet"> <p>First Name: <input type="text" name="firstname" size="20"></p> <p>Last Name: <input type="text" name="lastname" size="20"></p> <p>SSN: <input type="text" name="ssn" size="25"></p> <p><input type="submit" value="Submit" name="B1"></p> </form> </body> </html> When the user presses the Submit button, the form is sent to the servlet ClientInfoServlet.

Import the servlet packages import javax.servlet.*; import javax.servlet.http.*; import java.io.*; public class ClientInfoServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String fn, ln, ssn; response.setContentType(“text/html”); PrintWriter out = response.getWriter( ); fn = request.getParameter(“firstname”); ln = request.getParameter(“lastname”); ssn = request.getParameter(“ssn”); out.println(“<html><head><title>Client Info Servlet</title></head>”); out.println(“<body>”); out.println("Here is the information you entered:"); out.println(“<p>”); out.println(fn); out.println(“</p>”); out.println(ln); out.println(ssn); out.println(“</body></html>”); out.close( ); } Import the servlet packages Create a class that extends HttpServlet Override the doGet( ) and/or the doPost( ) method Open an output stream to the Browser Get the value for the form parameter named firstname. Get the value for the form parameter named lastname. Get the value for the parameter ssn. Send the response

| | | | Web Server Retrieves the values that were typed in the form request firstname=jack lastname=sprat ssn=123-45-6789 firstname=jack lastname=sprat ssn=123-45-6789 request import javax.servlet.*; import javax.servlet.http.*; import java.io.*; public class ClientInfoServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String fn, ln, ssn; res.setContentType(“text/html”); PrintWriter out = res.getWriter( ); fn = req.getParameter(“firstname”); ln = req.getParameter(“lastname”); ssn = req.getParameter(“ssn”); ~~~~~~~~~~~~~~~ } Retrieves the values that were typed in the form

Web Server | | | | response response ~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~ out.println(“<html><head><title>Client Info Servlet</title></head>”); out.println(“<body>”); out.println("Here is the information you entered:"); out.println(“<p>”); out.println(fn); out.println(“</p>”); out.println(ln); out.println(ssn); out.println(“</body></html>”); out.close( ); } Construct HTML and send it to the user’s Web Browser

Java Servlets Servlet Overview Servlets and HTML Forms Servlet Basics Web Server Servlets and HTML Forms Name Address SSN Servlet Basics Using JDBC in Servlets offenders

Servlets and JDBC | | | | | Servlet Web Browser Web Server A Servlet that uses JDBC is not much different from any application that uses JDBC. One difference is how the results are displayed. Instead of writing the results to the standard output device (console), we will generate HTML that will be sent back to the client. Here are the steps: 1. The user enters information into an HTML form, and the form data is sent to the Servlet via Web Server. 2. The Servlet parses the form data and constructs an SQL statement. The statement is passed to the database server using JDBC. 3. The database server executes the SQL and returns a result set to the Servlet. 4. The Servlet processes the result set and constructs an HTML page with the data. The HTML page is then returned to the user’s Web browser. Web Browser Servlet Web Server request response | | | | | clients

Lets look at a Servlet that allows a user to search for client records in database. The search web page allows the user to show all clients in the database, or . . .

. . . Search by providing an ID.

| | | | | ClientSearchServlet Web Browser Web Server request response

| | | | | ClientSearchServlet Web Browser Web Server request response

** Below is the HTML to invoke the servlet findclient.htm <head> <title>Find Clients</title> </head> <body> <p align="center">Find Client</p> <form method="GET" action="/servlet/ClientSearchServlet"> <p>Client ID: <input type="text" name="clientid" size="20"></p> <p><input type="submit" value="Find Client By ID" name="searchtype"> <input type="submit" value="Show All Clients" name="searchtype"></p> </form> <p> </p> </body> </html>

ClientSearchServlet.java Import the servlet and jdbc packages import javax.servlet.*; import javax.servlet.http.*; import java.io.*; import java.sql.*; public class ClientSearchServlet extends HttpServlet { public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { String searchtype,clientid; Statement st; ResultSet rs; String query = "select * from clientinfo"; Connection DBConnection; res.setContentType("text/html"); PrintWriter out = res.getWriter( ); searchtype = req.getParameter("searchtype"); clientid = req.getParameter("clientid"); if( searchtype.equals("Find Client By ID") ) { query = query + " where id = "+clientid; } Create a class that extends HttpServlet Override the doGet( ) and/or the doPost( ) method Open an output stream to the Browser Retrieve the form data

Get a connection to the database out.println("<html><head><title>Client Search</title></head>"); out.println("<body>"); out.println("<p>Clients</p>"); out.println("<table border=\"1\” > <tr>"); out.println("<td>ID</td>"); out.println("<td>First Name</td>"); out.println("<td>Last Name</td>"); out.println("<td>SSN</td></tr>"); try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); String url = "jdbc:odbc:clients"; DBConnection = DriverManager.getConnection(url); st = DBConnection.createStatement(); rs = st.executeQuery(query); while(rs.next() ) { out.println("<td>"+ rs.getString("id") + "</td>"); out.println("<td>" + rs.getString("firstname") + "</td>"); out.println("<td>"+ rs.getString("lastname") +"</td>"); out.println("<td>"+ rs.getString("ssn") +"</td></tr>"); } out.println("</body></html>"); out.close( ); } catch(Exception e) { e.printStackTrace(); } Get a connection to the database Create a statement Execute the statement Retrieve rows, construct HTML, and send it to the user’s Web Browser