JSP built-in object--application object

Source: Internet
Author: User

The Application object is an instantiated object of the Javax.servlet.ServletContext interface. Is the context of the entire servlet and represents the operation of the entire Web container.

Common methods:

1.java.lang.string getrealpath(java.lang.String path): Obtains the absolute path corresponding to the virtual directory;

2. java.util.enumeration<java.lang.string> getattributenames(): Get the names of all the properties

3. Java.lang.String Getcontextpath(): Gets the current virtual path name

In addition to the above 3 methods, the addition of attributes, acquisition and deletion has also been applied, SetAttribute (), getattribute (), RemoveAttribute ()

    • Get absolute path

Example:

<%@ page contenttype= "text/html" pageencoding= "GBK"%>//http://localhost/ mldn/    String path = Application.getrealpath ("/"); %>

Operation Result:

True path: D:\Workspace\

This true path is configured in Server.xml.

It is important to note that the application operation itself is an instance of the ServletContext interface, but the function of a method in JSP can be fully corresponding to it, Getservletcontext ();

<%@ page contenttype= "text/html" pageencoding= "GBK"%>//http://localhost/ mldn/this    . Getservletcontext (). Getrealpath ("/"); %>

The effect of these two operations is exactly the same, under normal circumstances, all methods are not called by the object? Then why is there no object in this?

If you want to add an object, you need to use this to indicate.

Be sure to remember that this.getservletcontext () is very important!!! Replace application operation in actual use

Because the absolute path is obtained, it means that the JSP can do the file operation.

If you want to perform a file operation, one will find a specified path through a single file class. This path is preferably an absolute path, and at this point the Getrealpath () method works because all web directories are alive.

Input_content.htm:

<HTML><HEAD><TITLE>WWW.MLDNJAVA.CN,MLDN High-end Java training </title>    Input file name:<input type= "text" name= "filename" > <br>     Input file contents:<textarea name= "Filecontent" cols= "3" rows= "></textarea><br>    <input type= "Submit" value= "save" >    <input type= "reset" value= "reset" ></form></body> 

INPUT_CONTENT.JSP:

<%@ page contenttype= "text/html" pageencoding= "GBK"%><%@ pageImport= "java.io.*"%><%@ pageImport= "Java.util.*"%&GT;&LT;HTML&GT;&LT;HEAD&GT;&LT;TITLE&GT;WWW.MLDNJAVA.CN,MLDN high-end Java training </title>request.setcharacterencoding ("GBK");//Solve garbled problemsString name = Request.getparameter ("filename") ; String content= Request.getparameter ("Filecontent") ; //To manipulate the file must have an absolute path, then this time Getrealpath ()String FileName = This. Getservletcontext (). Getrealpath ("/") + "Note" + File.separator + name;//Save in note folderFile File =NewFile (FileName);//instantiating a file class object    if(!File.getparentfile (). exists ())    {File.getparentfile (). mkdir (); //Create a folder} printstream PS=NULL ; PS=NewPrintStream (Newfileoutputstream (file));    PS.PRINTLN (content); Ps.close ();%><%Scanner Scan=NewScanner (Newfileinputstream (file)); Scan.usedelimiter ("\ n") ; StringBuffer buf=NewStringBuffer ();  while(Scan.hasnext ()) {Buf.append (Scan.next ()). Append ("<br>") ; } scan.close ();%><%=buf%></body>

Website Counter Features:

1. The number of sites may be many, need to use BigInteger to complete

2. The user will need to count the operation every time the first time they visit. You must use IsNew () to determine before performing calculations

3. Change, save the time need to synchronize operation

count.jsp;

<%@ page contenttype= "text/html" pageencoding= "GBK"%><%@ pageImport= "java.io.*"%><%@ pageImport= "java.util.*"%><%@ pageImport= "java.math.*"%&GT;&LT;HTML&GT;&LT;HEAD&GT;&LT;TITLE&GT;WWW.MLDNJAVA.CN,MLDN high-end Java training </title>BigInteger Count=NULL ;%><%!//for ease of development, all operations are defined in the method, and all exceptions are added directly to the complete Try...catch processing     PublicBigInteger Load (file file) {BigInteger count=NULL;//Receive Data        Try{            if(File.exists ()) {Scanner Scan=NewScanner (Newfileinputstream (file)); if(Scan.hasnext ()) {count=NewBigInteger (Scan.next ());            } scan.close (); } Else{//should save a new one, starting from 0Count =NewBigInteger ("0") ;    Save (File,count); //Save a new file            }        }Catch(Exception e) {e.printstacktrace (); }        returncount; }     Public voidSave (File File,biginteger count) {Try{printstream ps=NULL ; PS=NewPrintStream (Newfileoutputstream (file));            Ps.println (count);        Ps.close (); }Catch(Exception e) {e.printstacktrace (); }    }%><%String FileName= This. Getservletcontext (). Getrealpath ("/") + "count.txt";//This keeps the results of all the CountsFile File =NewFile (fileName); if(Session.isnew ()) {synchronized( This) {Count= load (file);//ReadCount = Count.add (NewBigInteger ("1"));//1 more on the original basis. Save (File,count); }    }%>NULL?0:count%>-bit visitors! 

    • View Properties

There is also a property operation in application, there is a way to get all the properties: Getattributenames ()

 <%@ page contenttype= "text/html" pageencoding= "GBK"%><%@ page import  = "java.util.*"%><HTML><HEAD><TITLE>WWW.MLDNJAVA.CN,MLDN high-end Java training </title></ Head><body><% enumeration enu  = thi    S . Getservletcontext (). Getattributenames (); //     Get all the properties  while    (Enu.hasmoreelements ()) {string name  = (String) Enu.nextelement ();  %> this . Getservletcontext (). GetAttribute (name)%>%></body>

Third-party jar files configured with Tomcat are set to the server via the Application property, so the server must be restarted each time a new development package is configured.

Summarize:

1. Application represents the context of the resource environment

2. In practice, you can use the This.getservletcontext () method instead of the application

3. Use the Getrealpath () method to obtain a virtual directory corresponding to the true path

JSP built-in object--application object

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.