zoukankan      html  css  js  c++  java
  • 初学servlet之使用web.xml配置

    先写两个servlet,之后展示web.xml配置

    package app01c;

    import java.io.IOException;
    import java.io.PrintWriter;

    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;

    /**
     * 如何访问:
     *     http://localhost:端口号(使用tomcat没有修改过的是8080)/项目名/下面WebServlet中的urlPatterns
     *  如果使用的是web.xml配置文件,那么访问路径就变为:
     *  http://localhost:端口号(使用tomcat没有修改过的是8080)/项目名/web.xml中该servlet对应的url-pattern
     * @author Administrator
     *
     */
    public class SimpleServlet extends HttpServlet {
    private static final long serialVersionUID = 8946L;
        
        @Override
        public void doGet(HttpServletRequest request,
                HttpServletResponse response)
                throws ServletException, IOException {
            response.setContentType("text/html");
            PrintWriter writer = response.getWriter();
            writer.print("<html><head></head>" +
                    "<body>Simple Servlet</body></html");
        }
    }

    package app01c;

    import java.io.IOException;
    import java.io.PrintWriter;

    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;

    /**
     * 如何访问:
     *     http://localhost:端口号(使用tomcat没有修改过的是8080)/项目名/下面WebServlet中的urlPatterns
     *  如果使用的是web.xml配置文件,那么访问路径就变为:
     *  http://localhost:端口号(使用tomcat没有修改过的是8080)/项目名/web.xml中该servlet对应的url-pattern
     * @author Administrator
     *
     */
    public class WelcomeServlet extends HttpServlet {
        private static final long serialVersionUID = 27126L;

        @Override
        public void doGet(HttpServletRequest request,
                HttpServletResponse response)
                throws ServletException, IOException {
            response.setContentType("text/html");
            PrintWriter writer = response.getWriter();
            writer.print("<html><head></head>"
                    + "<body>Welcome</body></html>");
        }
    }

    <?xml version="1.0" encoding="ISO-8859-1"?>
    <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 http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
        version="3.0">

        <servlet>
            <!-- name表示对应servlet的类名 -->
            <servlet-name>SimpleServlet</servlet-name>
            <!-- class表示完整的路径 -->
            <servlet-class>app01c.SimpleServlet</servlet-class>
            <!-- load-on-startup这个元素使得Servlet在启动时加载,而不是在第一次调用时加载 -->
            <load-on-startup>10</load-on-startup>
        </servlet>

        <servlet-mapping>
            <!-- 与上面的name必须相同 -->
            <servlet-name>SimpleServlet</servlet-name>
            <!-- 与WebServlet的urlPatterns相同,利用url访问的时候有用,还有就是form表单中的action的内容 -->
            <url-pattern>/simple</url-pattern>
        </servlet-mapping>  


        <servlet>
            <servlet-name>WelcomeServlet</servlet-name>
            <servlet-class>app01c.WelcomeServlet</servlet-class>
            <load-on-startup>20</load-on-startup>
        </servlet>

        <servlet-mapping>
            <servlet-name>WelcomeServlet</servlet-name>
            <url-pattern>/welcome</url-pattern>
        </servlet-mapping>
    </web-app>

  • 相关阅读:
    Web API 强势入门指南
    毫秒必争,前端网页性能最佳实践
    Windbg Extension NetExt 使用指南 【3】 ---- 挖掘你想要的数据 Managed Heap
    Windbg Extension NetExt 使用指南 【2】 ---- NetExt 的基本命令介绍
    Windbg Extension NetExt 使用指南 【1】 ---- NetExt 介绍
    WCF : 修复 Security settings for this service require Windows Authentication but it is not enabled for the IIS application that hosts this service 问题
    透过WinDBG的视角看String
    Microsoft Azure Web Sites应用与实践【4】—— Microsoft Azure网站的“后门”
    企业IT管理员IE11升级指南【17】—— F12 开发者工具
    WCF : 如何将NetTcpBinding寄宿在IIS7上
  • 原文地址:https://www.cnblogs.com/wadmwz/p/7521523.html
Copyright © 2011-2022 走看看