Sie sind auf Seite 1von 12

Servlets Programming

Servlets are the programming concepts of J2EE , and it used for developing the web
applications .
The Servlets are server side language , so it is required to install the web server software
for it.
The webserver is the software which is required for performing the virtual client server
environment, some of the commonly used webserver softwares are ,
1. JBOSS
2. Java Web Server
3. Jakarta Tomcat
In order to run the servlets , we are required to set some environment variables.
1. JAVA_HOME
This environment variable is used for setting the home directory where the java
development kit software is installed.

In order to set these environment variables , we have to open the system


properties dialog box , by right clicking on mycomputer icon and for the context
sensitive menu , select properties.
In the dialog box , click on Advanced tab and then click on the Environment
Variables button.
2. CLASSPATH
This environment variable is used for setting the path for the .class files. Actually
we are using it to specify where the classes related to servlet are present .

3. CATALINA_HOME
This environment variable is used for setting the path for the home directory for the
tomcat directory.

Starting With the Servlet Programming


import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class SamplesDemo extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws IOException, ServletException {
res.setContentType("text/html");
PrintWriter in = res.getWriter();
/* Display some response to the user */
in.println("<html><head>");
in.println("<title>TestServlet</title>");

in.println("\t<style>body { font-family: 'Lucida Grande', " +


"'Lucida Sans Unicode';font-size: 13px; }</style>");
in.println("</head>");
in.println("<body>");
in.println("<p>Welcome to Servlets</p>");
in.println("</body></html>");
in.close();
}
}

Servlets use classes and interfaces from two packages:


1. javax.servlet
2. javax.servlet.http

. The javax.servlet package contains classes to support generic, protocolindependent servlets.


These classes are extended by the classes in the javax.servlet.http package to
add HTTP-specific functionality.
The top-level package name is javax instead of the familiar java, to indicate that the
Servlet API is a standard extension.
Every servlet must implement the javax.servlet.Servlet interface. Most servlets
implement it by extending one of two special classes: javax.
servlet.GenericServlet or javax.servlet.http.HttpServlet .
A protocol-independent servlet should subclass GenericServlet, while an HTTP
servlet should subclass HttpServlet, which is itself a subclass of GenericServlet
with added HTTP-specific functionality.

Handling Requests

In contrast, an HTTP servlet usually does not override the service() method.
Instead, it overrides doGet() to handle GET requests and doPost() to handle POST
requests. An HTTP servlet can override either or both of these methods, depending on
the type of requests it needs to handle. The service() method of HttpServlet
handles the setup and dispatching to all the doXXX() methods, which is why it
usually should not be overridden

HTTP Protocol Parameters.


Request parameters for the servlet are the strings sent by the client to a servlet container
as part of its request. When the request is an HttpServletRequest object, the container
populates the parameters from the URI query string and POST-ed data.
The parameters are stored as a set of name-value pairs. Multiple parameter values CAN
exist for any given parameter name. The following methods of the ServletRequest
interface are available to access parameters:

getParameter

Returns the value of a request parameter as a String, or null if the parameter


does not exist. Request parameters are extra information sent with the request. For
HTTP servlets, parameters are contained in the query string or posted form data.
You should only use this method when you are sure the parameter has only ONE
value. If the parameter might have MORE than one value, use
getParameterValues(String).
If you use this method with a multivalued parameter, the value returned is equal to
the FIRST value in the array returned by getParameterValues.

If the parameter data was sent in the request body, such as occurs with an HTTP
POST request, then reading the body directly via getInputStream() or
getReader() can interfere with the execution of this method.

getParameterNames

Returns an Enumeration of String objects containing the names of the


parameters contained in this request. If the request has no parameters, the method
returns an EMPTY Enumeration.

getParameterValues

Returns an array of String objects containing all of the values the given request
parameter has, or null if the parameter does not exist. If the parameter has a
single value, the array has a length of 1.

getParameterMap

Returns an immutable java.util.Map containing parameter names as keys and


parameter values as map values. The keys in the parameter map are of type
String. The values in the parameter map are of type String array.
Configuring the WEB-INF
====================
Before running the servlet , it should be servlet container for this we have to add the
enteries in the web.xml file.
For this purpose we have to make use of the following tag ,
<servlet-mapping>
<servlet-name>milk</servlet-name>
<url-pattern>/drink/*</url-pattern>
</servlet-mapping>
servlet-mapping has two child tags,
(i) url-pattern
(ii) servlet-name.

1. url-pattern

It specifies the type of urls for which, the servlet given in servlet-name should
be called.

2. servlet-name

It will specify the name of the servlet.


.
Rule for URL path mapping:
It is used in the following order. First successful match is used with no further attempts.
1. The container will try to find an exact match of the path of the request to the path
of the servlet. A successful match selects the servlet.
2. The container will recursively try to match the longest path-prefix. This is done
by stepping down the path tree a directory at a time, using the / character as a
path separator. The longest match determines the servlet selected.
3. If the last segment in the URL path contains an extension (e.g. .jsp), the servlet
container will try to match a servlet that handles requests for the extension. An
extension is defined as the part of the last segment after the last . character.
4. If neither of the previous three rules result in a servlet match, the container will
attempt to serve content appropriate for the resource requested. If a default
servlet is defined for the application, it will be used.

Steps for running the servlet :

1. Create the folder with name sdemos in the directory F:\Tomcat 6.0\webapps

2. Create the folder with name WEB-INF in the folder F:\Tomcat


6.0\webapps\sdemos.

3. Now create the folder classes and copy the web.xml file in the folder F:\Tomcat
6.0\webapps\sdemos\WEB-INF

4. Create you file HelloWorld.java in the folder class and compile it.

Now open the file web.xml

<?xml version="1.0"?>

<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">

<servlet>
<servlet-name>SamplesDemo</servlet-name>
<servlet-class>SamplesDemo</servlet-class>
</servlet>
<servlet>
<servlet-name>DateExample</servlet-name>
<servlet-class>DateExample</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>SamplesDemo</servlet-name>
<url-pattern>/SamplesDemo</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>DateExample</servlet-name>
<url-pattern>/DateExample</url-pattern>
</servlet-mapping>
</web-app>

Das könnte Ihnen auch gefallen