Escolar Documentos
Profissional Documentos
Cultura Documentos
NetBeans IDE
Kai Qian
Chapter 2
Java Servlets
Objectives
Java Servlet Web Components
Support Environments for Java
Servlets
Servlets with CGI Compared to Java
Applets
Functionality of Java Servlets
Java Servlet API
Java Servlet Debugging
Introduction to Java
Servlets
Java Servlets technology provides an HTTPbased request and response paradigm on web
servers.
Java Servlets can handle generic service
requests and respond to the clients requests.
Java Servlets are used in embedded systems,
wireless communication, and any other generic
request/response application.
The Common Gateway Interface (CGI) was a
popular technology used to generate dynamic
HTTP web contents in the mid-1990s.
Introduction to Java
Servlets, contd.
A Servlet component can delegate the requests to its back-end tier
such as a database management system, RMI, EAI, or other
Enterprise Information System (EIS).
A Servlet is deployed as a middle tier just like other web
components such as JSP components.
The Servlet components are building block components, which
always work together with other components such as JSP
components, JavaBean components, Enterprise Java Bean (EJB)
components, and web service components.
A Servlet component is also a distributed
component, which can provide services to remote
clients and also access remote resources.
Web Server
Configuration
An XML format file called server.xml
is used to control and configure the
behavior and setting of the Tomcat
web server.
This file is located in the conf
subdirectory of the Tomcat
installation directory.
Web Server
Configuration, contd.
1. Reset the server port number
where the Servlet class or other web
component will listen for requests.
2. Turn on Servlet reloading so that
you dont need to reload the
recompiled Servlet.
Servlet Lifecycle
A Servlet has a lifecycle just like a
Java applet. The lifecycle is managed
by the Servlet container.
There are three methods in the
Servlet interface, which each Servlet
class must implement. They are
init(), service(), and destroy().
Processing of HTTP
Requests and Responses
The prototypes of doGet() and doPost()
void doGet( HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException;
void doPost( HttpServletRequest request,
HttpServletResponse response )
throws ServletException, IOException;
Processing of HTTP
Requests and
Responses, contd.
HttpServletRequest and
HttpServletResponse are the two
interfaces that provide the Servlet with
full access to all information from the
request and the response sent back to the
client.
When the doGet() or doPost() is called, the
Servlet container passes in the
HttpServletRequest and
HttpServletResponse objects.
import java.io.*;
import java.net.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.text.DecimalFormat;
public class TestServlet extends HttpServlet
{
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws javax.servlet.ServletException, java.io.IOException
{
String temperature = req.getParameter("temperature");
DecimalFormat twoDigits = new DecimalFormat("0.00");
try
{
double tempF = Double.parseDouble(temperature);
String tempC = twoDigits.format((tempF -32)*5.0/9.0);
PrintWriter out = res.getWriter();
out.println("<html>");
out.println("<head>");
out.println("</head>");
out.println("<body>");
out.println("<h3>" + temperature + " Fahrenheit is
converted to " + tempC + " Celsius</h3><p>");
out.println("</body>");
out.println("</html>");
}
catch(Exception e)
{
res.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
"There was an input error");
}
}
}
Communication from
Clients to HTTP Servlets
For GET type HTTP requests you can also
directly type http://<host>/<servletName>
in the URL location field of the browser if you
dont want to pass in any parameter values.
If you do need to pass in some parameter
values with the request you can type
http://<host>/<servletName>?
<paramterName>=<value> such as
http://localhost:8080/conv/conversion?feet=
100
.
Ways Servlets
Communicate with Other
Web Components
There are four web component
scopes: page, request, session, and
application.
The page scope only covers the
current page.
Web components can share and
delegate the data within a request,
session, or application scope.
Attribute Scopes in
Servlet
Servlet Cookie
Debugging Servlets
The best solution to fix a runtime
logic error is to plug in the Tomcat
Servlet container to JBuilder, Eclipse,
or VisualAge, or just use an
integrated debugger in your IDE such
as Sun One Studio, BEA WebLogic, or
IBM WebSphere.
Debugging Servlets,
contd.
Check the generated HTML source code by using
View Source from the web browser menu. Some
simple HTML errors can be easily found this way,
which can help to locate the Servlet error.
Use the sendError(int sc) method of
HttpServletResponse to send an error response to
the client with the specified status code and a
default message.
Use the out.println() method to print debugging
messages on the client page.
Use the jdb Java debug utility to debug the Servlet.
Debugging Servlets,
contd.
Use the void log(String message) method of
HttpServlet to write debug information into the log
file in the Tomcat installation directory.
Use System.out.println() or System.err.println() to
write error messages. The error messages will be
displayed on the server console if Tomcat is started
in developer mode.
Check the request data and response data
separately. Determine whether clients have sent
the right data or the Servlets responded the right
data. You can have separate classes to detect
these problems.
Chapter 2
The End