Create an HTTP Servlet

Source: Internet
Author: User

Creating an HTTP Servlet usually involves the following four steps:

1. Extend the HTTP servlet abstract class.

2. Reload the appropriate method.] For example, override (or rewrite) The doGet () or doPost () method.

3. Obtain the HTTP request information. Use the HttpServletRequest object to retrieve the HTML table
The submitted data or query string on the URL. The "request" object contains specific methods to retrieve information provided by the client. There are three available methods:
= GetParameterNames (),
= GetParameter (),
= GetParameterValues ().

4. generate an HTTP response. The HttpServletResponse object generates a response and returns it to the client sending the request. It
You can set the "request" title and "response" subject. The "response" object also contains the getWriter () method to return a PrintWriter object. Use the print () and println () Methods of PrintWriter to write the Servlet response and return it to the client. Alternatively, you can directly use the out object to output HTML document content.
A servlet sample (ServletSample. java) is as follows:
Import java. io .*;
Import java. util .*;
Import javax. servlet .*;
Import javax. servlet. http .*;

Public class ServletSample extends HttpServlet {// Step 1: Expand the HttpServlet abstract class.

Public void doGet (HttpServletRequest request, HttpServletResponse response)
Throws ServletException, IOException {// Step 2: override the doGet () method

String myName = ""; // Step 3: get http Request Information
Java. util. Enumeration keys = request. getParameterNames ();
While (keys. hasMoreElements ());
{
Key = (String) keys. nextElement ();
If (key. inclusignorecase ("myName "))
MyName = request. getParameter (key );
}
If (myName = "")
MyName = "Hello ";
// Step 4: generate an HTTP response.
Response. setContentType ("text/html ");
Response. setHeader ("Pragma", "No-cache ");
Response. setDateHeader ("Expires", 0 );
Response. setHeader ("Cache-Control", "no-cache ");

Out. println ("Out. println ("<body> ");
Out. println ("Out. println ("<p>" + myName + ", this is a very basic servlet that writes an HTML page .");
Out. println ("<p> For instructions on running those samples on your WebSphere Application Server," +
"Open the page :");
Out. println ("<pre> http: // <em> your. server. name </em>/IBMWebAs/samples/index.html </pre> ");
Out. println ("where <em> your. server. name </em> is the hostname of your WebSphere Application server .");
Out. println ("</body> Out. flush ();
}
}
The above ServletSample class extends the HttpServlet abstract class and overrides the doGet () method. In the rewritten doGet () method, obtain an optional parameter (myName) in the HTTP request. This parameter can be passed to the Servlet as a query parameter on the called URL. Example: http://your.server.name/servlet/ServletSample? Myname = Michael.


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.