Import Javax.servlet.*;import java.io.ioexception;import java.io.pipedwriter;import java.io.printwriter;/** * Created with IntelliJ idea. * USER:WBB * DATE:14-6-17 * Time: Morning 11:56 * To change this template use File | Settings | File Templates. */public class Helloworldservlet implements Servlet {private ServletConfig config; @Override public void init (ServletConfig config) throws servletexception {this.config = config; } @Override Public ServletConfig getservletconfig () {return config; } @Override public void service (ServletRequest servletrequest, Servletresponse servletresponse) throws Servletexcept Ion, IOException {printwriter out = Servletresponse.getwriter (); Out.println ("Hello World"); Out.close (); } @Override Public String Getservletinfo () {return null; To change body of implemented methods use File | Settings | File Templates. } @Override public void Destroy () {}}
Configuration of Web. xml
<?xml version= "1.0" encoding= "UTF-8"? ><web-app 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<span style=" White-space:pre "></span> http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd " version=" 2.5 "> <!--for declaring servlet--> <servlet> <!--specifying the name of the servlet, In the same Web application, the name of each servlet must be unique, and the content of the element cannot be empty--> <servlet-name>helloworldservlet </servlet-name> <!--Specify the full qualified name of the servlet (that is, the path to which the class is located)--> <servlet-class>wbb.servlet.HelloWorld.HelloWorldServlet</servlet-class> <!--configuration initialization parameters--> <init-param> &NBsp <param-name>greeting</param-name> <param-value> Welcome you</param-value> </init-param> </servlet> <!--used to define a mapping between a servlet and a URL--> <servlet-mapping> <!-- The servlet name here must be the same as the child element <servlet-name> given name in <servlet>--> <servlet-name >HelloWorldServlet</servlet-name> <!--to specify the URL path for the servlet, The path is relative to the Web application up and down root--> <url-pattern>/helloworld</url-pattern> </servlet-mapping>
Enter in the browser: Http://localhost:8080/helloworld, you can see that HelloWorld is defined in the Web. XML <url-pattern>