Presentation is loading. Please wait.

Presentation is loading. Please wait.

Java Servlet. What is Java Servlet? A Servlet is a java based server side web technology. It is a java class that serves a client request and receives.

Similar presentations


Presentation on theme: "Java Servlet. What is Java Servlet? A Servlet is a java based server side web technology. It is a java class that serves a client request and receives."— Presentation transcript:

1 Java Servlet

2 What is Java Servlet? A Servlet is a java based server side web technology. It is a java class that serves a client request and receives a response from the server.

3 Adding a Servlet Servlet is a class with a “java” extension: – Ex: FVServlet.java It must belong to a package: – Ex:ServletPackage FVServlet.java

4 Servlet Methods: doGet() and doPost()

5 processRequest Method: 1. doGet and doPost methods now call processRequest method to handle the http requests. 2. These methods use the same request and response objects as JSP. protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter();

6 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

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

8 Straight Line Depreciation Table Enter Property Value: Enter Property Life:

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

10 Creating a Java Database with NetBeans Tutorial: – http://netbeans.org/kb/docs/ide/java- db.html#starting http://netbeans.org/kb/docs/ide/java- db.html#starting

11 JDBC http://docs.oracle.com/javase/tutorial/jdbc/b asics/index.html


Download ppt "Java Servlet. What is Java Servlet? A Servlet is a java based server side web technology. It is a java class that serves a client request and receives."

Similar presentations


Ads by Google