Presentation is loading. Please wait.

Presentation is loading. Please wait.

Java Servlets Servlet Overview Servlets and HTML Forms Servlet Basics

Similar presentations


Presentation on theme: "Java Servlets Servlet Overview Servlets and HTML Forms Servlet Basics"— Presentation transcript:

1 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

2 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 | | | | |

3 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

4 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)

5 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

6 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( );

7 | | | | | 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

8 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.

9 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( ); }

10 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

11 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

12 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.

13 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

14 | | | | Web Server Retrieves the values that were typed in the form
request firstname=jack lastname=sprat ssn= firstname=jack lastname=sprat ssn= 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

15 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

16 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

17 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

18 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 . . .

19 . . . Search by providing an ID.

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

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

22 ** 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>

23 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

24 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


Download ppt "Java Servlets Servlet Overview Servlets and HTML Forms Servlet Basics"

Similar presentations


Ads by Google