Download presentation
Presentation is loading. Please wait.
Published byBrian Heath Modified over 9 years ago
1
Java Servlet ISYS 350
2
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:
3
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
4
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(" ");
5
Example: Future Value Calculator: Requesting FVServlet servlet Enter present value: Select interest rate: 4% 5% 6% 7% 8% Select year: 10-year 15-year 30-year
6
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(" "); and out.println(" ");
7
Servlet: depTableServlet to create straight line depreciation table Straight Line Depreciation Table Enter Property Value: Enter Property Life:
8
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" + " "); out.println("Property Value: "); out.println("Property Life: "); depreciation=value/life; totalDepreciation=depreciation; out.println( " "); out.println(" Year Value at BeginYr "); out.println(" Dep During Yr Total to EndOfYr "); out.println(" "); for (int count = 1; count <= life; count++) { out.write(" "); out.write(" " + count + " "); out.write(" " + nf.format(value) + " "); out.write(" " + nf.format(depreciation) + " "); out.write(" " + nf.format(totalDepreciation) + " "); value -= depreciation; totalDepreciation+=depreciation; }
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.