Servlet Development Preliminary

Source: Internet
Author: User
Tags command line config contains html page http request netscape web server java web microsoft iis
The servlet servlet is an application of the Java language on the Web server side, and the future servlet may completely replace CGI. This lecture will specifically introduce the concept, development, debugging and corresponding application examples of the servlet.


One, what is a servlet?


· A servlet is a platform-and protocol-independent, server-side Java application that generates dynamic Web pages.


· A servlet is a server-side Java application located inside a Web server, unlike a traditional Java application that starts from the command line, which is loaded by a Web server that must contain a Java virtual machine that supports the servlet.


· The servlet's relationship with the Web server is similar to the applet's relationship to the Web browser (which is why servlet technology is called a servlet), and we can imagine the servlet as an applet without a front-end interface (faceless). Unlike applets, because the servlet runs on the Web server side, it is a trusted program that is not limited by Java security and has the same permissions as a normal Java application.


· The servlet is an alternative to CGI scripts, and because the servlet has a significant advantage over CGI in terms of performance, portability, code reuse, and so on, the servlet is likely to completely replace CGI in future technology development.


Second, 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, so if you are developing with Java 1.2, you do not have to install JSDK.


JSDK can be downloaded at the JavaSoft Company's site for free, and its address is


Http://jserv.javasoft.com/products/java-server/downloads/index.html


2. Web server with servlet support


The servlet needs to run on a Web server that supports a servlet. Currently, the Web server that supports the servlet has the Sun Company's Java Web servers. 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.


Third, the process of developing a servlet


This article will illustrate the process of developing a servlet by writing a simple servlet (what we call HelloServlet) as an example.


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 typical servlet program code:


Import javax.servlet.*;


Import javax.servlet.http.*;


Import java.io.*;


Import java.util.*;


public class HelloServlet extends HttpServlet {


public void init (ServletConfig config) throws servletexception {


Super.init (config);


}


public void Service (HttpServletRequest req, httpservletresponse Res)


Throws Servletexception, IOException {


String clientipaddress = Req.getremoteaddr ();


Res.setcontenttype (″text/html″);


Servletoutputstream out = Res.getoutputstream ();


Out.println (″〈html〉″);


Out.println (″〈head〉〈title〉hello World〈/title〉〈/head〉″);


Out.println (″〈body〉″);


Out.println (″〈h1〉hello,you come From″+clientipaddress+″〈/h1〉″);


Out.println (″〈/body〉〈/html〉″);


}


}


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:


Hello, come from 192.168.0.11


Where 192.168.0.11 is the client IP address. The main points of the program code are as follows:


• A servlet based on HTTP protocol must introduce Javax.servlet and javax.servlet.http packages;


· HelloServlet derives 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 Service () method is the entry point of the servlet program that the servlet enters when the user invokes the servlet from the browser. Service () contains two parameters, 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 (for example,, 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.


2. Compiling servlet code


Using JDK 1.1 to compile the servlet code (assuming that the Web server is in Java Web servers), its command behavior is:


c:\> javac-d c:\JavaWebServer\servlets Helloservlet.java


When compiling, you must ensure that the JSDK Java Servlet class is already contained in Classpth, and the above command places the compiled. Class code in the Servlets directory of the Java WEB Server (if you use another Web server, You need to place the. Class code in the directory specified by the Web Server.


3. Add servlet to Web Server


Because the servlet is invoked through the Web server, it must be registered in the Web server so that Web server can correctly locate the servlet code. For Java Web Server, it provides a system management applet that registers our helloservlet with this applet (where we name HelloServlet as Firstservelt).


3. Test servlet


Now you can test the HelloServlet, open the browser, and type


Http://192.168.0.9/servlet/firstServlet;


Where 192.168.0.9 is the IP address of the machine that has the Java WEB Server installed.


If everything works, a page will be returned in the browser, with the output "hello,you come from 192.168.0.11". Where 192.168.0.11 is the machine IP address that runs the browser.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.