Java Servlet ISYS 350.

Slides:



Advertisements
Similar presentations
Java Classes ISYS 350. Introduction to Classes A class is the blueprint for an object. – It describes a particular type of object. – It specifies the.
Advertisements

Server Side Programming Common Gateway Interface (CGI): Scripts generate Web pages or other files dynamically by processing form data and returning documents.
Server-Side Scripting with Java Server Page, JSP.
Multiple Tiers in Action
Contrast with JavaScript HTML Formsto invoke Java Server Pages Structure of Forms Query strings Java Server Pages Sent From Browser To Serverfor JSP.
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.
CSCI 6962: Server-side Design and Programming History and Background.
Gayle J Yaverbaum, PhD Professor of Information Systems Penn State Harrisburg.
Web Development & Design Foundations with XHTML Chapter 9 Key Concepts.
AJAX and Java ISYS 350. AJAX Asynchronous JavaScript and XML: – Related technologies: JavaScript, Document Object Model, XML, server-side script such.
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 –
Server-Side Scripting with Java Server Page, JSP ISYS 350.
Java Servlets and Java Server Pages Carol Wolf Computer Science.
Chapter 7 Java Server Pages. Objectives Explain how the separation of concerns principle applies to JSP Describe the operation and life-cycle of a JSP.
Server-Side Scripting with Java Server Page, JSP ISYS 350.
Java Classes ISYS 350. Introduction to Classes Two basic uses of class: – 1. A class is a way to organize functions (methods) to perform calculations.
JSP Fundamentals Elements of a JSP Using Beans with JSP Integrating Servlets and JSP.
Server-Side Scripting with JSP (2) ISYS 350. Post Back A postback is call to the same page that the form is on. In other words, the contents of the form.
Server-Side Scripting with PHP ISYS 475. PHP Manual Website
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 –
Java Servlet ISYS 350. 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.
Basic JSP Celsina Bignoli Problems with Servlets Servlets contain –request processing, –business logic –response generation all lumped.
COMP9321 Web Application Engineering Semester 2, 2015 Dr. Amin Beheshti Service Oriented Computing Group, CSE, UNSW Australia Week 3 1COMP9321, 15s2, Week.
©SoftMoore ConsultingSlide 1 Overview of Servlets and JavaServer Pages (JSP)
Server-Side Scripting with Java Server Page, JSP.
Server-Side Scripting with Java Server Page, JSP ISYS 350.
Server-Side Scripting with Java Server Page, JSP ISYS 350.
Introduction To HTML Dr. Magdi AMER. HTML elements.
How CGI and Java Servlets are Run By David Stein 14 November 2006.
 Java Server Pages (JSP) By Offir Golan. What is JSP?  A technology that allows for the creation of dynamically generated web pages based on HTML, XML,
Web Forms. Web Forms: A form allows our web visitors to submit information to us. Some examples uses for forms are to let the web user contact us, fill.
Programming with Java Lecture 6 Elements of a Java Servlet
JSP java server pages.
Introduction to Servlets
Java Class and Servlet ISYS 350.
JSP Based on
Computing with C# and the .NET Framework
Making your HTML Page Interactive
Web Programming: If Statement and Servlet
Principles of Software Development
Developing JavaServer Pages
Net-centric Computing
JDBC & Servlet CSE 4504/6504 Lab.
CS3220 Web and Internet Programming HTML Tables and Forms
Introduction to Client-Server Programming
AJAX and Java Servlet ISYS 350.
Tables Tables provide a means of organising the layout of data
JSP Implicit Objects Implicit Objects in JSP are objects that are automatically available in JSP. request: The request object retrieves the values that.
Client-Side Web Application Development with JavaScript
PHP Overview PHP: Hypertext Preprocessor Server-Side Scripting
Sessions.
HTML: Basic Tags & Form Tags
Server-Side Scripting with Java Server Page, JSP
Server-Side Scripting with Java Server Page, JSP
AJAX and JSP ISYS 350.
Database Processing with JSP
Servlets.
Java Server Pages.
Client-Side Web Application Development with JavaScript
Servlets and Java Server Pages
Servlets and JSP 20-Nov-18 servletsJSP.ppt.
CS3220 Web and Internet Programming HTML Tables and Forms
Web Search Interfaces.
Servlets.
Array ISYS 350.
Java Servlets Servlet Overview Servlets and HTML Forms Servlet Basics
Multivalued parameters
AJAX and JSP ISYS 350.
Making your HTML Page Interactive
HTML: Basic Tags & Form Tags
Presentation transcript:

Java Servlet ISYS 350

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

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

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

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>

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