public class firstServlet extends HttpServlet
It identifies information about the node transmitting the the HTTP request. This could be used in aiding security efforts by specifying that certain actions can only be carried out if the node transmitting the request is a link local address, site local address, or loopback address. Since these types of addresses can't be sent over the public internet, this would help insure that hackers couldn't carry out the operations remotely.
This is a very simple example based on the idea that simple examples should be under two pages in length.
You will notice to references to the javax.servlet and javax.servlet.http packages appear as hyperlinks. This is because I included the following options in the javadoc command.
-link "http://java.sun.com/j2se/1.4.2/docs/api"
-link "http://tomcat.apache.org/tomcat-5.5-doc/servletapi"
The relationship between the Java servlet to be executed and the HTTP call to the application server is defined by the server.xml file for the application server and the WEB-INF/web.xml file that exists for each web application. The following is the WEB-INF/web.xml file that was used with this servlet.
<web-app>
<servlet>
<servlet-name>first</servlet-name>
<servlet-class>bradleyross.servlets.firstServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>first</servlet-name>
<url-pattern>/firstServlet</url-pattern>
</servlet-mapping>
</web-app>
The meaning of this XML document is as follows.
The servlet is named first
(servlet-name
). This name doesn't appear
in the Java code but is means of describing the configuration to
the application server.
The Java class for the servlet is
bradleyross.servlets.firstServlet
(servlet-class
). (Although it isn't
indicated from this document, this class
file was included in the CLASSPATH for the application server and
is therefore accessible to the system.)
The URL entered in the browser will contain /test
to indicate that this servlet is to be executed
(servlet-mapping
). This is known as the
servlet path, meaning that it refers to the path
within the web application.
The web application is contained in the directory
webapps/test
on the application server, meaning that the
XML file is at webapps/test/WEB-INF/web.xml
. Unless
there are entries in server.xml, the context path for the
web application is /test
, meaning that the
servlet is accessed as /test/firstServlet
. (The
context path followed by the servlet path.)
Modifier and Type | Field and Description |
---|---|
private static long |
serialVersionUID |
Constructor and Description |
---|
firstServlet() |
Modifier and Type | Method and Description |
---|---|
void |
destroy() |
private String |
doubleCell(String first,
String last) |
private String |
firstCell(String contents) |
void |
init(ServletConfig config) |
private String |
lastCell(String contents) |
void |
service(HttpServletRequest req,
HttpServletResponse res) |
doDelete, doGet, doHead, doOptions, doPost, doPut, doTrace, getLastModified, service
getInitParameter, getInitParameterNames, getServletConfig, getServletContext, getServletInfo, getServletName, init, log, log
private static final long serialVersionUID
public firstServlet()
private String doubleCell(String first, String last)
public void init(ServletConfig config) throws ServletException
init
in interface Servlet
init
in class GenericServlet
ServletException
public void destroy()
destroy
in interface Servlet
destroy
in class GenericServlet
public void service(HttpServletRequest req, HttpServletResponse res) throws IOException
service
in class HttpServlet
req
- Information concerning the HTTP request received
by the server.res
- Information concerning the HTTP response generated
by the server.IOException