servlet
Develop
One,
servlet
Development initial
Servlet is an application technology in the Java language in the web server, and future servlets will thoroughly replace CGI. This lecture will specifically introduce the concept, development, commissioning, and corresponding application examples of servlets.
First, what is servlet?
· Servlet is a Java application that is independent of the platform and protocol, which can generate a dynamic web page.
· Servlet is a Java application located within the server side within the web server. Unlike the traditional Java application launched from the command line, the servlet is loaded by the web server, which must contain Java virtual machines that support servlet.
• The relationship between servlet and web server is similar to the relationship between Applet and web browsers (this is why servlet technology is called servlet), we can imagine servlets to do not have an APPLET without front-end interface (Faceless). Unlike Applets, because servlet runs in the web server side, it is a trusted program that is not limited by Java security, with the same permissions as ordinary Java applications.
· Servlet is an alternative technology for CGI Script, because servlet has a significant advantage over the performance, portability, code reuse, etc., so servlets may completely replace CGI during future technology development.
Second, write the development environment required by servlet
The basic environment required for servlet development is JSDK and a web server that supports servlet.
1.JSDK (Java Servlet Development Kit)
JSDK contains the Java class libraries that compile the servlet application and related documents. For users developed using Java 1.1, JSDK must be installed. JSDK has been integrated into the Java 1.2 Beta, so if you develop using Java 1.2, you don't have to install JSDK.
JSDK can download free download of JavaSoft's site, its address is
http://jserv.javasoft.com/products/java-server/downloads/index.html
2. Support the WEB server for servlet
Servlet needs to run on a web server that supports servlet. Currently supporting the WEB server of Servlet to push Sun's Java Web Server. If the existing web server does not support servlet, you can use some third-party manufacturers' server add-up (add-ons) to enable the web server to support servlet, where Live Software (http://www.livesoftware) is available. A product called JRUN allows Microsoft IIS and Netscape Web Server to support servlet by installing the corresponding version of JRUN.
Third, the process of developing a servlet
This article will describe the process of developing a servlet with a simple servlet (we call HelloServlet).
Write a servlet code
The Java Servlet API is a standard Java extension package that contains two packages :javax.servlets and javax.servlet.http. For developers who want to develop customer-defined protocols, you should use the class and interface in the Javax.Servlet package; for developers with the HTTP protocol to interact with the client, just need to use javax.servlet.http package The class can be developed with the interface.
Below 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. maxOutputStream ();
Out.println ("");
Out.println ("
Out.println ("
Out.println ("
Out.println (" Body> HTML>");
}
}
The servlet is implemented as follows: When the user accesses the servlet through the browser, the servlet returns an HTML page to the client browser:
Hello, you come from 192.168.0.11
Where 192.168.0.11 is the client IP address. The point points of the program code are as follows:
· The servlet based on the HTTP protocol must introduce Javax.Servlet and Javax.Servlet.http package;
· Helloservlet is derived from class httpservlets, HTTPSERVLET is a derived class of GenericServlet, and the servlet interface is implemented via GenericServlet. HTTPSERVLET provides basic support for servlet based on HTTP protocol;
The service () method is the entry point of the servlet program. When the user calls servlet from the browser, the servlet will enter the method. Service () contains two parameters, the HTTPServletRequest object contains information of the client request, which can obtain some information of the client (eg the IP address, browser type, etc.) and HTTP request type (for example, Get, Head, POST, PUT) through this parameter. Wait); HTTPSERVLETRESPONSE object is used to complete the interaction of servlet and client, and send an HTML page to the client by calling httpservletresponse.getoutputStream () customers to obtain the output stream to the client.
2. Compile the servlet code
Using JDK 1.1 to compile the servlet code (assuming the web server uses Java Web Server), its command behavior:
C: /> javac -d c: / javawebserver / servlets helloservlet.java
When compiling, you must ensure that JSDK's Java Servlet class is already included in ClassPth. The above command places the compiled .CLASS code in the servlets directory of Java Web Server (if you use other web server, you need to place the .class code in In the designated directory specified by the web server).
3. Add a servlet to Web Server
Since servlet is called via Web Server, it must be registered in Web Server so that Web Server can find the servlet code correctly. For Java Web Server, it provides a system management applet that registers our Helloservlet through this applet (here, we name the Helloservlet). 3. Test servlet
You can now test the Helloservlet, open your browser, type
http://192.168.0.9/kervlet/firstservlet
Where 192.168.0.9 is a machine IP address installed with Java Web Server.
If everything is normal, return a page in the browser, output "Hello, You Come 192.168.0.11". Where 192.168.0.11 is a machine IP address running a browser.
two,