Configuration of the servlet in Web. xml

Source: Internet
Author: User

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>



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.