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


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

Similar presentations


Ads by Google