JavaWeb程序填空题知识点总结
Servletextends HttpServletHttpServletRequest HttpServletResponsethrows ServletException, IOExceptionweb.xml中的配置信息ServletContext获取:getServletContext();保存:context.setAttribute("times", times)ServletConf
·
Servlet
extends HttpServlet
HttpServletRequest
HttpServletResponse
throws ServletException, IOException
- web.xml中的配置信息
ServletContext
- 获取:
getServletContext();
- 保存:
context.setAttribute("times", times)
ServletConfig-获取servlet的配置信息
- 获取:
ServletConfig config = this.getServletConfig()
- 获取参数名所对应的参数值:
String param = config.getInitParameter("参数名")
response
- 设置编码:
resp.setContentType("text/html;charset=GB2312"); resp.setContentType("text/html;charset=utf-8");
- 得到输出流对象:
PrintWriter out = resp.getWriter(); out.println("XXX");
- 禁止浏览器缓存:
resp.setDateHeader("Expires", 0); resp.setHeader("Cache-Control", "no-cache"); resp.setHeader("Pragma", "no-cache");
- 实现重定向:
response.sendRedirect(request.getContextPath() + "/index.jsp");
request
- 实现请求转发(留头不留体)
request.getRequestDispatcher("/TwoServlet").forward(request, response);
- 实现请求包含
request.getRequestDispatcher("/TwoServlet").include(request, response);
Referer
实现防盗链// Referer 说明请求头来源 request.getHeader("Referer");
Cookie
- 获取所有的cookie
Cookie[] cookies = request.getCookies();
- 获取第i个cookie的键和值
cookies[i].getName(); cookies[i].getValue();
- 创建cookie
// 创建cookie Cookie cookie = new Cookie(键, 值); // 设置保存时间 cookie.setMaxAge(60 * 60); // 设置路径:访问chapter05下资源时回送cookie cookie.setPath("/chapter05"); // 发送cookie response.addCookie(cookie);
Session
- 创建或者获得用户的Session对象
HttpSession session = req.getSession();
- 获取属性对应的值
session.getAttribute("cart");
- 设置session
session.setAttribute("cart", cart);
- 创建cookie存放Session的标识号
Cookie cookie = new Cookie("JSESSIONID", session.getId()); cookie.setMaxAge(60 * 30); cookie.setPath("/chapter05"); resp.addCookie(cookie);
Filter
- 编写web.xml文件,设置对MyServlet的拦截
Listener
- 编写一个MyAttributeListener类,该类实现了ServletContextAttributeListener、HttpSessionAttributeListener和ServletRequestAttributeListener接口,并实现该接口中的所有方法
public class MyAttributeListener implements ServletContextAttributeListener, HttpSessionAttributeListener, ServletRequestAttributeListener { public void attributeAdded(ServletContextAttributeEvent sae) { String name = sae.getName(); System.out.println("ServletContext添加属性:" + name + "=" + sae.getServletContext().getAttribute(name)); } public void attributeRemoved(ServletContextAttributeEvent sae) { String name = sae.getName(); System.out.println("ServletContext移除属性: " + name); } public void attributeReplaced(ServletContextAttributeEvent sae) { String name = sae.getName(); System.out.println("ServletContext替换属性:" + name + "=" + sae.getServletContext().getAttribute(name)); } public void attributeAdded(HttpSessionBindingEvent hbe) { String name = hbe.getName(); System.out.println("HttpSession添加属性:" + name + "=" + hbe.getSession().getAttribute(name)); } public void attributeRemoved(HttpSessionBindingEvent hbe) { String name = hbe.getName(); System.out.println("HttpSession移除属性: " + name); } public void attributeReplaced(HttpSessionBindingEvent hbe) { String name = hbe.getName(); System.out.println("HttpSession替换属性:" + name + "=" + hbe.getSession().getAttribute(name)); } public void attributeAdded(ServletRequestAttributeEvent sra) { String name = sra.getName(); System.out.println("ServletRequest添加属性:" + name + "=" + sra.getServletRequest().getAttribute(name)); } public void attributeRemoved(ServletRequestAttributeEvent sra) { String name = sra.getName(); System.out.println("ServletRequest移除属性: " + name); } public void attributeReplaced(ServletRequestAttributeEvent sra) { String name = sra.getName(); System.out.println("ServletRequest替换属性:" + name + "=" + sra.getServletRequest().getAttribute(name)); } }
- 在web.xml文件中,部署MyAttributeListener事件监听器
数据库
- 请编写一个用于读取数据库中users表信息的JDBC程序,要求分别获取字段id、name、password和email字段的值
更多推荐
已为社区贡献2条内容
所有评论(0)