1 / 11

Java Servlet

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 a response from the server. . Adding a Servlet. Servlet is a class with a “java” extension: Ex: FVServlet.java It must belong to a package:

cybill
Télécharger la présentation

Java Servlet

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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

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

  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); NumberFormatnf = 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; }

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

  11. JDBC • http://docs.oracle.com/javase/tutorial/jdbc/basics/index.html

More Related