The session in the JSP

Source: Internet
Author: User

JSP session is a lifetime of the use of beans, generally page,session means that the user has not left the site before the effective, if unable to determine when users leave, generally based on system settings, Tomcat is set to 30 minutes. We can use the session function to reach multiple JSP programs from the same Java bean, so this Java bean can be used as our "global variable Pool" in our traditional sense. (in Java we can use static to statically initialize a variable and method, using singleton to uniquely object.) In the project practice, we JSP program many parameters need to read from the database, and some of the parameters are actually read once, if designed to each user to read the database every page, it is clear that the database load is very large, but also a waste of time, although there may be database connection pool optimization, But using as few databases as possible is the principle of our programming. JSPs use an object called HttpSession to achieve the same functionality. HTTPSession is a high-quality interface built on cookies and url-rewriting. The session information is stored on the server side, and the session ID is stored in the client's cookie. In fact, on many servers, cookies are used if supported by browsers, but automatically converted to Url-rewriting,session automatically provides a convenient way to store information for each process if it is not supported or repealed. HttpSession has the following Api:getid this method returns a unique identity, which is generated for each session. When there is only one single value associated with a session, or if the log information is related to the previous sessions, it is used as the key name. GetCreationTime returns the time that the session was created. The minimum unit is 1 per thousand seconds. To get a value that is useful for printouts, you can pass this value to the method of date constructor or GregorianCalendar Settimeinmillis.getlastaccessedtime Returns the time that the session was last sent by the customer. The minimum unit is 1 per thousand seconds. Getmaxinactiveinterval returns the total time (in seconds), and a negative value indicates that the session never times out. GetAttribute take a session with the information associated with it. (GetValue in jsp1.0) Integer item = (integer) session.getattribute ("item")//Retrieved SessIon values and translates to integer setattribute provides a keyword and a value. will replace any previous value. (Putvalue in jsp1.0) session.setattribute ("Itemvalue", itemname);//Itemvalue The must simple type must not be the most used in the app is GetAttribute and setattribute. A simple example is given to illustrate the application of the session, the Test1.jsp (information is written to the session), and the test2.jsp (read the information from the session). test1.jspsession has the following features 1, not thread-safe, should avoid multiple threads sharing the same session instance 2,session instance is lightweight, so-called lightweight: refers to his creation and deletion does not need to consume too much resources 3,session object inside there is a cache, called Hibernate first cache, He stores objects that are loaded in the current unit of work, and each session instance has its own cache. Org.hibernate Interface sessionpublic Interface Session extends Serializable: is a primary runtime interface between Java application and hibernate, This is the primary method for implementing the persistence service's Central API: Public Transaction BeginTransaction () throws Hibernateexception: Returns the Transaction object that is associated with the current session object (indicating that a transaction is restarted in the database) public Transaction gettransaction () : Returns the transaction object that is associated with the current session public Connection Connection close () throws Hibernateexcepton: Ends the current session object public void Clear (): Empties the session, clears all entity objects stored in the current session cache, terminates all executing methods (Eg:save (), update (), delete () ...) Public Serializable Save (object object) throws hibernateexception to persist the object specified by the current parameter (the system will first give the parameter object an identifier OID), which is equivalent to the INSERT statement Later in the details of public Connection Connection () throws Hibernateexception get the Connection objects contained in the current session. Public Boolean contains (Object object): Determines whether the object given by the parameter (persisted Class) is in the cache of the current session public void evict (object object) throws Hibernateexception: The object given by the argument is from the current SessioN object class, so that the object from the persistent state into a free State, this change of state does not cause synchronization of the database, the following details the public object load (Class theclass,serializable ID) throws Hibernateexception returns the first parameter specified in the table corresponding to the class, the second parameter specifies the row (the second parameter is to get the OID of the object, he corresponds to the value of the primary key column in the table) public void Update (Object object) throws Hibernateexception: Update an object into the database, followed by the public void Delete (Object object) in detail throws Hibernateexception: The record that corresponds to the object specified by the parameter is deleted from the database public object get (Class class,serializable ID) throws Hibernateexception: The same difference as the load () method is that if there is no corresponding record in the database table, the Get () method returns the Null,load () method to report the exceptionTransactionThe Transanction interface is Hibernate's database transaction interface, which is used to manage transactions, and he encapsulates the underlying transaction, which allows the user to define their own atomic operations on the database using the Transanction object, including the following: JDBC Api,jta ( Java Transaction API). A transaction for a Transaction object may include multiple operations on the database org.hibernate Interface transactionpublic Interface TransactionCommon Methodspublic void Commit () throws Hibernateexception refreshes the current session and ends the transaction, which forces the database to commit to the current transaction public void rollback () throws Hibernateexception: Force rollback of the current transaction public boolean isActive () throws Hibernateexception: Does the transaction survive----------------------------------------------------------------------------------------Sessio N: Contains a Connection object connection C =session.getconnection (); The session's cache is used to temporarily save persisted objects, and then save the cached objects to the database at some point. Application transactions: If a session contains multiple transaction (database transactions), the collection of these transaction is called the application transaction standard usage form: Configuration config=new Configuration (). Configure ("Hibernate.cfg.xml"); Sessionfactory sessionfactory=config.buildsessionfactory (); Session session=sessionfactory.opensession (); Transaction tx=session.begintransaction (); Try{session.save (); Tx.commit ();} catch (Exception e) {if (tx!=null) Tx.rollback ();} Finally{session.close ();}

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.