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;
public class LocalServlet extends HttpServlet {
private static int calltime=0;
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("访问LocalServlet次数:"+(calltime++));
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response);
}
}
Tomcat启动时,自动访问本地servlet



在Java EE(Java Platform, Enterprise Edition)开发中,Tomcat是一个广泛应用的开源Servlet容器,它遵循Java Servlet和JavaServer Pages(JSP)规范。当我们希望在Tomcat启动时自动访问本地的Servlet或JSP,可以通过配置监听器(Listeners)来实现这一需求。监听器是Servlet容器中的一个组件,它可以监听特定的事件,比如Web应用的初始化、销毁等,并在这些事件发生时执行相应的代码。 我们需要创建一个Servlet,这个Servlet将会在启动时被调用。下面是一个简单的Servlet示例: ```java import javax.servlet.*; import java.io.IOException; public class CallLocationServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println("CallLocationServlet 被自动调用了!"); // 在这里可以执行你需要在启动时运行的逻辑 } } ``` 接下来,我们需要在`web.xml`配置文件中添加一个监听器,这个监听器会在Tomcat启动时调用我们的Servlet。打开`WEB-INF/web.xml`,添加以下内容: ```xml <web-app> ... <listener> <listener-class>org.apache.catalina.startup.ContextConfig</listener-class> </listener> <!-- 自定义的启动时调用的监听器 --> <listener> <listener-class>com.example.CallLocationListener</listener-class> </listener> <!-- 配置Servlet --> <servlet> <servlet-name>CallLocationServlet</servlet-name> <servlet-class>com.example.CallLocationServlet</servlet-class> </servlet> <!-- 配置Servlet的映射 --> <servlet-mapping> <servlet-name>CallLocationServlet</servlet-name> <url-pattern>/callLocation</url-pattern> </servlet-mapping> ... </web-app> ``` 在这个配置中,我们创建了一个名为`CallLocationListener`的自定义监听器。这个监听器需要继承`javax.servlet.ServletContextListener`接口并实现其`contextInitialized`方法。在这个方法中,我们可以调用Servlet的URL,使其在Tomcat启动时被访问: ```java import javax.servlet.*; import javax.servlet.annotation.WebListener; import java.io.IOException; @WebListener public class CallLocationListener implements ServletContextListener { @Override public void contextInitialized(ServletContextEvent sce) { sce.getServletContext().getContextPath(); // 获取上下文路径 try { sce.getServletContext().getRequestDispatcher("/callLocation").forward(sce.getRequest(), sce.getResponse()); } catch (ServletException | IOException e) { e.printStackTrace(); } } // ... 其他方法(contextDestroyed) } ``` 这样,每次Tomcat启动时,`CallLocationServlet`就会被自动调用,打印出"CallLocationServlet 被自动调用了!",并且你可以在此基础上添加任何需要在启动时执行的逻辑。 如果你还需要访问本地的JSP,只需将Servlet的映射URL改为JSP文件的路径即可。例如,如果你有一个名为`callLocation.jsp`的JSP文件,那么在`web.xml`中将Servlet的映射更改为: ```xml <servlet-mapping> <servlet-name>CallLocationServlet</servlet-name> <url-pattern>/callLocation.jsp</url-pattern> </servlet-mapping> ``` 然后,在`CallLocationListener`的`contextInitialized`方法中,调用JSP的URL: ```java sce.getServletContext().getRequestDispatcher("/callLocation.jsp").forward(sce.getRequest(), sce.getResponse()); ``` 这样一来,Tomcat启动时会自动访问本地的JSP页面。 以上就是如何在Tomcat启动时通过监听器自动访问本地Servlet或JSP的方法。这个机制在一些需要在服务器启动时进行预处理或者初始化任务的场景中非常有用。
















































- 1

- #完美解决问题
- #运行顺畅
- #内容详尽
- #全网独家
- #注释完整
- 一代禽兽2016-03-01学习了,谢谢分享
- hlby32015-10-17谢谢分享,终于成功了!

- 粉丝: 102
- 资源: 50
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助


最新资源
- 西门子S7-200 Smart与东元TECO N310变频器基于Modbus RTU通讯的实战程序详解
- (源码)基于DS游戏机的宝可梦白版2能力扩展项目.zip
- Javascript-JavaScript资源
- 毕业设计前端-毕业设计资源
- TI_MSP0G3507-电赛资源
- (源码)基于Python和C++的AVR微控制器模拟器.zip
- 安卓手机云控系统框架源码:PHP+Autojs的双协议架构及其应用场景
- (源码)基于Go语言的文件存储系统.zip
- vehicle-智能车资源
- (源码)基于C语言和ARM汇编的MasterMind游戏系统.zip
- 能源领域:基于MATLAB的阶梯式碳交易与供需灵活双响应综合能源系统优化调度
- (源码)基于ESP32和MQTT的智能通风控制系统.zip
- 蓝桥杯单片机真题代码-蓝桥杯资源
- JavaGuide-Interview-计算机求职面经资源
- awesome-java-books-计算机求职笔试资源
- 西门子SMART PLC与东元变频器基于Modbus RTU协议的通讯实现及应用


