The development environment required to write a servlet
The basic environment required for servlet development is JSDK and a Web server that supports the servlet.
1.JSDK (Java Servlet Development Kit)
JSDK contains the Java class libraries and related documentation needed to compile the servlet application. For users who are developing with Java 1.1, JSDK must be installed. JSDK has been integrated into the Java 1.2 beta, and if you are developing with Java 1.2 or later, you do not have to install JSDK.
JSDK can be downloaded at JavaSoft Company's site for free, with the address: http://www.sun.com/software/jwebserver/redirect.html
2. Web server with servlet support
The servlet needs to run on a Web server that supports a servlet. The Web server that currently supports the servlet, JSWDK1.0.1 of Sun Corporation. If the existing Web server does not support a servlet, you can leverage the server additions (add-ons) of some third-party vendors to enable the Web server to support the servlet, which is the Live software company (http:// Www.livesoftware.com provides a product called JRun that enables Microsoft IIS and Netscape Web server to support the servlet by installing the appropriate version of JRun.
The process of developing a servlet
Here's a simple servlet example to illustrate the process of developing a servlet.
1. Writing servlet code
The Java Servlet API is a standard Java Extender package that contains two Package:javax.servlet and Javax.servlet.http. For developers who want to develop custom based protocols, the classes and interfaces in the Javax.servlet package should be used, and developers interacting with the client only with HTTP protocols need to be developed using the classes and interfaces in the Javax.servlet.http package.
The following is a servlet's program code (REQUESTINFOEXAMPLE.JAVA):
Import java.io.*;
Import java.servlet.*;
Import javax.servlet.*;
public class Requestinfoexample extends HttpServlet {
public void doget (HttpServletRequest request, httpservletresponse response)
Throws IOException, Servletexception
{
Response.setcontenttype ("text/html");
PrintWriter out = Response.getwriter ();
Out.println ("Out.println ("<body>");
Out.println ("Out.println ("<title> Request information Example </title>");
Out.println ("Out.println ("<body>");
Out.println ("Out.println ("Method:" + Request.getmethod ());
Out.println ("Request URI:" + Request.getrequesturi ());
Out.println ("Protocol:" + request.getprotocol ());
Out.println ("PathInfo:" + request.getpathinfo ());
Out.println ("Remote Address:" + request.getremoteaddr ());
Out.println ("</body>");
Out.println ("}
public void DoPost (HttpServletRequest request, HttpServletResponse Res)
Throws IOException, Servletexception
{
Doget (request, response);
}
}
The servlet implements the following functionality: When a user accesses the servlet through a browser, the servlet returns an HTML page to the client browser:
------------------------------------------------
Request Information Example
Method: GET
Request URI: /examples/servlet/RequestInfoExample
Protocol: HTTP/1.1
Path Info: null
Remote Address: 127.0.0.1
--------------------------------------------------
For a servlet program description:
* The servlet based on HTTP protocol must introduce Javax.servlet and javax.servlet.http packages;
* HelloServlet derived from class HttpServlet, HttpServlet is a derived class of genericservlet that implements the Servlet interface through Genericservlet. HttpServlet provides basic support for the servlet based on HTTP protocol;
* The HttpServletRequest object contains information requested by the client, through which you can obtain some information about the client (such as IP address, browser type, etc.) and the type of HTTP request (e.g., POST, put, etc.) The HttpServletResponse object is used to complete the interaction of the servlet with the client, by invoking the Httpservletresponse.getoutputstream () client to obtain the output stream to the client, Sends an HTML page to the client.
* The Doget method is written, and the Dopost () method of the servlet is invoked for an HTML POST request.
2. Compiling servlet code
Using JDK 1.2.2 to compile servlet code (assuming the Web server is jswdk-1.0.1), its command behavior:
c:\> javac-d C:\jswdk-1.0.1\examples\WEB-INF\servlets Helloservlet.java
When compiling, you must make sure that the Helloservlet.java files are copied under the directory C:\jswdk-1.0.1\examples\WEB-INF\servlets.
3. Test servlet
Now you can test the HelloServlet, open the browser, and type:
Http://localhost:8080/examples/servlet/RequestInfoExample
where localhost is installed with jswdk-1.0.1 machine, 8080 is the port number.