在pom.xml中配置
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>
在web.xml中配置
<!-- 启动我们自己的listener -->
<listener>
<listener-class>com.atguigu.scw.manager.listener.MyAppListener</listener-class>
</listener>
<servlet>
<servlet-name>springDispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>springDispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
项目启动的时候,将一些常用数据放在application域中,都能用
public class MyListener implements ServletContextListener {
public void contextInitialized(ServletContextEvent sce) {
ServletContext servletContext = sce.getServletContext();
//将项目路径放在application域中
servletContext.setAttribute("path", servletContext.getContextPath());
}
public void contextDestroyed(ServletContextEvent sce) {
//项目关闭,销毁application域中所有数据
}
}
在jsp页面获取 ${path }
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
${path }
</body>
</html>