在项目中的web.xml配置servlet
<?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中的代码:
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);
}
}
获取全局参数的方法
response.getWriter().append("Served at: ").append(request.getContextPath());
// 指向同一个内存地址,可以获取全局配置地址。
ServletContext s = getServletContext();
String globa = s.getInitParameter("gloableparam");
System.out.println("这个params为:" + globa); //这个params为:全局参数
}
获取tomcat的相对路径
文件的下载:
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();
}
}