一、ServletConfig:javax.servlet.ServletContext包;
// in the corresponding application jsp
// which defines a number of methods that can exchange data and Servlet exchange Rocchi and Tomcat;
// ServletContext jvm current web application and only allows sharing of data among a plurality of the Servlet;
Second, get ServletContext objects:
(1 ) by GenericServlet provided GetServletContext ();
//this.getServletContext();
(2 ) by ServletConfig provided GetServletContext ();
//this.getServletConfig().getServletContext();
(3 ) by obtaining the HttpServletRequest;
//req.getServletContext();
(4 ) by acquiring the HttpSession;
//req.getSession().getServletContext();
Fourth, the method of operation of the data: // domain method includes the property method;
1. Set the properties: void setAttribute (String name, Object Object); // call with a name attribute more than once will be covered;
2. acquiring property: Object getAttribute (String name); // Gets the property name had not set return null;
3. Delete Attribute: void removeAttribute (String name);
4. Get all the domain of attribute names: the Enumeration getAttributeNames ();
@WebServlet("/test") public class Test extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //获取ServletContext对象 ServletContext servletContext = getServletContext(); //设置属性 servletContext.setAttribute("name", "zs"); servletContext.setAttribute("name", "ww"); servletContext.setAttribute("age", 14); //获取指定属性 System.out.println(servletContext.getAttribute("name")); //删除属性 servletContext.removeAttribute("name"); //获取所有属性 Enumeration<String> attributeNames = servletContext.getAttributeNames(); while (attributeNames.hasMoreElements()) { String name = attributeNames.nextElement(); Object value = servletContext.getAttribute(name); System.out.println(name+":"+value); } } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { this.doGet(req, resp); } }
Fifth, access to resources Method :
( 1 ) get the project name: String getContextPath ();
// Returns the name of the beginning of the project include / slash;
( 2 ) obtain the real path: String getRealPath (String path);
// incoming path before must contain slashes; "/a.txt"
( 3 all resources path) Gets the path: the Set getResourcePaths ( "/ the WEB-INF");
( 4 ) access to resources stream: the InputStream the getResourceAsStream ( "/ the WEB-INF / b.txt");
(5 ) acquires a parameter value: String the getInitParameter (String name);
// returns the current value of the initialization parameter in accordance with a program name included; <context-param>
(6 ) obtain the parameter values: the Enumeration <String> the getInitParameterNames ()
// returns a reference to the current enumeration of all the names in the program initialization parameters (collection);
(7 ) obtains the encoded: String getRequestCharacterEncoding ();
(8 ) set the encoding: void setRequestCharacterEncoding (String encoding);
@WebServlet ( "Test" ) public class the Test the extends the HttpServlet { @Override protected void the doGet (the HttpServletRequest REQ, the HttpServletResponse RESP) throws ServletException, IOException { // Get ServletContext object; ServletContext where servletContext = GetServletContext (); // returns the name of the project, with / slash; / Review String contextPath = servletContext.getContextPath (); System.out.println (contextPath); // returns true path of the file; E: \ JavaProject \ review2 \ Web \ index.jsp String = realpath ServletContext.getRealPath ( "/index.jsp" ); System.out.println(realPath); //返回路径下所有资源路径; Set<String> resourcePaths = servletContext.getResourcePaths("/WEB-INF"); for (Iterator<String> iterator = resourcePaths.iterator(); iterator.hasNext(); ) { String next = iterator.next(); System.out.println(next); } //获取资源流 InputStream stream = servletContext.getResourceAsStream("/WEB-INF/b.txt"); System.out.println(stream); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { this.doGet(req, resp); } }