Java's ServletContext object

ServletContext object

First, the concept

  ServletContext object: representative of the web application, the container may program (server) to communicate.

Second, get

  1, obtained by request

    method:

request.getServletContext();

  2, obtained by HttpServlet

    We use the servlet inherited HttpServlet class, so you can get:

this.getServletContext();

  

Third, the function

  1, to obtain the MIME type

    MIME types: one file data type defined in the Internet communication.

    format:

Type large / small type; such as: text / html, image / jpeg, etc.

     Gets the MIME type of a file

String getMimeType(String filename)  

  

  2, domain objects: shared data

    (1) setAttribute (String name, Object value); storing data

    (2) getAttribute (String name); obtaining data

    (3) removeAttribute (String name); removable data

     ServletContent target range: all requested data for all users (with caution)

 

  3, get real file (server) path

    Get the real path method:

String getRealPath(String path)  

      Demo:

1 String path = context.getRealPath ( "/" ); // get the true path of the project
 2  
3 String b = context.getRealPath ( "/ b.txt"); // resource access web directory 
4  System.out.println (B);
 . 5      
. 6   String context.getRealPath C = ( "/ WEB-INF / C.txt"); // resource access in a WEB-INF directory 
. 7  System.out.println (C);
 . 8      
. 9 String a = context.getRealPath ( "/ the WEB-INF / classes / a.txt"); // resources under the src directory access 
10 System.out.println (a);

 

 

 

Guess you like

Origin www.cnblogs.com/niujifei/p/11620266.html