With the development of Web technology, enterprise-class Web applications have been very popular, and servlet technology has been used as examples, web browsers, servlet intermediate layers, background database servers constitute this three-layer structure, which is very clear, design and development. Brought a lot of convenience, but everyone knows that between layers and layers, plus a large number of logical operations and data reads are completed in the intermediate layer server, resulting in the performance of the Web, has always made the developers a headache , I will write out some ideas in the process of developing a document center system on how to improve JSP performance, I hope everyone will mention.
My idea is to save objects as a file. There are many formats in this file, which can be XML format, or a custom file generated by serialized objects, here I talk about the latter one. The intermediate layer is not read from the database when reading data, but is also the same when reading local files, new, modifications. Reduce the number of intermediate servers connected to the database server, use the hard disk file read and write the intermediate layer server to replace the Database, improve the performance of JSP.
Specific implementation is as follows: Taking a document object (document) as an example, my document data table holds a copy in the database, the document object is saved in the middle-level server local hard disk in the form of the file, and the user enters the reading interface Read one document When this page will generate a document object based on the requested document ID, and the document doc = new document (ID), then other properties of the document are obtained from the document ID named file,
FileInputStream Fis = New FileInputStream (Doc.getId ());
ObjectInputStream Ois = New ObjectInputStream (FIS);
DOC = (document) os.readObject ();
New or modify the documentation:
Document doc = new document ();
Doc.setid (...);
Doc.SetTitle (...);
Doc.setKeyword (...);
......
FileOutputStream Fos = New FileOutputStream (Doc.getID ());
ObjectOutputStream Oos = New ObjectOutputStream (FOS);
Oos.writeObject (DOC);
OOS.FLUSH ();
Generate new or overwriting the original file, when the user read this document (in this system, when it is new or modified after the document is added, I will go to the document page immediately), I will add this document in the background. In the database, this user does not have to wait until all the operations are completed before you can see the page, which also spreads the burden on the intermediate layer server, because the document table has been associated with a lot of views, so I update file and data at the same time when I added or modified. Table, similarly, the logs such as the log, you can use only the corresponding files can be used, and then update the comments when the intermediate server is idle. This effect will be better.
Let's introduce it to its implementation, you want to save the object and copy it to rebuild objects, this object must
Implement serializableInterface, for this class, this interface does not need to implement any method, it
Mainly inform Java Virtual Machines (JVM), you need to serialize an object, for us developers, just imports
SERIALIZABLE can, don't do anything else,
There are two main classes that read or write the stream: ObjectOutputStream and ObjectInputStream.
ObjectOutputStream provides WriteObject method to write objects to output streams, ObjectInputStream
The ReadObject method for reading the object from the input stream. It is determined whether a class has been serialized, and it can be judged by JDK's Serial Vervice:
C: / j2sdk
1.4.1
/ bin> serialver java.util.timezone
Java.util.Timezone: static final long serialversionuid = 3581463369166924961L;
I have written such an example here, and everyone will be at a glance:
Package com.test;
Import java.io. *;
Public Class Test
{
Public static void main (string [] args)
{
Try
{
FileOutputStream Os = New FileoutputStream ("Test.out");
ObjectOutputStream O = New ObjectOutputStream (OS);
Plant Plant = New Plant ("apple");
O.WriteObject (Plant);
O.flush ();
FileInputStream Fs = New FileInputStream ("Test.out");
ObjectInputStream OSS = New ObjectInputStream (fs);
Plant P = (Plant) oss.readObject ();
System.out.println (p.getname ());
}
Catch (filenotfoundexception)
e) {system.out.println ("FilenotFoundException:" E.getMessage ());
Catch (IOEXCEPTION EE) {system.out.println ("IOEXCEPTION:" EE.GETMESSAGE ());
Catch (ClassNotFoundException)
EE) {system.out.println ("IOEXCEPTION:" EE.GETMESSAGE ());
Class Plant Implements Serializable // Internal Class Implementation SERIALIZABLE Interface
{
PRIVATE STRING NAME;
Public Plant (String PNAME)
{
Name = PNAME;
}
Public string getName ()
{
Return Name;
}
}
}
}
Run results: Apple