自学Servlet_2_servletContext
1.两种获取servletContext对象的方式
ServletContext context = this.getServletConfig().getServletContext();ServletContext context1 = this.getServletContext();
context.setAttribute("data", "aaaaaaaaaa");
context.getAttribute("data")
String url = this.getServletContext().getInitParameter("url");String username = this.getServletContext().getInitParameter("username");String password = this.getServletContext().getInitParameter("password");
<context-param> <param-name>url</param-name> <param-value>jdbc:mysql://localhost:3306/test</param-value> </context-param> <context-param> <param-name>username</param-name> <param-value>root</param-value> </context-param> <context-param> <param-name>password</param-name> <param-value>root</param-value> </context-param>
String filename = "1.jpg";ServletContext context = this.getServletContext();System.out.println(context.getMimeType(filename));
//servlet收到请求产生数据,然后转交给jsp显示String data = "aaaaaa";this.getServletContext().setAttribute("data", data);RequestDispatcher rd = this.getServletContext().getRequestDispatcher("/view.jsp");rd.forward(request, response);