servlets - Sheridan

Download Report

Transcript servlets - Sheridan

Servlets
Joseph Sant
Sheridan Institute of Technology
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.
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.
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.
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.
J.Sant
<HTML>
HTML Forms
<HEAD>
<TITLE>FORM USING GET</TITLE>
</HEAD>
<BODY BGCOLOR="#FDF5E6">
<H1 ALIGN="CENTER">FORM using GET</H1>
Form interacts with
circle Servlet
<FORM ACTION="http://localhost:8080/servlet/circle">
<CENTER>
Diameter:
<INPUT TYPE="TEXT" NAME="diameter">
Use textbox to
get a diameter.
&nbsp;&nbsp;&nbsp;
<INPUT TYPE="SUBMIT" VALUE="Submit Diameter">
</CENTER>
</FORM>
</BODY>
</HTML>
J.Sant
Submit Button
Sends form
input to server.
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.
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 ).
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);
J.Sant
}
Servlets – Coding

Use getParameter() to retrieve the
parameters from the form by name.
Named Field values HTML FORM
<INPUT TYPE="TEXT" NAME="diameter">
In JAVA Servlet
String sdiam = request.getParameter("diameter");
J.Sant
import java.io.*;
import javax.servlet.*;
Subclass
import javax.servlet.http.*;
HttpServlet.
Servlet Code
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(
Specify HTML
output.
Attach a
PrintWriter to
Response Object
“<HTML><BODY><H1 ALIGN=CENTER> Circle Info </H1>\n");
try{
String sdiam = request.getParameter("diameter");
double diam = Double.parseDouble(sdiam);
out.println("<BR><H3>Diam:</H3>" + diam +
"<BR><H3>Area:</H3>" + diam/2.0 * diam/2.0 * 3.14159 +
"<BR><H3>Perimeter:</H3>" + 2.0 * diam/2.0 * 3.14159);
} catch ( NumberFormatException e ){
out.println("Please enter a valid number");
}
out.println("</BODY></HTML>");
Servlet Lifecycle.
Taken from
IBM Website.
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.
J.Sant
Servlet Lifecycle

init() executed only once when servlet is started.


doPost(), doGet() executed with every request
depending on whether request was a post or a get.


Use this to load collections, open database connections etc.
Responding to form submission.
Destroy() executed only once when servlet is terminated.

clean-up routines
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.
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.
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.
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.
J.Sant
fullinstallpathforTomcat\common\lib\servlet-api.jar.
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
 The tomcat homepage should appear. Select the Servlet
Examples link from the left side bar and attempt to
execute some of the examples.
J.Sant