server-side scripting with java server page, jsp
Post on 19-Dec-2015
226 views
TRANSCRIPT
Server-Side Scripting with Java Server Page, JSP
Hyper Text Transfer Protocol: Request & Response
Web ServerBrowser
HTTP Request
HTTP Response
Web Application
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.
Methods of request Object
• * getCookies()• * getHeader(String name)• * getMethod()• * getParameter(String name)• * getParameterNames()• * getParameterValues(String name)• * getQueryString()• * getRequestURI()• etc.
Use JSP to Process a Form
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: <form name="fvForm" method ="post" action="computeFV.jsp">
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)
Slide 8
How to declare and initialize a variable in one statement
Syntax type variableName = value;
Examples int scoreCounter = 1; // initialize an integer variable double unitPrice = 14.95; // initialize a double variable int maxQuantity = 100; // initialize another integer variable
Declare VariablesType variableName ;Examples
int myCounter;double interestRate;
Slide 9
The eight primitive data types
Type Bytes Use
byte 1 Very short integers from -128 to 127.
short 2 Short integers from -32,768 to 32,767.
int 4 Integers from -2,147,483,648 to 2,147,483,647.
long 8 Long integers from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807.
float 4 Single-precision, floating-point numbers from -3.4E38 to 3.4E38 with up to 7 significant digits.
double 8 Double-precision, floating-point numbers from -1.7E308 to 1.7E308 with up to 16 significant digits.
char 2 A single Unicode character that’s stored in two bytes.
boolean 1 A true or false value.
Compute the Sum of 2 Numbers:Example of JSP scriptlet <% %>
<body> <% 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); %> </body>
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 .
Writing HTML code as output<body> <% 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(" <p>Value1: <input type='text' name='num1' size='20' value='" + value1 + "'></p>"); out.println(" <p>Value2: <input type='text' name='num2' size='20' value='" + value2 + "'></p>");
out.println("The sum is: <input type='text' name='sum' size='20' value='" + sum + "'>" ); %> </body>
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; %>
<form method="POST" name="testForm" action="ComputeSum2.jsp"> <p>Value1: <input type="text" name="num1" size="20" value="<%=value1%>"></p> <p>Value2: <input type="text" name="num2" size="20" value="<%=value2%>"></p> <p> Sum is: <input type="text" name="num2" size="20" value="<%=sum%>"></p> <input type="submit" value="Compute Sum" name="btnSubmit" />
Compute Future Value:Process form with various controls
Code Example
<body> <% 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); %>
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);
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.
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()
Example: Enter CID in a box and retrieve the customer record
<% 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
{
} %>
Create a CID ListBox
<form name="cidForm" method ="post" action="getCustomer.jsp"> <p>Select CID:<select name="cid"></p> <% 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("<option value='" +myCid + "'>" + myCid + "</option>"); } } catch(SQLException e)
{for (Throwable t :e)
t.printStackTrace();}
%> </select> <input type="submit" value="GetCustomerData" name="btnSubmit" /> </form>