Presentation is loading. Please wait.

Presentation is loading. Please wait.

Server-Side Scripting with Java Server Page, JSP.

Similar presentations


Presentation on theme: "Server-Side Scripting with Java Server Page, JSP."— Presentation transcript:

1 Server-Side Scripting with Java Server Page, JSP

2 Hyper Text Transfer Protocol: Request & Response Web Server Browser HTTP Request HTTP Response Web Application

3 Data Sent with Request and Response Request: – requested URL, cookies, queryString, data from a form, etc. Response: – Web page content in HTML code – Cookies – Etc.

4 Methods of request Object * getCookies() * getHeader(String name) * getMethod() * getParameter(String name) * getParameterNames() * getParameterValues(String name) * getQueryString() * getRequestURI() etc.

5 Use JSP to Process a Form

6 FORM Tag Form attribute: – Method: Preferred method is: Post – Action: Specify the URL of a program on a server or an email address to which a form’s data will be submitted. Example:

7 Methods of response Object * setContentType() * addCookie(Cookie cookie) * addHeader(String name, String value) * containsHeader(String name) * setHeader(String name, String value) * sendRedirect(String) * sendError(int status_code)

8 Slide 8 Declare Variables Type variableName ; Examples int myCounter; double interestRate;

9 Slide 9

10 Compute the Sum of 2 Numbers: Example of JSP scriptlet <% String value1, value2; double n1, n2, sum; value1=request.getParameter("num1"); value2=request.getParameter("num2"); n1= Double.parseDouble(value1); n2= Double.parseDouble(value2); sum=n1+n2; out.println("The sum is:" + sum); %> Note 1: Double is an object, not “double” data type. Note 2: “out” is a variable that does not need to be declared. It is already predefined.

11 Writing HTML code as output <% String value1, value2; double n1, n2, sum; value1=request.getParameter("num1"); value2=request.getParameter("num2"); n1= Double.parseDouble(value1); n2= Double.parseDouble(value2); sum=n1+n2; out.println(" Value1: "); out.println(" Value2: "); out.println("The sum is: " ); %>

12 Using JSP Expression: String value1, value2; double n1, n2, sum; value1=request.getParameter("num1"); value2=request.getParameter("num2"); n1= Double.parseDouble(value1); n2= Double.parseDouble(value2); sum=n1+n2; %> Value1: "> Value2: "> Sum is: ">

13 Compute Future Value: Process form with various controls

14 Code Example <% String myPV, myRate, myYear,myCheckbox; myPV=request.getParameter("PV"); myRate=request.getParameter("Rate"); myYear=request.getParameter("Year"); myCheckbox=request.getParameter("testCheckbox"); double FV, PV, Rate, Year; PV=Double.parseDouble(myPV); Rate=Double.parseDouble(myRate); Year=Double.parseDouble(myYear); FV=PV*Math.pow(1+Rate,Year); out.println("FutureValue is:"+ FV); %>

15 Database Processing with JSP Must import java.sql using page directive: – %@page import="java.sql.*" % Need a driver: – JDBC-ODBC Bridge: The JDBC-ODBC Bridge allows applications written in the JavaTM programming language to use the JDBCTM API with many existing ODBC drivers. – Example: String DBUrl="jdbc:odbc:MySalesDB"; Define connection object: – connection = DriverManager.getConnection(DBUrl); Define Statement object: – Statement SQLStatement = connection.createStatement(); Run SQL to create resultset: – strSQL="select * from customer where cid='" + myCid + "'"; – ResultSet rs = SQLStatement.executeQuery(strSQL);

16 JDBC ResultSet The rows that satisfy a particular query are called the result set. The number of rows returned in a result set can be zero or more. A user can access the data in a result set using a cursor one row at a time from top to bottom. A cursor can be thought of as a pointer to the rows of the result set that has the ability to keep track of which row is currently being accessed.

17 ResultSet Methods Next() : Returns true if the cursor is now positioned on a row and false if the cursor is positioned after the last row. previous() first() last()

18 Example: Enter CID in a box and retrieve the customer record

19 <% Connection connection = null; String DBUrl="jdbc:odbc:MySalesDB"; try { String myCid, strSQL, Cname, City, Rating; connection = DriverManager.getConnection(DBUrl); Statement SQLStatement = connection.createStatement(); myCid=request.getParameter("cid"); strSQL="select * from customer where cid='" + myCid + "'"; ResultSet rs = SQLStatement.executeQuery(strSQL); if (rs.next()) { Cname=rs.getString("CNAME"); City=rs.getString("CITY"); Rating=rs.getString("Rating"); rs.close(); out.println( Cname); out.println( City); out.println( Rating); } else { out.println("Customer not exist!"); rs.close(); } catch(SQLException e) { for (Throwable t :e) t.printStackTrace(); } finally { } %>

20 Create a CID ListBox

21 Select CID: <% Connection connection = null; String DBUrl="jdbc:odbc:MySalesDB"; try { String myCid, strSQL; connection = DriverManager.getConnection(DBUrl); Statement SQLStatement = connection.createStatement(); strSQL="select cid from customer;"; ResultSet rs = SQLStatement.executeQuery(strSQL); while (rs.next()) { myCid=rs.getString("cid"); out.println(" " + myCid + " "); } catch(SQLException e) { for (Throwable t :e) t.printStackTrace(); } %>

22 Insert a New Record Statement object: – executeUpdate method

23 Adding a new customer <% Connection connection = null; String DBUrl="jdbc:odbc:MySalesDB"; try { String myCid, strSQL; String CID, Cname, City, Rating; CID=request.getParameter("CID"); Cname=request.getParameter("Cname"); City=request.getParameter("City"); Rating=request.getParameter("Rating"); strSQL = "Insert into Customer values ('"; strSQL = strSQL + CID + "','" + Cname + "','"; strSQL = strSQL + City + "','" + Rating + "')"; connection = DriverManager.getConnection(DBUrl); Statement SQLStatement = connection.createStatement(); SQLStatement.executeUpdate(strSQL); out.write("Record added"); } catch(SQLException e) { out.write("Record not added"); out.write(e.getMessage()); for (Throwable t :e) t.printStackTrace(); } %>

24 <% Connection connection = null; String DBUrl="jdbc:odbc:MySalesDB"; try { connection = DriverManager.getConnection(DBUrl); Statement SQLStatement = connection.createStatement(); strSQL="select * from customer;"; ResultSet rs = SQLStatement.executeQuery(strSQL); out.write(" "); while (rs.next()) { out.write(" "); out.write(" " + rs.getString("CID") + " "); out.write(" " + rs.getString("Cname") + " "); out.write(" " + rs.getString("City") + " "); out.write(" " + rs.getString("Rating") + " "); out.write(" "); } out.write(" "); } catch(SQLException e) { for (Throwable t :e) t.printStackTrace(); } %> Create a Table to Display Customer Records

25 Creating Class Classes used with a web application must be stored as a “Package”. Step 1: Create a new package – Right click src folder and select New/Java Package – Name the new package For example, myPackage Step 2: Creating new class in the package – Right click the package folder and select New/Java Class – Name the class

26 Customer Class Example Use a private variable to store a property’s value. private String pvCID, pvCname, pvCity, pvRating ; Use set and get method to define a property: public void setCname(String cname){ this.pvCname=cname; } public String getCname(){ return pvCname; } Note: “void” indicates a method will not return a value.

27 Code Example: Customer Class package myPackage; import java.sql.*; public class Customer { private String pvCID, pvCname, pvCity, pvRating ; public Customer(){} public void setCID(String cid){ this.pvCID=cid; } public String getCID(){ return pvCID; } public void setCname(String cname){ this.pvCname=cname; } public String getCname(){ return pvCname; } public void setCity(String city){ this.pvCity=city; } public String getCity(){ return pvCity; } public void setRating(String rating){ this.pvRating=rating; } public String getRating(){ return pvRating; }

28 public Boolean GetCustData(String cid) { Connection connection = null; String DBUrl="jdbc:odbc:MySalesDB"; Boolean RecExist=false; try { connection = DriverManager.getConnection(DBUrl); Statement SQLStatement = connection.createStatement(); String strSQL="select * from customer where cid='" + cid + "'"; ResultSet rs = SQLStatement.executeQuery(strSQL); if (rs.next()) { pvCID=rs.getString("CID"); pvCname=rs.getString("CNAME"); pvCity=rs.getString("CITY"); pvRating=rs.getString("Rating"); rs.close(); RecExist=true; } else { System.out.print("Customer not exist!"); rs.close(); RecExist=false; } catch(SQLException e) { for (Throwable t :e) t.printStackTrace(); } finally { return RecExist; }

29 Using Class Must import the package: %@page import="myPackage.*" % Define a class variable: Customer C1 = new Customer();

30 JSP Using Class <% String custID = request.getParameter("cid"); String Cname, Rating; Customer C1 = new Customer(); if (C1.GetCustData(custID)) { Cname = C1.getCname(); Rating = C1.getRating(); } else { Cname = "NA"; Rating = "NA"; } %> CID: Cust name: Rating:


Download ppt "Server-Side Scripting with Java Server Page, JSP."

Similar presentations


Ads by Google