基本排序实现 笔记记录
一、ServletConfig
1. ServletConfig配置方式
2. ServletConfig常用方法
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
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.util.Enumeration;
/**
* ServletConfig 演示
*/
public class TestServlet extends HttpServlet {
//1.声明ServletConfig
private ServletConfig config;
//2.通过init方法,对ServletConfig对象赋值
@Override
public void init(ServletConfig config) throws ServletException {
this.config=config;
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//3.演示ServletConfig常用方法
//根究key获取相应的value值
String encodingValue = config.getInitParameter("encoding");
System.out.println("encoding对应的value值:"+encodingValue);
//获取所有的key
Enumeration<String> keys = config.getInitParameterNames();
while (keys.hasMoreElements()){
//获取每一个key
String key = keys.nextElement();
//根究key获取value
String value = config.getInitParameter(key);
System.out.println(key+":"+value);
}
//获取Servlet名称
String servletName = config.getServletName();
System.out.println("servletName:" + servletName);
//获取ServletContext对象
ServletContext servletContext = config.getServletContext();
System.out.println("servletContext:"+servletContext);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req,resp);
}
}
二、ServletContext
什么是域对象:
1. ServletContext的配置方式
2. ServletContext常用方法
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* ServletContext 演示
*/
public class ServletContextDemo extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//1. 获取ServletContext对象
ServletContext context = getServletContext();
//2.常用方法演示
//获取全局配置参数 getInitParameter(String key) 根据key获取value
String value = context.getInitParameter("globalDesc");
System.out.println("globalDesc:"+value);
//获取应用虚拟目录 context.getContextPath()
String contextPath = context.getContextPath();
System.out.println("contextPath:"+contextPath);
//根据虚拟目录获取绝对路径
String realPath = context.getRealPath("/");
System.out.println("realPath:"+realPath);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req,resp);
}
}
这里的/demo来源于项目部署配置的地方:
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* ServletContext 演示
*/
public class ServletContextDemo extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//1. 获取ServletContext对象
ServletContext context = getServletContext();
//2.常用方法演示
//获取全局配置参数 getInitParameter(String key) 根据key获取value
String value = context.getInitParameter("globalDesc");
System.out.println("globalDesc:"+value);
//获取应用虚拟目录 context.getContextPath()
String contextPath = context.getContextPath();
System.out.println("contextPath:"+contextPath);
//根据虚拟目录获取绝对路径
String realPath = context.getRealPath("/");
System.out.println("realPath:"+realPath);
String a = context.getRealPath("/WEB-INF/classes/a.txt");
System.out.println("a.txt:"+a);
String b = context.getRealPath("/b.txt");
System.out.println("b.txt:"+b);
String c = context.getRealPath("/WEB-INF/c.txt");
System.out.println("c.txt:"+c);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req,resp);
}
}
2. ServletContext常用方法(数据共享)
在不同的Servlet中获取信息
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* ServletContext 演示
*/
public class ServletContextDemo extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//1. 获取ServletContext对象
ServletContext context = getServletContext();
//2.常用方法演示
//获取全局配置参数 getInitParameter(String key) 根据key获取value
String value = context.getInitParameter("globalDesc");
System.out.println("globalDesc:"+value);
//获取应用虚拟目录 context.getContextPath()
String contextPath = context.getContextPath();
System.out.println("contextPath:"+contextPath);
//根据虚拟目录获取绝对路径
String realPath = context.getRealPath("/");
System.out.println("realPath:"+realPath);
//设置共享数据
context.setAttribute("username","zhangsan");
//删除共享数据
context.removeAttribute("username");
//删除后再另一个Servlet中就获取不到了
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req,resp);
}
}