Servlet在web.xml中的配置

來源:互聯網
上載者:User

標籤:servlet   xml   

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: 上午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 ServletException, 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() {    }}

   

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">    <!--用於聲明Servlet-->    <servlet>        <!--指定Servlet的名字,在同一個Web應用程式中,每一個Servlet的名字必須是唯一的,該元素的內容不可為空-->        <servlet-name>HelloWorldServlet</servlet-name>        <!--指定Servlet的完整限定名(即該類所在的路徑)-->        <servlet-class>wbb.servlet.HelloWorld.HelloWorldServlet</servlet-class>        <!--配置初始化參數-->        <init-param>            <param-name>greeting</param-name>            <param-value>Welcome you</param-value>        </init-param>    </servlet>    <!--用於在Servlet跟URL之間定義一個映射-->    <servlet-mapping>        <!--此處的Servlet名字必須跟<servlet>中的子項目<servlet-name>給的名字相同-->        <servlet-name>HelloWorldServlet</servlet-name>        <!--用於指定該Servlet的URL路徑,該路徑是相對Web應用程式上下文根的路徑-->        <url-pattern>/helloworld</url-pattern>    </servlet-mapping>

在瀏覽器中輸入:http://localhost:8080/helloworld即可,此處可以看出helloworld即為web.xml中定義的<url-pattern>



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.