zoukankan      html  css  js  c++  java
  • Servlet 配置文件web.xml实验

    我们的servlet为

    import java.io.IOException;
    import java.io.PrintWriter;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    public class ServletDemo1 extends HttpServlet{
        
        public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws IOException{
            PrintWriter out = response.getWriter();
            out.println("<html>");
            out.println("<body>");
            out.println("<h1>Hello Servlet Get</h1>");
            out.println("</body>");
            out.println("</html>");    
        }
    }

    我们的web.xml配置文件为

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
        
        <servlet>
            <servlet-name>Servlet Name For Demo1</servlet-name>
            <servlet-class>com.mkyong.ServletDemo</servlet-class>
        </servlet>
        
        <servlet-mapping>
            <servlet-name>Servlet Name For Demo</servlet-name>
            <url-pattern>/Demo</url-pattern>
        </servlet-mapping>
    </web-app>

    现在我们的实验分为三步

    1 测试<servlet-mapping>中的<url-pattern>,当我们在浏览器的请求不写成Demo而写成其他任意值得时候,浏览器出现404错误 resource not found。这个显而易见啦

    2 测试假如<servlet-mapping><servlet>的这两个标签的<servlet-name>不一致的时候出现的现象

    在我们启动tomcat的过程中,此时我们还没有从浏览器发出URL请求。控制台记录中抛出如下异常

    Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/Servlet]]
    Caused by: java.lang.IllegalArgumentException: Servlet mapping specifies an unknown servlet name

    3 测试假如

    当我们的<servlet>标签的<servlet-class>写的同web-inf文件夹中的servlet类不一致的时候 当我们的客户端发送URL请求时才会抛出以下错误
    我们的客户端出现为

    HTTP Status 500 - Error instantiating servlet class

    javax.servlet.ServletException: Error instantiating servlet class 

    root cause

    java.lang.ClassNotFoundException: com.mkyong.ServletDemo
    
    控制台显示为
    java.lang.ClassNotFoundException

    从上面可以看出在启动tomcat的过程中并不会对我们的servlet具体的实例化,而只有在服务器收到客户端浏览器正确的URL请求时才会找相关的类并且实例化。

  • 相关阅读:
    oracle ash性能报告的使用方法
    Top 5 Timed Events[转]
    通过top 5等待事件查看sql语句
    oracle中关于删除表purge语句和闪回语句的基本使用
    oracle表类似:BIN$dJ5h8mA4Lr/gQAB/AQB0oA==$0 TABLE
    Oracle 10G 中的"回收站"
    oracle ash性能报告的使用方法
    分析AWR报告
    global cache cr request
    ORACLE创建OEM是老爱报的错误【weber出品】
  • 原文地址:https://www.cnblogs.com/winAlaugh/p/5456628.html
Copyright © 2011-2022 走看看