Java Servlet Programming and application of the three

Source: Internet
Author: User
Tags date object config current time string client


Genericservlet class can be said to be the most important class in Jsdk, and also the most basic class. Programmers want to write servlet applications, typically inheriting JSDK-provided Genericservlet classes or its subclass HttpServlet classes.



Programming Idea: The following is a simple example, to execute it on the server side, is to the client's browser output "HELLO world" and the time of the server and other information.



Helloworldservlet.java's source code is as follows:


import java.io.*;
import java.util.Date;
import javax.servlet.*;
public class HelloWorldServlet extends GenericServlet
{
 String initString;
 public void init(ServletConfig config) throws ServletException
 {
 // The init method of the parent class completes the storage of the object ServletConfig.
Super.init (config);
InitString = new String ("I have veen initialized");
}
// In the Sercice method, realize the response to the customer request. It throws two exceptions ServletException
Public void service (ServletRequest req, ServletResponse res)
Throws ServletException, IOException
{
// Class Date to get the current time of the server.

  Date today = new Date( );
  ServletOutputStream out = res.getOutputStream( );
  out.println("HELLO WORLD");
  out.println(today.toString());
  out.println(getServletInfo());
 }
 public void Destroy( )
 {
 }
 public String getServletInfo( )
 {
  return "HELLO WORLD";
 }
}


Instructions for programming Tips:



Service method is the most important method in class Genericservlet. This method is invoked by the server each time a client makes a request to the server. If programmers want to respond to a customer's request, they must overwrite the method and add their own code in this method to implement the customer's response. The Service has two parameters, ServletRequest and Servletresponse. Where ServletRequest preserves the various attributes that the customer requests to the server.



Object Servletresponse is used to set up how to respond to a customer, it has the following three methods:



* setContentType (String) is used to set the type of response to the customer.



* SETCONTENTLENGTH (int) is used to set the length of the response to the customer.



* Getoutputstream () it returns an output stream of write response data.



In the example, we pass this output to the Servletoutputstream object, which is a subclass of Java.io.outputStream, where we can send the response data to the client's browser.




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.