How can I configure Servlet to achieve my own programming goals? Let's start learning:
The basic environment required for Servlet development is JSDK and a Web server supporting Servlet.
1. JSDK (Java Servlet Development Kit)
The JSDK contains the Java class libraries and related documents required for compiling Servlet applications. For users who use Java 1.1 for development, JSDK must be installed. JSDK has been integrated into Java 1.2 Beta. If you use Java 1.2 or later for development, you do not have to install JSDK.
The JSDK can be downloaded free of charge at your oft website. Its address is: http://www.sun.com/software/jwebserver/redirect.html.
2. Web servers supporting Servlet Configuration
Servlet needs to run on the Web server that supports Servlet. Currently, JSWDK1.0.1 of the Servlet Web Server SUN is supported. If the existing Web server does not support Servlet, you can use some third-party vendor's server add-ons to make the Web server support Servlet, which Live Software (http://www.livesoftware.com) a product called JRun is provided. by installing the corresponding JRun version, Microsoft IIS and Netscape Web Server can support Servlet.
Servlet Development Process
The following is a simple Servlet example to illustrate the Servlet development process.
1. Compile Servlet configuration code
Java Servlet API is a standard Java extension Package, which contains two packages: javax. servlet and javax. servlet. http. For developers who want to develop user-defined protocols, they should use javax. the class and interface in the servlet package. for developers who only use the HTTP protocol to interact with the client, they only need to use javax. servlet. develop the class and interface in the http package.
The following is a servlet 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("<html>");
- out.println("<body>");
- out.println("<head>");
- out.println("<title>Request Information Example</title>");
- out.println("</head>");
- out.println("<body>");
- out.println("<h3>Request Information Example</h3>");
- 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("</html>");
- }
- public void doPost(HttpServletRequest request, HttpServletResponse res)
- throws IOException, ServletException
- {
- doGet(request, response);
- }
- }
The Servlet configuration provides the following functions: 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
- --------------------------------------------------
Servlet configuration program description:
◆ The javax. servlet and javax. servlet. HTTP packages must be introduced to http-based servlets;
◆ HelloServlet is derived from the class HttpServlet. HttpServlet is a derived class of GenericServlet and implements the Servlet interface through GenericServlet. HttpServlet provides basic support for HTTP-based servlets;
◆ The HttpServletRequest object contains the client request information. You can use this parameter to obtain client information (such as IP addresses and browser types) and HTTP request types (such as GET, HEAD, POST, and PUT );
The HttpServletResponse object is used to complete the interaction between the Servlet and the client. By calling HttpServletResponse. getOutputStream (), the client obtains the output stream to the client and sends an HTML page to the client.
◆ Write the doGet method. For html post requests, call the Servlet doPost () method.
2. Compile the Servlet configuration code
Using JDK 1.2.2 to compile Servlet code (assuming the Web server uses jswdk-1.0.1), its Command Behavior:
C:> javac-d C: jswdk-1.0.1examplesWEB-INFservlets HelloServlet. java
Make sure that the HelloServlet. java file is copied to the directory C: jswdk-1.0.1examplesWEB-INFservlets.
3. Test Servlet Configuration
Now we can test HelloServlet. Open the browser and type:
Http: // localhost: 8080/examples/servlet/RequestInfoExample
Localhost is the machine where the jswdk-1.0.1 is installed, and 8080 is the port number.
The problem with Servlet configuration is demonstrated above. Do you know something about Servlet configuration?
- What are servlets and common Servlet APIs?
- Enhanced Servlet and JSP security without modifying code
- Configuration of JSP, Servlet, and Bean in Tomcat
- How to Improve Servlet and JSP Application Efficiency
- Functions and principles of several encodings in JSP and Servlet