1.共享数据
servletContext全局共享,ServletContext context = this.getServletContext();
实际开发中基本不用,了解即可
2.读取web.xml中的初始化参数配置等
url
jdbc.mysql://localhost:3306/mybatis
public class ServletDemo01 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 获取web.xml中配置的初始化参数
ServletContext context = this.getServletContext();
String url = context.getInitParameter("url");
System.out.println(url);
}
}
3.请求转发:注意转发不同于response重定向,路径不会发生改变
public class ServletDemo02 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 请求转发
ServletContext context = this.getServletContext();
// 映射中的s5路径转发到/s3路径 转发路劲不变化 不同于重定向
context.getRequestDispatcher("/s3").forward(req,resp);
}
}
4.配合Properties类来获取配置文件信息
要在java或者静态资源resources文件下新疆配置文件
最后运行完会在target文件夹下生成对应的配置文件,我们就是要根据这个相对路径’/WEB-INF/classes/dp.properties‘,来读取信息
public class ServletDemo03 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 使用properties来读取配置文件
InputStream rs = this.getServletContext().getResourceAsStream("/WEB-INF/classes/dp.properties");
Properties prop = new Properties();
prop.load(rs);
String user = prop.getProperty("user");
String pwd = prop.getProperty("pwd");
resp.getWriter().print(user+";"+pwd);
}
}
重定向源码解析只需两句话
public class redirectServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
/*
* 重定向等价代码
* resp.setHeader("location","/s6");
* resp.setStatus(HttpServletResponse.SC_MOVED_TEMPORARILY);
* */
resp.sendRedirect("/s6");
}
public class CookieDemo01 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("utf-8");
resp.setContentType("text/html");
resp.setCharacterEncoding("utf-8");
// 获取cookie对象
Cookie[] cookies = req.getCookies();
PrintWriter out = resp.getWriter();
if(cookies!=null){
out.write("你上一次登录浏览器时间是:");
for (int i = 0; i < cookies.length; i++) {
Cookie cookie = cookies[i];
if(cookie.getName().equals("lastLoginTime")){
Long lastLoginTime = Long.parseLong(cookie.getValue());
Date date = new Date(lastLoginTime);
System.out.println(date.toLocaleString());
}
}
}else {
out.write("这是你第一次登陆浏览器");
}
// 服务端存入一个cookie
Cookie cookie = new Cookie("lastLoginTime",System.currentTimeMillis()+"");
resp.addCookie(cookie);
}
}
创建session对象并存入相关内容
public class sessionDemo01 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 1.解决编码问题
req.setCharacterEncoding("utf-8");
resp.setContentType("text/html");
resp.setCharacterEncoding("utf-8");
// 2.获取session对象
HttpSession session = req.getSession();
session.setAttribute("name","java工程师");
session.setAttribute("student",new Student("黄杰",20));
String id =session.getId();
if(session.isNew()){
resp.getWriter().write("第一次");
}else {
resp.getWriter().write("sessionID是"+id);
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
新建一个类,session全局共享,可以直接通过req.getSession()返回的session对象来获取内容
要注意session.setAttribute(String,Object)的两个参数类型
在调用session.getAttribute(String)时要根据具体的信息来进行相应的转化;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 1.解决编码问题
req.setCharacterEncoding("utf-8");
resp.setContentType("text/html");
resp.setCharacterEncoding("utf-8");
// 2.获取session对象并获取内容
HttpSession session = req.getSession();
String name = (String) session.getAttribute("name");
Student s1 = (Student) session.getAttribute("student");
resp.getWriter().write("姓名是"+name);
System.out.println(s1.toString());
}
注销session
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
HttpSession session = req.getSession();
// 注销session
session.removeAttribute("name"); //常用
// session.invalidate();
}
在porm.xml中配置如下代码块
src/main/resources
**/*.properties
**/*.xml
src/main/java
**/*.properties
**/*.xml
true