310 likes | 640 Views
JDBC & Servlet. CSE 4504/6504 Lab. Outline. HTML Forms Tomcat Functions in JDBC & Servlet. HTML Forms. An interface controls to collect data from the user and transmit it to server. Element in Forms. TEXT CONTROLS: <INPUT TYPE="TEXT" NAME="NAME" VALUE="INIT"> PASSWORD FIELDS:
E N D
JDBC & Servlet CSE 4504/6504 Lab
Outline • HTML Forms • Tomcat • Functions in JDBC & Servlet
HTML Forms An interface controls to collect data from the user and transmit it to server.
Element in Forms • TEXT CONTROLS: <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">
Cont. • List <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> <option value="Item 1">Item 1</option> <option value="Item 2">Item 2</option> <option value="Item 3">Item 3</option> </select>
Cont. • Submit Button <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">
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 http://www.cse.msstate.edu/~cs2324/spring03/
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 http://www.cse.msstate.edu/~cs2324/spring03/
JDBC : Establishing a Connection • loading the driver DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver()); • making the connection String url = "jdbc:oracle:thin:@ra.msstate.edu:1521:ACAD"; Connection con = DriverManager.getConnection(url, “loginName", “Password");
Statement • Create a statement Statement stmt = con.createStatement(); • Two methods of statement 1. executeUpdate() create, alter, drop a table Or insert, delete, update data 2. executeQuery() select
Create Table • String createTableCoffees = "CREATE TABLE COFFEES " + "(COF_NAME VARCHAR(32), SUP_ID INTEGER, PRICE FLOAT, " + "SALES INTEGER, TOTAL INTEGER)"; stmt.executeUpdate(createTableCoffees);
Query Data from a Table • stmt.executeQuery (“select * from customer”); • ResultSet rs = stmt.executeQuery( "SELECT COF_NAME, PRICE FROM COFFEES");
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: http://java.sun.com/docs/books/tutorial/jdbc/index.html
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 In html forms: <FORM ACTION=“/servlet/dosearch” METHOD=“POST”>
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>
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>”);
Servlet Program Structures • import javax.servlet.*; import javax.servlet.http.*; • Class must extend from class HttpServlet
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(); } }