JavaWeb基础 通过配置web.xml 让servlet类和服务器共同启动
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JavaWeb基础 通过配置web.xml 让servlet类和服务器共同启动相关的知识,希望对你有一定的参考价值。
礼悟:
好好学习多思考,尊师重道存感恩。叶见寻根三二一,江河湖海同一体。
虚怀若谷良心主,愿行无悔给最苦。读书锻炼强身心,诚劝且行且珍惜。
javaEE:7
javaSE:1.8
JSTL:1.2.2
server:tomcat 8.5
explorer:Firefox
os:windows7 x64
ide:MyEclipse 2017
工程目录结构
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> <servlet> <servlet-name>SpecialServletOne</servlet-name> <servlet-class>jizuiku.web.servlet.SpecialServletOne</servlet-class> <load-on-startup>0</load-on-startup><!-- 按照非负数字,从大到小 一次启动相应服务 --> </servlet> <servlet-mapping> <servlet-name>SpecialServletOne</servlet-name> <url-pattern>/SpecialServletOne</url-pattern> </servlet-mapping> <servlet> <servlet-name>SpecialServletTwo</servlet-name> <servlet-class>jizuiku.web.servlet.SpecialServletTwo</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>SpecialServletTwo</servlet-name> <url-pattern>/SpecialServletTwo</url-pattern> </servlet-mapping> <servlet> <servlet-name>CommonServlet</servlet-name> <servlet-class>jizuiku.web.servlet.CommonServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>CommonServlet</servlet-name> <url-pattern>/CommonServlet</url-pattern> </servlet-mapping> </web-app>
分析xml文件,可知有三个servlet类。其中SpecialServletTwo、SpecialServletOne随着服务器启动而启动,而CommonServlet是普通的,有访问时才会启动。
CommonServlet
package jizuiku.web.servlet; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * * * @author 给最苦 * @version V17.10.20 */ public class CommonServlet extends HttpServlet { /** * */ private static final long serialVersionUID = 1L; @Override public void init() throws ServletException { // TODO Auto-generated method stub System.out.println("CommonServlet已启动"); } @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // TODO Auto-generated method stub } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // TODO Auto-generated method stub } }
SpecialServletOne
package jizuiku.web.servlet; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * * * @author 给最苦 * @version V17.10.20 */ public class SpecialServletOne extends HttpServlet { /** * */ private static final long serialVersionUID = 1L; @Override public void init() throws ServletException { // TODO Auto-generated method stub System.out.println("SpecialServletOne已启动"); } @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // TODO Auto-generated method stub } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // TODO Auto-generated method stub } }
SpecialServletTwo
package jizuiku.web.servlet; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * * * @author 给最苦 * @version V17.10.20 */ public class SpecialServletTwo extends HttpServlet { /** * */ private static final long serialVersionUID = 1L; @Override public void init() throws ServletException { // TODO Auto-generated method stub System.out.println("SpecialServletTwo已启动"); } @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // TODO Auto-generated method stub } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // TODO Auto-generated method stub } }
服务器启动时,控制台输出的部分内容
信息: Starting Servlet Engine: Apache Tomcat/8.5.23 十月 20, 2017 9:10:41 下午 org.apache.catalina.startup.HostConfig deployDirectory 信息: Deploying web application directory [E:\\develop\\apache-tomcat-8.5.23\\webapps\\Day09] 十月 20, 2017 9:10:41 下午 org.apache.jasper.servlet.TldScanner scanJars 信息: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time. SpecialServletOne已启动 SpecialServletTwo已启动 十月 20, 2017 9:10:41 下午 org.apache.catalina.startup.HostConfig deployDirectory 信息: Deployment of web application directory [E:\\develop\\apache-tomcat-8.5.23\\webapps\\Day09] has finished in [729] ms 十月 20, 2017 9:10:41 下午 org.apache.catalina.startup.HostConfig deployDirectory 信息: Deploying web application directory [E:\\develop\\apache-tomcat-8.5.23\\webapps\\docs] 十月 20, 2017 9:10:41 下午 org.apache.catalina.startup.HostConfig deployDirectory 信息: Deployment of web application directory [E:\\develop\\apache-tomcat-8.5.23\\webapps\\docs] has finished in [26] ms 十月 20, 2017 9:10:41 下午 org.apache.catalina.startup.HostConfig deployDirectory 信息: Deploying web application directory [E:\\develop\\apache-tomcat-8.5.23\\webapps\\examples] 十月 20, 2017 9:10:42 下午 org.apache.catalina.core.ApplicationContext log 信息: ContextListener: contextInitialized() 十月 20, 2017 9:10:42 下午 org.apache.catalina.core.ApplicationContext log 信息: SessionListener: contextInitialized() 十月 20, 2017 9:10:42 下午 org.apache.catalina.startup.HostConfig deployDirectory 信息: Deployment of web application directory [E:\\develop\\apache-tomcat-8.5.23\\webapps\\examples] has finished in [429] ms 十月 20, 2017 9:10:42 下午 org.apache.catalina.startup.HostConfig deployDirectory 信息: Deploying web application directory [E:\\develop\\apache-tomcat-8.5.23\\webapps\\host-manager] 十月 20, 2017 9:10:42 下午 org.apache.catalina.startup.HostConfig deployDirectory 信息: Deployment of web application directory [E:\\develop\\apache-tomcat-8.5.23\\webapps\\host-manager] has finished in [41] ms 十月 20, 2017 9:10:42 下午 org.apache.catalina.startup.HostConfig deployDirectory 信息: Deploying web application directory [E:\\develop\\apache-tomcat-8.5.23\\webapps\\manager] 十月 20, 2017 9:10:42 下午 org.apache.catalina.startup.HostConfig deployDirectory 信息: Deployment of web application directory [E:\\develop\\apache-tomcat-8.5.23\\webapps\\manager] has finished in [39] ms 十月 20, 2017 9:10:42 下午 org.apache.catalina.startup.HostConfig deployDirectory 信息: Deploying web application directory [E:\\develop\\apache-tomcat-8.5.23\\webapps\\ROOT] 十月 20, 2017 9:10:42 下午 org.apache.catalina.startup.HostConfig deployDirectory 信息: Deployment of web application directory [E:\\develop\\apache-tomcat-8.5.23\\webapps\\ROOT] has finished in [33] ms 十月 20, 2017 9:10:42 下午 org.apache.coyote.AbstractProtocol start 信息: Starting ProtocolHandler ["http-nio-8081"] 十月 20, 2017 9:10:42 下午 org.apache.coyote.AbstractProtocol start 信息: Starting ProtocolHandler ["ajp-nio-8009"] 十月 20, 2017 9:10:42 下午 org.apache.catalina.startup.Catalina start 信息: Server startup in 1383 ms
浏览器访问 CommonServlet 所对应的网页时
学习资源:itcast和itheima视频库。如果您有公开的资源,可以分享给我的话,用您的资源学习也可以。
博文是观看视频后,融入思考写成的。博文好,是老师讲得好。博文坏,是 给最苦 没认真。
以上是关于JavaWeb基础 通过配置web.xml 让servlet类和服务器共同启动的主要内容,如果未能解决你的问题,请参考以下文章
JavaWeb基础 通过设置web.xml 实现一个servlet拥有两个浏览路径