Presentation is loading. Please wait.

Presentation is loading. Please wait.

Java Class and Servlet ISYS 350.

Similar presentations


Presentation on theme: "Java Class and Servlet ISYS 350."— Presentation transcript:

1 Java Class and Servlet ISYS 350

2 Introduction to Classes
Two basic uses of class: 1. A class is a way to organize functions (methods) to perform calculations. Example: Java Math class offers methods such as pow, sqrt etc. to perform basic math calculations. 2. A class is used to define or model an object.

3 Defining Functions (Methods)
Modifier ReturnType functionName(dataType arg1,..,dataType argN) { Statements; return return_value; } 1. Modifiers—such as public, private. 2. The return type—the data type of the value returned by the method, or void if the method does not return a value. 3. The method name 4. Optional list of parameters in parenthesis separated by a comma 5. Any method that is not declared void must contain a return statement with a corresponding return value, like this: return return_Value;

4 Example: myFinancial Class
Define a myFinancial class with two methods: myPmt(double loan, double rate, double term) myFv(double pv, double rate, double years)

5 Code Example: myFinancial Class
public class myFinancial { public double myFV(double pv, double rate, double years) { double fv; fv=pv*Math.pow(1+rate,years); return fv; } public double myPmt(double loan, double rate, double term) double pmt; pmt=(loan*rate/12)/(1-Math.pow(1+rate/12,-12*term)); return pmt;

6 Adding Class to a Java Web Project
Java classes used with a web application must be stored as a “Package”. A package may have several classes. Step 1: Create a new package Right click the Source Packages 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

7 Using Class Must import the package: Define a class variable:
import="myPackage.*" %> Define a class variable: myFinancial myFin=new myFinancial();

8 FV HTML Form <form name="fvForm" method="post" action="computeFV.jsp"> 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> <input type="submit" value="ComputeFVJSP" name="btnCompute" /> </form>

9 computeFV.jsp <%@page import="myPackage.*" %> <%
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); myFinancial myFin=new myFinancial(); FV=myFin.myFV(PV, Rate, Year); out.println("FutureValue is:"+ FV); %>

10 What is Java Servlet? It is a java class that serves a client request.
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">

11 Adding a Servlet Servlet is a class with a “java” extension:
Ex: FVServlet.java It must belong to a package: Add a new Java Package: Ex. MyPackage Then add a new servelet under MyPackage: Ex: MyPackage FVServlet.java

12 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>");

13 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>

14 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); Note: Copy/paste the code between: out.println("<body>"); and out.println("</body>");

15 Servlet: depTableServlet to create 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>

16 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; }

17 Example of user-defined function
protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { ….. double n1,n2,sum; n1=Double.parseDouble(request.getParameter("num1")); n2=Double.parseDouble(request.getParameter("num2")); sum=ComputeSum(n1,n2); out.println("The sum is:" + sum); out.println("</body>"); out.println("</html>"); } private double ComputeSum(double n1, double n2) { return n1+n2; }

18 JSP and Servlet The JSP is translated into the appropriate Servlet code Your browser sends an HTTP request to the web server. The web server recognizes that the HTTP request is for a JSP page and forwards it to a JSP engine. The JSP engine loads the JSP page from disk and converts it into a servlet content. This conversion is very simple in which all template text is converted to println( ) statements and all JSP elements are converted to Java code. This code implements the corresponding dynamic behavior of the page. The JSP engine compiles the servlet into an executable class and forwards the original request to a servlet engine. A part of the web server called the servlet engine loads the Servlet class and executes it.


Download ppt "Java Class and Servlet ISYS 350."

Similar presentations


Ads by Google