初学servlet之使用web.xml配置

时间:2022-10-27 13:23:31

先写两个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>