servlet
servlet
Center
New-to-Java Programming Center
Contents
What You Need to Write Servlets
To develop and test servlets, you will need special software and editing tools:
Tomcat@Jakarta
This download enables you to run servlets and JavaServer PagesTM for
development and testing.
Or:
JavaServerTM Web Development Kit
Develop and test your servlets and JavaServer Pages with this download.
A simple text editor like TextPad or Notepad, or one that comes with
HTML development software.
To run servlets on a server, you will need a Java technology-enabled server with
a standalone, add-on, or embedded servlet engine.
If you need a servlet to load with customized initialization behavior, you can
override the init method using either of the following two formats:
1. No argument format:
The latter is used when the servlet needs to be initialized with server
information such as:
Password files
A hit count number
Serialized cookie information
Data from previous requests
When using the ServletConfig format, create a call to the super.init so that
the super class registers the information where the servlet can find it later.
These methods are the heart of the servlet, where instructions and the purpose of
the servlet are carried out. You will likely only need to use, or override, a few of
the methods. RedirectServlet overrides doGet and doPost, but does not need
any of the other methods.
public class Example extends HttpServlet {
public void doGet ( HttpServletRequest req,
HttpServletResponse res)
When a client calls a servlet by typing the URL in the browser, submitting a
form, or clicking a button on a menu, the servlet’s service method checks the
HTTP request type, such as POST or GET. This in turn calls doGet, doPOST,
doPUT, or doDelete as needed.
You can override the service method without implementing doGet and
doPost, but it’s generally better to call both doGet and doPost. See the JDC
RedirectServlet.
When you select a destination and click the Go button, the menu form invokes
the servlet by through the Action line, highlighted in purple. This line contains
the path to where the servlet lives on the server or testing servlet engine.
Note: Tomcat or JavaServerTM Web Development Kit use
localhost:8080 as the host and port. Read the software
documentation to find out what directories the servlets and the
HTML pages should be saved in.
NAME attributes should be descriptive of the information that is entered into the
values, or values that are given. In this example, url describes what the values
contain, a URL.
Other types of form fields, like radio buttons or check boxes, provide the value,
as in the following example:
Make certain that NAME and its values are meaningful so if the data is written to
email messages, log files, or databases the information makes sense to the user
viewing them.
The RedirectServlet uses the URL of the chosen destination for its value, so
the value is not provided until the user select an item from the menu.
So the servlet collects the parameter NAME and corresponding value as a String
pair, the String object must be declared:
private String paramName;
To initialized the servlet with the NAME and value String pair, the init method is
overridden, and the ServletConfig object is passed as a parameter. Because
errors can occur, such as a bad URL, the throws ServletException is
included.
public void init(ServletConfig config)
throws ServletException {
When overriding the init method, call super.init(config). After the call to
super.init(config), the servlet can invoke its own getInitParameter
method as shown. The getInitParameter method returns a string containing
the value of the named initialization parameter, or null if the requested
parameter does not exist. Init parameters have a single string value.
super.init(config);
paramName =
config.getInitParameter("paramName");
}
The doGet method calls the getParameter method through the request object,
passing in the paramName object and its value as a string pair, in this case url
and the actual URL. A special sendRedirect method then passes the string to
the browser and redirects the user to the desired destination.
}
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException{
doGet(request, response);
}
}
As long as the value is not null, the code moves onto the res object and its
sendRedirect method, which redirects the client to the URL that was passed to
the method. This makes for smooth navigation when a client’s browers cannot
support JavaScript, or the user has JavaScript toggled off.
The JDC uses servlets for a number of other functions as well, such as
processing the Reader Feedback form below, conducting site-wide searches, and
enabling members to subscribe to newsletters.
Reader Feedback
Tell us what you think of this article.
Very worth reading Worth reading Not worth reading
if you have other comments or ideas for future articles, please type
them here:
Submit
Reset