How to compile an HTTP servlet Program

Source: Internet
Author: User
When compiling a servlet, you need to use two basic software packages for all servlets: javax. servlet and javax. servlet. HTTP. The following describes the HTTP servlet application programming interface provided by javax. servlet. HTTP.

First, we will introduce the basic methods of Java Servlet.

  • Init () method
    In the servlet lifecycle, only the init () method is executed once, that is, when the server loads the servlet. By configuring the server, you can set the servlet to be imported when the server is started or the client accesses the servlet for the first time. No matter how many clients access the servlet, INIT () is not repeated ().
  • Service () method
    The Service () method is the main part of the servlet. Each request made to an httpservlet object calls the Service () method of the object and passes a "request" object and a "response" object to the method as parameters. The "request" Object provides information about the request, and the "response" Object provides a communication channel for returning the response information to the browser. The related classes in the javax. servlet software package are servletrequest and servletresponse, while those in the javax. servlet. Http software package are httpservletrequest and httpservletresponse. Servlet communicates with the server through these objects and finally communicates with the client. Servlet can obtain information about the client environment and server environment and all information provided by the client by calling the method of the "request" object. by calling the method of the "response" object, servlet can send a response to the client.

    The default service function of the service () method in httpservlet is to call the do function corresponding to the HTTP request method. For example, if the HTTP request method is get, doget () is called by default (). When a customer sends an http post request through an HTML form, the dopost () method is called. Parameters related to the POST request are sent to the server as a separate HTTP request from the browser. To modify the data on the server, use the dopost () method.

    Servlet responses can be of the following types:
    · An output stream is interpreted by the browser based on its content type (such as text/html.
    · An HTTP Error Response is redirected to another URL, Servlet, and JSP.

  • Destroy () method
    The destroy () method is executed only once, that is, it is executed when the server is stopped and the servlet is uninstalled. When the server uninstalls the servlet, the destroy () method is called after all service () methods are called, or after the specified interval. A servlet may produce other threads when running the service () method. Therefore, when calling the destroy () method, you must confirm that these threads have been terminated or completed.
  • Getservletconfig () method
    The getservletconfig () method returns a servletconfig object, which is used to return initialization parameters and servletcontext. The servletcontext interface provides servlet environment information.
  • Getservletinfo () method
    The getservletinfo () method is an optional method that provides servlet-related information, such as the author, version, and copyright.

The following describes how to write a basic HTTP servlet.

(1) introduce the corresponding packages and classes, including:


     
      
Import javax. servlet. *; import javax. servlet. http. *; import java. Io. *; inherit from javax. servlet. http. httpservlet public class myservlet extends httpservlet {
     

(2) implement the service method.

The main function of servlet is to accept the HTTP request sent from the browser and return the HTTP response ). This task is completed in the service method. The service method includes obtaining client data from the request object and creating and outputting data to the response object.

If a servlet inherits from javax. servlet. http. httpservlet and implements the dopost or doget method, the servlet can only respond to post or get. If a developer wants to process all types of requests, simply implement the service method (but if the service method is selected, the dopost or doget method is not required, unless super is called at the beginning of the service method. service ()). The difference is 14-2.


Figure 14-2 Differences Between doget, dopost, and service

The HTTP servlet specification describes the methods used to process other request types. All these methods can belong to the service method. All service methods use the same parameters. Httpservletrequest provides information about the request. servlet can use httpservletresponse to respond to the HTTP client.


     
      
Public void Service (httpservletrequest req, httpservletresponse res) throws ioexception {// set the type of response content res. setcontenttype ("text/html"); // get Java. io. A reference to the printwriter object to output printwriter out = res. getwriter (); // use the println () method of the printwriter object to create some HTML code, such as out. println ("<HTML> 

(3) Compile the servlet.
Compile the servlet from the directory that stores the Servlet Source code file to the WEB-INF/classes directory in the application that contains the servlet. For example:
Javac-D/your_application_dir/WEB-INF/classes your_servlet.java

(4) Deploy the Servlet as part of the application.
After the program is compiled to the specified directory, the service is still not enabled. We must configure it in C: /BEA/wlserver6.1/config/mydomain/applications/defaultwebapp/There is a web under the WEB-INF. XML file. This is the application configuration file. The general format of this file is as follows:


     
      <?xml version="1.0" ?><!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 1.2//EN" "http://java.sun.com/j2ee/dtds/web-app_2_2.dtd"><web-app>  <!--chapter 8  java  servlets -->  <servlet>  <servlet-name>HelloWorld2</servlet-name>  <servlet-class>examples.servlets.HelloWorld2</servlet-class>  <init-param>   <param-name>greeting</param-name>   <param-value>welcome</param-value>   </init-param>   <init-param>    <param-name>person</param-name>    <param-value>weblogic developer</param-value>    </init-param>  </servlet>  <servlet-mapping>  <servlet-name>HelloWorld2</servlet-name>  <url-pattern>/HelloWorld2/*</url-pattern>  </servlet-mapping>    <welcome-file-list>    <welcome-file>index.html</welcome-file>  </welcome-file-list></web-app>
     

Servlet-name is the servlet name, servlet-class is the relative path to store class files under/WEB-INF/classes. We can also initialize parameters in this file in the following format:


     
      <init-param> <param-name>greeting</param-name> <param-value>welcome</param-value></init-param>
     

Param-name is the parameter name. In this example, It is greeting; param-value is the parameter value, and in this example it is welcome. We can initialize more parameters in this form.


     
      <servlet-mapping><servlet-name>HelloWorld2</servlet-name><url-pattern>/HelloWorld2/*</url-pattern></servlet-mapping>
     

This code is the servlet ing between servlet name and URL-pattern path. According to this Code, the URL path of the servlet program helloworld2 is http: // server_address: /Helloworld2.

(5) access the servlet from a browser. Generally, the URL for calling a servlet depends on the name of the Web application that contains the servlet and the name of the servlet ing in the Web Application Deployment description. The request parameter can also be part of the URL of the servlet. Generally, the servlet URL is in the following mode:
Http: // server_address: /Your_web_application_name/mapped_servlet_name? Parameter

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.