Servlet interface application (three methods for servlet development) and three methods for servlet
First: implement the Servlet Interface
Public class ServletDemo1 implements Servlet {
@ Override
Public void init (ServletConfig config) throws ServletException {
// TODO Auto-generated method stub
}
@ Override
Public ServletConfig getServletConfig (){
// TODO Auto-generated method stub
Return null;
}
@ Override
Public void service (ServletRequest req, ServletResponse res)
Throws ServletException, IOException {
HttpServletRequest request = (HttpServletRequest) req; // type conversion
HttpServletResponse response = (HttpServletResponse) res;
// Obtain the request type
String type = request. getMethod ();
// Determine the type of GET/POST
If ("GET". equals (type )){
This. doGet (request, response );
} Else {
This. doPost (request, response );
}
}
Private void doPost (HttpServletRequest request, HttpServletResponse response) throws IOException {
// TODO Auto-generated method stub
Response. setContentType ("text/html; charset = UTF-8"); // sets the response character encoding.
PrintWriter writer = response. getWriter (); // gets the print Stream object.
Writer. print ("method 1 for Servlet class implementation, Servlet interface Implementation"); // print in the browser
}
Private void doGet (HttpServletRequest request, HttpServletResponse response) throws IOException {
// TODO Auto-generated method stub
DoPost (request, response); // execute the call to doPost, because the default value is doGet.
}
@ Override
Public String getServletInfo (){
// TODO Auto-generated method stub
Return null;
}
@ Override
Public void destroy (){
// TODO Auto-generated method stub
}
}
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>
<Servlet>
<Servlet-name> ServletDemos </servlet-name>
<Servlet-class> cn. itcast. servlet. demo. ServletDemo1 </servlet-class>
</Servlet>
<Servlet-mapping>
<Servlet-name> ServletDemos </servlet-name>
<Url-pattern>/servletdemo1 </url-pattern>
</Servlet-mapping>
</Web-app>
Type 2: Inherit the abstract class form GenericServlet
// Implement Servlet development by inheriting the GenericServlet class
Public class ServletDemo2 extends GenericServlet {
// The GenericServlet method must be implemented
@ Override
Public void service (ServletRequest request, ServletResponse response)
Throws ServletException, IOException {
Response. getWriter (). write ("hello GenericServlet ");
// Output directly to the browser
}
}
XML configuration
<Servlet>
<Servlet-name> ServletDemo2 </servlet-name>
<Servlet-class> cn. itcast. servlet. demo. ServletDemo2 </servlet-class>
</Servlet>
<Servlet-mapping>
<Servlet-name> ServletDemo2 </servlet-name>
<Url-pattern>/servletdemo2 </url-pattern>
</Servlet-mapping>
Third: Inherit the abstract class HttpServlet (recommended)
// Develop Servlet by inheriting HttpServlet
// The most common
Public class ServletDemo3 extends HttpServlet {
// Rewrite Method
@ Override
Protected void doGet (HttpServletRequest request, HttpServletResponse response)
Throws ServletException, IOException {
DoPost (request, response );
}
// Rewrite Method
@ Override
Protected void doPost (HttpServletRequest request, HttpServletResponse response)
Throws ServletException, IOException {
Response. setContentType ("text/html; charset = UTF-8"); // sets the Encoding
PrintWriter pw = response. getWriter ();
Pw. write ("the third method inherits HttpServlet (recommended)"); // output
}
}
XML configuration
<Servlet>
<Servlet-name> ServletDemo3 </servlet-name>
<Servlet-class> cn. itcast. servlet. demo. ServletDemo3 </servlet-class>
</Servlet>
<Servlet-mapping>
<Servlet-name> ServletDemo3 </servlet-name>
<Url-pattern>/servletdemo3 </url-pattern>
</Servlet-mapping>