Presentation is loading. Please wait.

Presentation is loading. Please wait.

J.Sant Servlets Joseph Sant Sheridan Institute of Technology.

Similar presentations


Presentation on theme: "J.Sant Servlets Joseph Sant Sheridan Institute of Technology."— Presentation transcript:

1 J.Sant Servlets Joseph Sant Sheridan Institute of Technology

2 J.Sant Servlets - What Java classes launched by a web-server to respond to HTML form submissions. Programmer developed servlets are subclasses of HttpServlet Programs system output automatically redirected to the browser. inherently multi-threaded. Form input from client is automatically parsed. Each request launches a new thread.

3 J.Sant Servlets - Why? Provide convenient utilities for programmer to handle form input and HTML output. URL-encoded data from the form is automatically parsed. More efficient than some alternatives because lightweight threads are launched to service a request whereas other techniques launch entire processes (e.g. ASP’s, CGI). Portability. JVM security.

4 J.Sant Servlets – How to? download the servlet classes for your development environment and update the CLASSPATH variable. download application server. write and upload forms to the directories specified in the app server documentation. write and upload servlet java or class files to the directories specified in the app server documentation. run the server.

5 J.Sant Servlets – How? Install a web server capable of launching and managing servlet programs. Install the javax.servlet package to enable programmers to write servlets. Ensure CLASSPATH is changed to reference jar file containing java.servlet package. Define a servlet by subclassing the HttpServlet class and adding any necessary code to the doGet() and/or doPost() and if necessary the init() functions.

6 J.Sant HTML Forms FORM USING GET FORM using GET Diameter: Form interacts with circle Servlet Use textbox to get a diameter. Submit Button Sends form input to server.

7 J.Sant Servlets - Why Not? Need a programmer to change web site output. Ideally graphic designers and web masters should be able to change look-and-feel of site without programmer intervention. Requires Java expertise.

8 J.Sant Servlets - Alternatives? Many approaches possible for internet software development.  Java servlets (server) and HTML+Javascript (client). OR  CGI programs (server) and HTML+Javascript (client).  Java socket-based server application ( server ) and Java Applet client.  Java socket-based server application ( server ) and Java Application client.  EJB’s, Remote Method Calls, etc.  Java Server Pages ( server ).  Active Server Pages ( server ).

9 J.Sant Servlets – Coding import javax.servlet.* Subclass HttpServlet Provide any one time initialization code in the init() function. Provide code to respond to an http GET in the doGet(). Provide code to respond to an http POST in the doPost(). doPost() is commonly encoded by simply calling the doGet() public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); }

10 J.Sant Servlets – Coding Use getParameter() to retrieve the parameters from the form by name. String sdiam = request.getParameter("diameter"); Named Field values HTML FORM In JAVA Servlet

11 Servlet Code import java.io.*; import javax.servlet.*; import javax.servlet.http.*; import java.util.*; public class circle extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println( “ Circle Info \n"); try{ String sdiam = request.getParameter("diameter"); double diam = Double.parseDouble(sdiam); out.println(" Diam: " + diam + " Area: " + diam/2.0 * diam/2.0 * 3.14159 + " Perimeter: " + 2.0 * diam/2.0 * 3.14159); } catch ( NumberFormatException e ){ out.println("Please enter a valid number"); } out.println(" "); } Attach a PrintWriter to Response Object Specify HTML output. Subclass HttpServlet.

12 J.Sant Servlet Lifecycle. Taken from IBM Website.

13 J.Sant Servlet Lifecycle Understanding the lifecycle might help develop more efficient and scalable applications. Servlets have functions which are executed with every request and others that are executed only once.

14 J.Sant Servlet Lifecycle init() executed only once when servlet is started.  Use this to load collections, open database connections etc. doPost(), doGet() executed with every request depending on whether request was a post or a get.  Responding to form submission. Destroy() executed only once when servlet is terminated.  clean-up routines

15 J.Sant Servlets, Servlet Containers and Application Servers Servlets require a special type of web server. These servers are sometimes called application servers. Since servlets aren’t programs they must be run within a container. Well-known servlet containers are Apache Resin and Caucho Resin. When you develop servlets or jsps you must deploy them on a server. That means you must place them in an appropriate location on the server’s directory and update the server configuration. The first step in developing and running servlets/jsps is to install an application server.

16 J.Sant Setting up Tomcat In Windows: Key Tasks you must download and install the Tomcat server. You must change your system environment so that you can compile servlet classes in addition to running them. you must set the JAVA_HOME environment variable. HTML files and java class files for servlets must be placed in the appropriate directories.

17 J.Sant Setting The CLASSPATH CLASSPATH must include servlet-api.jar in Windows XP.  Select Control Panel from the Start Menu.  Select Switch to Classic View.  Select System then the Advanced Tab.  select Environment Variables.  The environment variable you want to change is CLASSPATH. You want to add to the path not replace it. Find the end of the list of classes on the classpath and append the following.  a semi-colon.  fullinstallpathforTomcat\common\lib\servlet-api.jar.

18 J.Sant CLASSPATH Examples Before %CLASSPATH%;c:\jdk1.4.2_09\lib\tools.jar;. After %CLASSPATH%;c:\jdk1.4.2_09\lib\tools.jar;.; common\lib\servlet-api.jar  Select Control Panel from the Start Menu.  Select Switch to Classic View.  Select System then the Advanced Tab.  select Environment Variables.  The environment variable you want to change is CLASSPATH. You want to add to the path not replace it. Find the end of the list of classes on the classpath and append the following.  a semi-colon.  fullinstallpathforTomcat\common\lib\servlet-api.jar.

19 J.Sant Installing Tomcat Download the Tomcat 5.0.28 installer into a known directory. http://apache.mirrored.ca/tomcat/tomcat-5/v5.0.28/bin/jakarta-tomcat-5.0.28.exe Run the installer using all defaults for installation. You may be asked for an admin password, choose one that is easy to remember. Test the installation.  Right-click on the Tomcat icon on the right side of the application bar.  Open a browser and type in the address http://localhost:8080 http://localhost:8080  The tomcat homepage should appear. Select the Servlet Examples link from the left side bar and attempt to execute some of the examples.


Download ppt "J.Sant Servlets Joseph Sant Sheridan Institute of Technology."

Similar presentations


Ads by Google