在項目中的web.xml配置servletjava
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0"> <!-- 配置全局參數 那個servlet對象均可以拿,這裏的是全局的參數--> <context-param> <param-name>gloableparam</param-name> <param-value>全局參數</param-value> </context-param> <!-- 告訴servlet這個名字 --> <servlet> <servlet-name>servelt</servlet-name> <servlet-class>com.hei.servelt</servlet-class> </servlet> <!-- 註冊servelt的映射,經過訪問url這個路徑,訪問這個servletd的名字 --> <servlet-mapping> <servlet-name>servelt</servlet-name> <url-pattern>/a</url-pattern> </servlet-mapping> <servlet> <servlet-name>Servlet03</servlet-name> <servlet-class>com.hei.Servlet03</servlet-class> </servlet> <servlet-mapping> <servlet-name>Servlet03</servlet-name> <url-pattern>/b</url-pattern> </servlet-mapping> <servlet> <servlet-name>ServletConfig</servlet-name> <servlet-class>com.hei.ServletConfigDemo</servlet-class> <init-param> <param-name>name</param-name> <param-value>beijing</param-value> </init-param> <init-param> <param-name>age</param-name> <param-value>18</param-value> </init-param> <init-param> <param-name>sex</param-name> <param-value>man</param-value> </init-param> </servlet> <servlet-mapping> // 映射的路由 <servlet-name>ServletConfig</servlet-name> <url-pattern>/ServletConfig</url-pattern> </servlet-mapping> </web-app>
java中的代碼:web
package com.hei; import javax.servlet.ServletConfig; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.sql.ClientInfoStatus; import java.util.Enumeration; public class ServletConfigDemo extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { super.doGet(req, resp); // 生成config方法 ServletConfig config = getServletConfig(); // 獲取web xml下的config配置名字 System.out.println("config的名字:" + config.getServletName()); //config的名字:ServletConfig System.out.println("------------------------------------"); System.out.println(config.getInitParameter("name")); // 根據值獲取單個value // 獲取web xml 下的全部init參數名 Enumeration<String> names = config.getInitParameterNames(); // 遍歷枚舉類型 while (names.hasMoreElements()){ // 獲取鍵 String s = names.nextElement(); System.out.println("配置文件的init參數的鍵: " + s); // 根據鍵獲取值 String value = config.getInitParameter(s); System.out.println("配置文件的init參數的值: " + value); } } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { super.doPost(req, resp); } }
獲取全局參數的方法sql
response.getWriter().append("Served at: ").append(request.getContextPath()); // 指向同一個內存地址,能夠獲取全局配置地址。 ServletContext s = getServletContext(); String globa = s.getInitParameter("gloableparam"); System.out.println("這個params爲:" + globa); //這個params爲:全局參數 }
獲取tomcat的相對路徑
tomcat
文件的下載:app
public class DownLoad extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String filename = req.getParameter("filename"); String realPath = getServletContext().getRealPath("down/" + filename); //先把文件塞進去 FileInputStream fileInputStream = new FileInputStream(realPath); // 構造輸出流對象 ServletOutputStream outputStream = resp.getOutputStream(); int len = 0; byte[] bytes = new byte[1024]; while ((len=fileInputStream.read(bytes)) != -1){ // 將塞進去的文件從新塞進去新的輸出流 outputStream.write(bytes,0,len); } outputStream.close(); fileInputStream.close(); } }