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 ("
}
}