Download presentation
Presentation is loading. Please wait.
1
JDBC & Servlet CSE 4504/6504 Lab
2
Outline HTML Forms Tomcat Functions in JDBC & Servlet
3
HTML Forms An interface controls to collect data from the user and transmit it to server.
4
Element in Forms TEXT CONTROLS: PASSWORD FIELDS: TEXT AREAS: Checkbox
<INPUT TYPE="TEXT" NAME="NAME" VALUE="INIT"> PASSWORD FIELDS: <INPUT TYPE="PASSWORD" NAME="PASSWORD"> TEXT AREAS: <TEXTAREA NAME="RESUME" ROWS=5 COLS=30>INPUT YOUR RESUME HERE </TEXTAREA> Checkbox <input type="checkbox" name="checkbox" checked> <input type="checkbox" name="checkbox"> Radio Button <input type="radio" name="radio" checked> <input type="radio" name="radio">
5
Cont. List Multilist <select name="list">
<option value="Item 1">Item 1</option> <option value="Item 2">Item 2</option> <option value="Item 3">Item 3</option> </select> Multilist <select name="multilist" size="3" multiple>
6
Cont. Submit Button Reset Button Image Button File
<input type="submit" name="submit" value="Submit"> Reset Button <input type="reset" name="reset" value="Reset Fields"> Image Button <input type="image" name="image" src="go.gif"> File <input type="file" name="file">
7
Tomcat A light web server that supports servlet & JSP.
It can be integrated in Apache, IIS For installation process, please refer: CS III: Lab assignment, servlet
8
What is JDBC & Servlet? JDBC (Java DataBase Connectivity) provides functions to access database system. Servlet enables java for CGI programs. Setup JDBC environment: Please refer: CS III, Lab assignment, JDBC JDBC is a API provides a lot of functions to access the database system. It is used in java programming language. Servlets are modules that extend request/response-oriented servers, such as Java-enabled web servers. For example, a servlet might be responsible for taking data in an HTML order-entry form and applying the business logic used to update a company's order database. Servlets can be embedded in many different servers because the servlet API, which you use to write servlets, assumes nothing about the server's environment or protocol. Servlets have become most widely used within HTTP servers; many web servers support the Servlet API.
9
JDBC : Establishing a Connection
loading the driver DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver()); making the connection String url = Connection con = DriverManager.getConnection(url, “loginName", “Password");
10
Statement Create a statement Two methods of statement
Statement stmt = con.createStatement(); Two methods of statement 1. executeUpdate() create, alter, drop a table Or insert, delete, update data 2. executeQuery() select
11
Create Table String createTableCoffees = "CREATE TABLE COFFEES " + "(COF_NAME VARCHAR(32), SUP_ID INTEGER, PRICE FLOAT, " + "SALES INTEGER, TOTAL INTEGER)"; stmt.executeUpdate(createTableCoffees);
12
Query Data from a Table stmt.executeQuery (“select * from customer”);
ResultSet rs = stmt.executeQuery( "SELECT COF_NAME, PRICE FROM COFFEES");
13
Display Result Method next()
Initially the cursor is above the first row of data. After call the method next(), the cursor is pointing to the first row of data. A Sample while (rs.next()) { String s = rs.getString ("COF_NAME"); float n = rs.getFloat ("PRICE"); System.out.println (s + " " + n); } References:
14
Methods to Call a Servlet
GET In html: <A HREF="/servlet/dosearch?aa=12&bb=32">Return Home</A> In html forms: <FORM ACTION=“/servlet/dosearch” METHOD=“GET”> POST <FORM ACTION=“/servlet/dosearch” METHOD=“POST”>
15
Interacting with Clients
Handling GET and POST Requests public void doGet (HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException <a href="/servlet/getCustomers">List Customers</a> public void doPost (HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException <FORM ACTION=\"/servlet/doInsert\" METHOD=post>
16
Output of the Response Set the content type of the output
response.setContentType ("text/html") Get parameter String bookId = request.getParameter ("bookId"); Get the output stream to write to PrintWriter out = response.getWriter(); out.println(“<HTML>”);
17
Servlet Program Structures
import javax.servlet.*; import javax.servlet.http.*; Class must extend from class HttpServlet
18
A Simple Application public class SimpleServlet extends HttpServlet {
// Handle the HTTP GET method by building a simple web page. public void doGet (HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException { PrintWriter out; String title = "Simple Servlet Output"; // set content type and other response header fields response.setContentType("text/html"); // then write the data of the response out = response.getWriter(); out.println("<HTML><HEAD><TITLE>"); out.println (title); out.println("</TITLE></HEAD><BODY>"); out.println("<H1>" + title + "</H1>"); out.println("<P>This is output from SimpleServlet."); out.println("</BODY></HTML>"); out.close(); } You must set HTTP header data before you access the Writer or OutputStream. The HttpServletResponse class provides methods to access the header data.For example, the setContentType method sets the content type. (This header is often the only one manually set.)
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.