Download Server-Side Scripting with JSP (2)

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Server-Side Scripting with JSP (2)
ISYS 350
Java Array
• Examples of declaring an array:
– int[] anArray = new int[10];
• 10 elements index from 0 to 9
– double[] anArrayOfDoubles = new double[10];
– String[] anArrayOfStrings = new String[10];
– int[] anArray = { 100, 200, 300, 400, 500, 600,
700, 800, 900, 1000};
Creating a listbox of rates
double[] rates= {0.03,0.04,0.05,0.06,0.07};
String[] displays={"3%","4%","5%","6%","7%"};
out.println( "Select interest rate: <select name='Rate'>");
for (int i = 0; i <= rates.length-1; i++)
{
out.println("<option value='" + rates[i] + "'>" + displays[i] + "</option>");
}
out.println( "</select><br><br>");
Compute Future Value:
Process form with controls Created Using JSP Code:
Note this page is a jsp page
JSP code to create the form
<form name="fvForm" method="post" action="computeFV.jsp">
Enter present value: <input type="text" name="PV" value="" /><br><br>
<%
double[] rates= {0.03,0.04,0.05,0.06,0.07};
String[] displays={"3%","4%","5%","6%","7%"};
out.println( "Select interest rate: <select name='Rate'>");
for (int i = 0; i <= rates.length-1; i++)
{
out.println("<option value='" + rates[i] + "'>" + displays[i] + "</option>");
}
out.println( "</select><br><br>");
double[] yearValues= {10,15,30};
String[] yearLabels={"10-year","15-year","30-year"};
out.println( "Select year:<br><br>");
for (int i = 0; i <= yearValues.length-1; i++)
{
out.println("<input type='radio' name='Year' value='" + yearValues[i] + "'/>" +
yearLabels[i] + "<br><br>");
}
%>
<input type="submit" value="ComputeFVJSP" name="btnCompute" />
JSP Code to process the form
<body>
<%
String myPV, myRate, myYear;
myPV=request.getParameter("PV");
myRate=request.getParameter("Rate");
myYear=request.getParameter("Year");
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);
%>
Testing if two strings are equal
• The equals() or equalsIgnoreCase method
should be used to determine whether two
objects have the same values.
String string1 = "foo";
String string2 = "Foo";
if (string1==string2)
//if (string1.equals(string2))
//if (string1.equalsIgnoreCase(string2))
{
out.println("The two strings are equal.");
}
else
{
out.println("The two strings are not equal.");
}
Java Date Processing:
Date Class
• Date class:
– Define a date object:
• Date myDate;
– Define a date object with the current date:
• Date currentDate = new Date();
– getTime method: return the date in milliseconds
since 1/1/1900
Java Date Processing:
DateFormat class
• DateFormat class is used to define a date format
to display a date object or parsing a date/time
string to create a date object.
• Define a date format:
– DateFormat formatter = new SimpleDateFormat("MM/dd/yy");
• To print a data object:
– out.print("Current date is: " + formatter.format(currentDate));
• To parse a date string:
– myDate=formatter.parse(request.getParameter("txtDa
te"));
Import Java Class
Example: Display Current Date Time
• Import java.util.Date
– <%@page import="java.util.Date"%>
• Define a Date type variable:
– Date currentDate = new Date();
• Display in textbox using JSP expression:
<p>The time is: <input type="text" name="num2" size="20"
value="<%=currentDate%>"></p>
Date to Date Format
• Import class:
– <%@page import="java.text.DateFormat"%>
– Define a format:
• SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yy");
– Convert:
• Example: Display a date with date format:
String displayDate = formatter.format(currentDate);
Define Date Format
Letter Date or Time Component Presentation
Examples
G
Era designator
Text
AD
y
Year
Year
1996; 96
M
Month in year
Month
July; Jul; 07
w
Week in year
Number
27
W
Week in month
Number
2
D
Day in year
Number
189
d
Day in month
Number
10
F
Day of week in month
Number
2
E
Day in week
Text
Tuesday; Tue
a
Am/pm marker
Text
PM
H
Hour in day (0-23)
Number
0
k
Hour in day (1-24)
Number
24
K
Hour in am/pm (0-11)
Number
0
h
Hour in am/pm (1-12)
Number
12
m
Minute in hour
Number
30
s
Second in minute
Number
55
S
Millisecond
Number
978
z
Time zone
General time zone Pacific Standard Time; PST; G
MT-08:00
Z
Time zone
RFC 822 time zone -0800
Calculating Days between Current Date
and Entered Date Using the getTime()
method
<form name="dateForm" method="get" action="computeDate.jsp">
Enter a date: <input type="text" name="txtDate" value="7/4/12"
/><br><br>
<input type="submit" value="compute Date" name="btnSubmit" />
</form>
computeDate.jsp
<%
Date currentDate = new Date();
Date myDate;
DateFormat formatter = new SimpleDateFormat("MM/dd/yy");
myDate=formatter.parse(request.getParameter("txtDate"));
out.print(currentDate +"<br>");
out.print("Current date is: " + formatter.format(currentDate)+"<br>");
out.print("Entered date is: " + formatter.format(myDate)+"<br>");
out.print ("Days between = " + (currentDate.getTime()myDate.getTime())/(24*60*60*1000)+"<br>");
DateFormat yearFormat=new SimpleDateFormat("yyyy");
DateFormat monthFormat=new SimpleDateFormat("MM");
DateFormat weekDayFormat=new SimpleDateFormat("E");
out.print(yearFormat.format(currentDate)+"<br>");
out.print(monthFormat.format(currentDate)+"<br>");
out.print(weekDayFormat.format(currentDate)+"<br>");
%>
Java Servlet
What is Java Servlet?
• It is a java class that serves a client request and
receives a response from the server.
• Servlets are most often used to:
• Process or store data that was submitted from an
HTML form
• Provide dynamic content such as the results of a
database query.
• It is not a web page and cannot be opened by
itself.
• A servlet is called by a HTML form’s action
attribute:
• <form name="fvForm" method="post" action="FVServlet">
Adding a Servlet
• Servlet is a class with a “java” extension:
– Ex: FVServlet.java
• It must belong to a package:
– Add a new package
– Then add a new servelet
– Ex:
ServletPackage
• FVServlet.java
Servlet’s processRequest Method
• This method use the same request and response
objects as JSP. For example, it uses the
request.getParameter method to read the data
submitted with http request:
– myPV=request.getParameter("PV");
• It uses the out.println statement to send HTML
code to browser:
• out.println("<html>");
• out.println("<head>");
Example: Future Value Calculator:
Requesting FVServlet servlet
<form name="fvForm" method="post" action="FVServlet">
Enter present value: <input type="text" name="PV" value="" /><br><br>
Select interest rate: <select name="Rate">
<option value=.04>4%</option>
<option value=.05>5%</option>
<option value=.06>6%</option>
<option value=.07>7%</option>
<option value=.08>8%</option>
</select><br><br>
Select year: <br>
<input type="radio" name="Year" value="10" />10-year<br>
<input type="radio" name="Year" value="15" />15-year<br>
<input type="radio" name="Year" value="30" />30-year<br><br>
<br>
<input type="submit" value="ComputeFVJSP" name="btnCompute"/>
</form>
FVServlet
protected void processRequest(HttpServletRequest request,
HttpServletResponse response)
String myPV, myRate, myYear,qString;
myPV=request.getParameter("PV");
myRate=request.getParameter("Rate");
myYear=request.getParameter("Year");
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);
Servlet: depTableServlet
to create straight line depreciation table
Straight Line Depreciation Table
<form name="depForm" method="post" action="depTableServlet">
Enter Property Value: <input type="text" name="pValue" value="" /><br>
Enter Property Life: <input type="text" name="pLife" value="" /><br>
<input type="submit" value="Show Table" name="btnShowTable" />
</form>
String strValue, strLife;
strValue=request.getParameter("pValue");
strLife=request.getParameter("pLife");
double value, life, depreciation,totalDepreciation=0;
value=Double.parseDouble(strValue);
life=Double.parseDouble(strLife);
NumberFormat nf = NumberFormat.getCurrencyInstance();
out.println("Straight Line Depreciation Table" + "<br>");
out.println("Property Value: <input type='text' name='pValue' value='" + nf.format(value) + "' /><br>");
out.println("Property Life: <input type='text' name='pLife' value='" + life + "' /><br>");
depreciation=value/life;
totalDepreciation=depreciation;
out.println( "<table border='1' width='400' cellspacing=1>");
out.println("<thead> <tr> <th>Year</th> <th>Value at BeginYr</th>");
out.println("<th>Dep During Yr</th> <th>Total to EndOfYr</th></tr> </thead>");
out.println("<tbody>");
for (int count = 1; count <= life; count++)
{
out.write("<tr>");
out.write(" <td width='10%'>" + count + "</td>");
out.write(" <td width='30%'>" + nf.format(value) + "</td>");
out.write(" <td width='30%'>" + nf.format(depreciation) + "</td>");
out.write(" <td width='30%'>" + nf.format(totalDepreciation) + "</td>");
value -= depreciation;
totalDepreciation+=depreciation;
}
Related documents