Servlet interfaces for beginners and Servlet interfaces for beginners

Source: Internet
Author: User

Servlet interfaces for beginners and Servlet interfaces for beginners

Package app01a;

Import java. io. IOException;
Import java. io. PrintWriter;

Import javax. servlet. Servlet;
Import javax. servlet. ServletConfig;
Import javax. servlet. ServletException;
Import javax. servlet. ServletRequest;
Import javax. servlet. ServletResponse;
Import javax. servlet. annotation. WebServlet;

/**
* How to access:
* Http: // localhost: Port Number (8080 is not modified using tomcat)/project name/urlPatterns in the following WebServlet
* If the web. xml configuration file is used, the access path is changed:
* Http: // localhost: Port Number (8080 is not modified using tomcat)/project name/url-pattern corresponding to the servlet in web. xml
* @ Author Administrator
*
*/
// For the moment, the content of servletConfig is the content in WebServlet.
@ WebServlet (name = "MyServlet", urlPatterns = {"/my "})
Public class MyServlet implements Servlet {

Private transient ServletConfig servletConfig;

/**
* When the servlet is destroyed, the servlet container will call this method.
*/
@ Override
Public void destroy (){
// TODO Auto-generated method stub

}

/**
* This method will return ServletConfig with servlet passed to the init method.
*/
@ Override
Public ServletConfig getServletConfig (){
Return servletConfig;
}

/**
* This method will return the Servlet description.
*/
@ Override
Public String getServletInfo (){
Return "My Servlet ";
}

/**
* Called upon the first request and will not be called in subsequent requests
*/
@ Override
Public void init (ServletConfig servletConfig) throws ServletException {
This. servletConfig = servletConfig;

}

/**
* This method is called every time a servlet is requested. The init method and service method are called in the first request, and only the service method is called in subsequent requests.
*/
@ Override
Public void service (ServletRequest request, ServletResponse response) throws ServletException, IOException {
String servletName = servletConfig. getServletName ();
// Set the response content type
Response. setContentType ("text/html ");
PrintWriter writer = response. getWriter ();
Writer. print ("
}

}

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.