1
.
Servlet
Technical introduction
Servlet
Technical is a solution to which Sun provides a dynamic web page. It is a web server-side programming technology based on the Java programming language, mainly for access request information and dynamically generating response messages for clients on the web server side. Servlet technology is also the basis for JSP technology (another dynamic web development technology). A servlet program is a Java class that implements a special interface, which is used to support the servlet's web server call and run, ie only the web server side with the servlet engine. A servlet program is responsible for processing access requests for one or set of URL addresses it, receives access request information and generating response content.
Applet
It is a Java applet for the browser side, which is interpreted in the browser side, which is used to implement some desktop applications in the HTML web page, called "small applications". Servlet is a Java applet for the web server side, which is interpreted on the web server side, and is used to process the client's request and generate dynamic web content. From the applet naming, this web server-side Java applet is named servlet, corresponds to the applet, the servlet can be called "small service".
Servlet
Compared with the ordinary Java program, only the source of input information is different, so most tasks that ordinary Java programs can complete, the servlet program can be completed. The servlet program has some basic functions:
Get the client via HTML
FORM
Form submitted data and URL
The following parameter information;
Create a response message content for the client;
Access the server side file system;
Connect the database and develop database-based applications;
Call other Java
class.
2. Write and compile the servlet program
A servlet program is a special Java class running on the web server. This special Java class must implement a javax.servlet.servlet interface, and the servlet interface defines the agreement agreement between the servlet container and the servlet program. In order to simplify the editor of the servlet program
Write, the SERVLET API also provides the easiest servlet class that implements the servlet interface. Its full name is Javax.Servlet.GenericServlet, which implements the basic features and features of the servlet program. A servlet class dedicated to the HTTP protocol is also provided in the Servlet API. Its name is javax.servlet.http.httpservlet, which is a subclass of GenericServlet, and has some expansion for HTTP features on the basis of the GenericServlet class. Obviously,
A Java class as long as inherited
GenericServlet
HTTPSERVLET
It is a servlet. Conversely, to write a servet class, this class must inherit the GenericServlet class or
Httpservlet
class. In order to make full use of the HTTP protocol, in general, the servlet class you write should inherit the httpservlet class instead of inheriting the GenericServlet class.
View httpservlet
The help documentation of the class, you can see that there is a method called Service,
When the client accesses a servlet program each time, the servlet engine will call this method for processing. The Service method accepts two parameters, one is an object for encapsulating the HTTP request message, which is httpservletRequest, and the other is an object representing the HTTP response message, which is httpservletResponse. Calling the GetWriter method of the HTTPSERVLETRESPONSE object can obtain a text output stream object to send data written in this streaming to the client content portion of the HTTP response message to the client. : Hand experience:
Write the process of compiling the servlet program
(1) Write a Helloservlet class that inherits the HTTPServlet class, which is overwritten on the service method in the HTTPServlet class, as shown in routine 4-2.
Routine 4-2
Helloservlet.java
Import java.io. *;
Import javax.servlet. *;
Import javax.servlet.http. *;
Public Class Helloservlet Extends Httpservlet
{
//
Copying the service method from the HTTPSERVLET class to avoid writing errors
Public void service (httpservletRequest Request,
HttpservletResponse response) THROWS servletexception, ioException
{
PrintWriter out = response.getwriter ();
Out.println ("");
OUT.PRINTLN (" www.it315.org font>
");
Out.println ("
Out.println (" html>");
}
}
(2) use
Javac
command
Compile this source file, usually, people who have learned a servlet program will encounter as the following error:
HelloWorld.java: 2: package javax.servlet does not exist
Import javax.servlet. *;
^
HelloWorld.java: 3: package javax.servlet.http does not exist
Import javax.servlet.http. *;
^
......
These error messages could not find two packets of Javax.Servlet and Javax.Servlet.http and some of them, because JAR files included in the servlet API class have not been added to Javac. ClassPath environment variables.
(3) In executing the Javac command line window, the JAR file containing the servlet API is added to the path list of the ClassPath environment variable, for example, for Tomcat 4.x, you should use the following command settings:
Set classpath = Installation Directory> /common/lib/servlet.jar;%CLASSPATH% After setting the CLASSPATH environment variable, recompile Helloservlet.java file, If there is no writing error in the source program, you can compile it. In the "Java Employment Training Tutor" prepared by the author, the ClassPath environment variable set in the command line window is temporary, and only the command line window works, it does not work for other windows and applications, so Reset it in each newly launched command line window. If you want to keep the settings remain valid, you should use the setting method of environmental variables that are valid for the entire system, for example, in the Environment Variables dialog box of Win2000 system features, set in Win98's boot automatic batch file autoexec. Set in the BAT, set in Linux's profile file. (4 ) Compilation and run Java Java The typeloader will also go to Install Main Catalog> / JRE / LIB / EXT JAR under the directory Search in the package to load the class to load. If you will contain a Serlet API JAR File copy to this directory, compiling servlet When procedures, you don't have to be in ClassPath. Increase in environment variables containing a SERLET API JAR file.