1 <?XML version= "1.0" encoding= "UTF-8"?>2 <Web-appversion= "2.5"xmlns= "Http://java.sun.com/xml/ns/javaee"3 Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"4 xsi:schemalocation= "Http://java.sun.com/xml/ns/javaee5 http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd ">6 <Display-name></Display-name>7 <welcome-file-list>8 <Welcome-file>index.jsp</Welcome-file>9 </welcome-file-list>Ten One <servlet> A <Servlet-name>Date</Servlet-name> - <Servlet-class>Date. Datedemo</Servlet-class> - </servlet> the <servlet-mapping> - <Servlet-name>Date</Servlet-name> - <Url-pattern>*.do</Url-pattern> - </servlet-mapping> + </Web-app>
1 Packagedate;2 3 Importjava.io.IOException;4 ImportJava.io.PrintWriter;5 ImportJava.text.SimpleDateFormat;6 Importjava.util.Date;7 8 Importjavax.servlet.ServletException;9 ImportJavax.servlet.http.HttpServlet;Ten Importjavax.servlet.http.HttpServletRequest; One ImportJavax.servlet.http.HttpServletResponse; A -@SuppressWarnings ("Serial") - Public classDatedemoextendsHttpServlet { the - - @Override - protected voidService (HttpServletRequest request, httpservletresponse response) + throwsservletexception, IOException { -Response.setcontenttype ("text/html"); +PrintWriter p=Response.getwriter (); ADate now=NewDate (); atSimpleDateFormat sdf=NewSimpleDateFormat ("Yyyy-mm-dd"); -String time=Sdf.format (now); - P.write (time); - p.close (); - } - in}
1) A servlet is actually a container, and Tomcat is a servlet that provides the environment in which the servlet runs
2) Understanding of the servlet path---how the servlet works
2.1 For example, enter in the browser: Http://localhost:8080/Servlet2/hi
1, the browser establishes a connection with the servlet via Ip,port
2, the browser sends the request parameter package to the Servlet container.
3,servlet parses the request data and places it on the request object while creating the response object
4,servlet will locate the configuration file through configuration (Servlet2/hi) and then create the corresponding Servlet object.
The 5,servlet container automatically calls the service method, processes the request object, and returns to the Response object
The 6,servlet container packs resopnse data back to the browser.
7, the browser to the sentence data display to the user.
Java code:
Package date;
Import java.io.IOException;
Import Java.io.PrintWriter;
Import Java.text.SimpleDateFormat;
Import Java.util.Date;
Import javax.servlet.ServletException;
Import Javax.servlet.http.HttpServlet;
Import Javax.servlet.http.HttpServletRequest;
Import Javax.servlet.http.HttpServletResponse;
@SuppressWarnings ("Serial")
public class Datedemo extends HttpServlet {
@Override
protected void Service (HttpServletRequest request, httpservletresponse response)
Throws Servletexception, IOException {
Response.setcontenttype ("text/html");
PrintWriter P=response.getwriter ();
Date Now=new date ();
SimpleDateFormat sdf=new SimpleDateFormat ("Yyyy-mm-dd");
String Time=sdf.format (now);
P.write (time);
P.close ();
}
}
Web. XML configuration
<?xml version= "1.0" encoding= "UTF-8"?>
<web-app version= "2.5" xmlns= "Http://java.sun.com/xml/ns/javaee"
Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"
Xsi:schemalocation= "Http://java.sun.com/xml/ns/javaee
Http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd ">
<display-name></display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>date</servlet-name>
<servlet-class>date. Datedemo</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>date</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
Servlet Detailed description