Java Servlet programming and application II

Source: Internet
Author: User
Tags netscape web server microsoft iis

The basic environment required for Servlet development is JSDK and a Web server supporting Servlet.

Development environment required for compiling Servlet

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

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. Write Servlet 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 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 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, PUT, etc.); HttpServletResponse object is used to complete the interaction between Servlet and client, by calling HttpServletResponse. the getOutputStream () Client obtains the output stream output to the client and sends the HTML page to the client.

* The doGet method is compiled. For html post requests, the Servlet doPost () method is called.

2. Compile Servlet 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

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.

Related Article

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.