Pradeepta Bhattacharya

Greenhorn
+ Follow
since Sep 07, 2001
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Pradeepta Bhattacharya

does anyone know how the servlet is generated by reading a jsp file. Can it be possible to convert the jsp file in a DOM object and then applying a predefined template thru an XSLT to generate the servlet file ..... what are the other considerations that usually goes in the process of developing a servlet out of a jsp page can the same procedure if i am right be used to generate the code for other technologies like EJB's and other templates .. kindly reply back as this is urgent
thanks in advance
21 years ago
does anyone know how the servlet is generated by reading a jsp file. Can it be possible to convert the jsp file in a DOM object and then applying a predefined template thru an XSLT to generate the servlet file ..... what are the other considerations that usually goes in the process of developing a servlet out of a jsp page can the same procedure if i am right be used to generate the code for other technologies like EJB's and other templates .. kindly reply back as this is urgent
thanks in advance
21 years ago
JSP
use filters available in servlets package to trap the output and then store them in the html files or anywhere you might want to use. that the easier way i think .. or else you can write your own writer and then i think you need to map your writer to the runtime environment which is more complicated as usually the PageContext class will instanciate your jspwriter and maps it to the default jspfactory and makes it available to your jsp page and then replace your own writer with the one present in the jsp i.e implicitly you get the out object in jsp and you need to replace it with your own writer and then i hope you can use the writer to get the desired output.
I have tried out both the ways but i feel that the filters are the better and easier way to do the task. If i am wrong plz do inform me. Thanks in advance
21 years ago
JSP
Build your own ObjectPool in Java to boost app speed
Increase the speed of your applications while reducing memory requirements
Summary
Object pooling allows the sharing of instantiated objects. Since different processes don't need to reinstantiate certain objects, there is no load time. And since the objects are returned to the pool, there is a reduction in garbage collection. Read on to learn, via the author's own example object pool design, how to employ this approach to boost performance and minimize memory use. (1,400 words)
By Thomas E. Davis

hen I first implemented object pooling (specifically for database connections) into a realtime, multiuser application, average transaction times dropped from 250 milliseconds to 30 milliseconds. Users actually noticed and commented on the drastic increase in performance.
The idea of object pooling is similar to the operation of your local library. When you want to read a book, you know that it's cheaper to borrow a copy from the library rather than purchase your own copy. Likewise, it is cheaper (in relation to memory and speed) for a process to borrow an object rather than create its own copy. In other words, the books in the library represent objects and the library patrons represent the processes. When a process needs an object, it checks out a copy from an object pool rather than instantiate a new one. The process then returns the object to the pool when it is no longer needed.
There are, however, a few minor distinctions between object pooling and the library analogy that should be understood. If a library patron wants a particular book, but all the copies of that book are checked out, the patron must wait until a copy is returned. We don't ever want a process to have to wait for an object, so the object pool will instantiate new copies as necessary. This could lead to an exhoribitant amount of objects lying around in the pool, so it will also keep a tally on unused objects and clean them up periodically.
My object pool design is generic enough to handle storage, tracking, and expiration times, but instantiation, validation, and destruction of specific object types must be handled by subclassing.
Now that the basics out of the way, lets jump into the code. This is the skeletal object:
public abstract class ObjectPool
{
private long expirationTime;
private Hashtable locked, unlocked;
abstract Object create();
abstract boolean validate( Object o );
abstract void expire( Object o );
synchronized Object checkOut(){...}
synchronized void checkIn( Object o ){...}
}

Internal storage of the pooled objects will be handled with two Hashtable objects, one for locked objects and the other for unlocked. The objects themselves will be the keys of the hashtable and their last-usage time (in epoch milliseconds) will be the value. By storing the last time an object was used, the pool can expire it and free up memory after a specied duration of inactivity.
Ultimately, the object pool would allow the subclass to specify the initial size of the hashtables along with their growth rate and the expiration time, but I'm trying to keep it simple for the purposes of this article by hard-coding these values in the constructor.
ObjectPool()
{
expirationTime = 30000; // 30 seconds
locked = new Hashtable();
unlocked = new Hashtable();
}

The checkOut() method first checks to see if there are any objects in the unlocked hashtable. If so, it cycles through them and looks for a valid one. Validation depends on two things. First, the object pool checks to see that the object's last-usage time does not exceed the expiration time specified by the subclass. Second, the object pool calls the abstract validate() method, which does any class-specific checking or reinitialization that is needed to re-use the object. If the object fails validation, it is freed and the loop continues to the next object in the hashtable. When an object is found that passes validation, it is moved into the locked hashtable and returned to the process that requested it. If the unlocked hashtable is empty, or none of its objects pass validation, a new object is instantiated and returned.
synchronized Object checkOut()
{
long now = System.currentTimeMillis();
Object o;
if( unlocked.size() > 0 )
{
Enumeration e = unlocked.keys();
while( e.hasMoreElements() )
{
o = e.nextElement();
if( ( now - ( ( Long ) unlocked.get( o ) ).longValue() ) >
expirationTime )
{
// object has expired
unlocked.remove( o );
expire( o );
o = null;
}
else
{
if( validate( o ) )
{
unlocked.remove( o );
locked.put( o, new Long( now ) );
return( o );
}
else
{
// object failed validation
unlocked.remove( o );
expire( o );
o = null;
}
}
}
}
// no objects available, create a new one
o = create();
locked.put( o, new Long( now ) );
return( o );
}

That's the most complex method in the ObjectPool class, it's all downhill from here. The checkIn() method simply moves the passed-in object from the locked hashtable into the unlocked hashtable.
synchronized void checkIn( Object o )
{
locked.remove( o );
unlocked.put( o, new Long( System.currentTimeMillis() ) );
}

The three remaining methods are abstract and therefore must be implemented by the subclass. For the sake of this article, I am going to create a database connection pool called JDBCConnectionPool. Here's the skeleton:
public class JDBCConnectionPool extends ObjectPool
{
private String dsn, usr, pwd;
public JDBCConnectionPool(){...}
create(){...}
validate(){...}
expire(){...}
public Connection borrowConnection(){...}
public void returnConnection(){...}
}

The JDBCConnectionPool will require the application to specify the database driver, DSN, username, and password upon instantiation (via the constructor). If this is Greek to you, don't worry, JDBC is another topic. Just bear with me until we get back to the pooling.
public JDBCConnectionPool( String driver, String dsn, String usr, String pwd )
{
try
{
Class.forName( driver ).newInstance();
}
catch( Exception e )
{
e.printStackTrace();
}
this.dsn = dsn;
this.usr = usr;
this.pwd = pwd;
}

Now we can dive into implementation of the abstract methods. As you saw in the checkOut() method, ObjectPool will call create() from its subclass when it needs to instantiate a new object. For JDBCConnectionPool, all we have to do is create a new Connection object and pass it back. Again, for the sake of keeping this article simple, I am throwing caution to the wind and ignoring any exceptions and null-pointer conditions.
Object create()
{
try
{
return( DriverManager.getConnection( dsn, usr, pwd ) );
}
catch( SQLException e )
{
e.printStackTrace();
return( null );
}
}

Before the ObjectPool frees an expired (or invalid) object for garbage collection, it passes it to its subclassed expire() method for any necessary last-minute clean-up (very similar to the finalize() method called by the garbage collector). In the case of JDBCConnectionPool, all we need to do is close the connection.
void expire( Object o )
{
try
{
( ( Connection ) o ).close();
}
catch( SQLException e )
{
e.printStackTrace();
}
}
And finally, we need to implement the validate() method that ObjectPool calls to make sure an object is still valid for use. This is also the place that any re-initialization should take place. For JDBCConnectionPool, we just check to see that the connection is still open.
boolean validate( Object o )
{
try
{
return( ! ( ( Connection ) o ).isClosed() );
}
catch( SQLException e )
{
e.printStackTrace();
return( false );
}
}

That's it for internal functionality. JDBCConnectionPool will allow the application to borrow and return database connections via these incredibly simple and aptly named methods.
public Connection borrowConnection()
{
return( ( Connection ) super.checkOut() );
}
public void returnConnection( Connection c )
{
super.checkIn( c );
}

This design has a couple of flaws. Perhaps the biggest is the possibility of creating a large pool of objects that never gets released. For example, if a bunch of processes request an object from the pool simultaneously, the pool will create all the instances necessary. Then, if all the processes return the objects back to the pool, but checkOut() never gets called again, none of the objects get cleaned up. This is a rare occurrence for active applications, but some back-end processes that have "idle" time might produce this scenario. I solved this design problem with a "clean up" thread, but I'll save that discussion for a follow-up article. I will also cover the proper handling of errors and propagation of exceptions to make the pool more robust for mission-critical applications.
I welcome any comments, criticisms, and/or design improvements you may have to offer.
21 years ago
I have a requirement that i need a filter in the out (JspPrintWriter) which is implicitly available to the JSP page which will gather the data that is been sent thru the JSP Page to the clients stream. Can anyone kindly suggest .. probably with some small code snippets or example as to how to achieve this .. i need it urgently
Thanks in advance
21 years ago
JSP
thnx but that option is already avalable thru some projects like jawin and bridge2java .... i need some solution like POI which is a pure solution and platform independent too. ... as thats the one aspect we are actually loking fr .. thnx anyway
21 years ago
ya i have to read a powerpoint file and then prepare an xml file using only the text content as that xml file will be used to facilitate search funtionality. I gotta use it on unix so i can't go for IBM Bridge2Java or jawin or any win32 integration project.
21 years ago
Hello Friends,
I have an urgent requirement related to reading a powerpoint file using java but the complexity is that i can't use tools like JIntegra or IBM Bridge2java as it may be required to run in non office or non windows enviornment. Any help is highly appreciated.
Thanks in advance
21 years ago
Hello Friends,
I have an urgent requirement related to reading a powerpoint file using java but the complexity is that i can't use tools like JIntegra or IBM Bridge2java as it may be required to run in non office or non windows enviornment. Any help is highly appreciated.
Thanks in advance
21 years ago
Dear John:
Thanks For Letting me know the soln.
Bye
Pradeepta
22 years ago
Try Using AppletContext to get an instance of the other applet
[This message has been edited by Pradeepta Bhattacharya (edited September 09, 2001).]
22 years ago

Originally posted by christopson:
I have a question.
How can I get the URL if I read it directly from the keyboard?
Hello all,
For example I run the following program:
javac myurl.java http://www.mysite.com/index.html
Can I do something like this? Or I have to hard code the URL inside the class?
//////////////////////// myurl.java///////////////
public class myurl {
public static void main(String[] args) throws Exception {
URL aURL = new URL(args);
}
}
Could someone help me with this ?


i didn't get why javac urlname

Originally posted by L.John:
Hi,
It would be kind if anybody could tell me why i am getting :java.io.FileNotFoundException:
on line
InputStream in = uConnect.getInputStream();


See wether you r trying to invoke doGet or doPost .... i think doGet is not Possible by this try doPost once i hope ur code will work ......
22 years ago

Originally posted by Ify Innoma:
What is the reason why an applet will be running in appletviewer but not in a browser.


Try Out With This Html ... i Hope it will solve your problem
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<html>
<head>
<title>Idea Management System</title>
<script language="JavaScript">
function MyToolBar()
{
window.open("Login.html","LoginPage","toolbar=0,menubar=0,scrollbar=0,resizable=0,directories=0,width='110%',height='150%'");
}
</script>
</head>
<body BGCOLOR="#ffffff" LINK="#000099">
<blockquote>
<!--"CONVERTED_APPLET"-->
<!-- CONVERTER VERSION 1.1 -->
<SCRIPT LANGUAGE="JavaScript"><!--
var _info = navigator.userAgent; var _ns = false;
var _ie = (_info.indexOf("MSIE") > 0 && _info.indexOf("Win") > 0 && _info.indexOf("Windows 3.1") < 0);
//--></SCRIPT>
<COMMENT><SCRIPT LANGUAGE="JavaScript1.1"><!--
var _ns = (navigator.appName.indexOf("Netscape") >= 0 && ((_info.indexOf("Win") > 0 && _info.indexOf("Win16") < 0 && java.lang.System.getProperty("os.version").indexOf("3.5") < 0) | | (_info.indexOf("Sun") > 0) | | (_info.indexOf("Linux") > 0)));
//--></SCRIPT></COMMENT>
<SCRIPT LANGUAGE="JavaScript"><!--
if (_ie == true) document.writeln('<Center><OBJECT classid="clsid:8AD9C840-044E-11D1-B3E9-00805F499D93" WIDTH = "100%" HEIGHT = "150%" codebase="http://java.sun.com/products/plugin/1.1.2/jinstall-112-win32.cab#Version=1,1,2,0"><NOEMBED><XMP>');
else if (_ns == true) document.writeln('<EMBED type="application/x-java-applet;version=1.1.2" java_CODE = "MyApplet.class" WIDTH = "110%" HEIGHT = "150%" pluginspage="http://java.sun.com/products/plugin/1.1.2/plugin-install.html"><NOEMBED><XMP>');
//--></SCRIPT>
<APPLET CODE = "MyApplet.class" WIDTH = "110%" HEIGHT = "150%" ></XMP>
<PARAM NAME = CODE VALUE = "MyApplet.class" >
</APPLET>
</NOEMBED></EMBED></OBJECT></Center>

<!--
<APPLET CODE = "MyApplet.class" WIDTH = "110%" HEIGHT = "150%" >

</APPLET>
-->
</body>
</html>
22 years ago

Originally posted by john klucas:
I want to have FileDialog which will be displayed when user clicks on a save button.
when the file dialog is displayed the file should be written to seletecd folder.
Can any one show me how to do that?
I will appreciate it!



I think This Will Solve Your Problem ..Please imtimate me if you need something else
Thanking You
Pradeepta
The Code :
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
public class SaveFile extends Frame implements ActionListener
{
Button b;
FileDialog fd;
String dir,filename,extention;
byte[] barr=null;
public SaveFile()
{
super("Save File Demo");
setLayout(new FlowLayout());
b=new Button("Open");
add(b);
b.addActionListener(this);
}
public void actionPerformed(ActionEvent ae)
{
try
{
if(ae.getActionCommand().equals("Open"))
{
fd=new FileDialog(this,"Open Dialog",FileDialog.LOAD);
fd.show();
dir=fd.getDirectory();
filename=fd.getFile();
System.out.println(dir+" "+filename);
if(dir.length()>0 && filename.length()>0){
b.setLabel("Save");
FileInputStream fis=new FileInputStream(dir+filename);
barr = new byte[fis.available()];
fis.read(barr,0,fis.available());
StringTokenizer stoken=new StringTokenizer(filename,".");
while(stoken.hasMoreTokens())
extention=stoken.nextToken();
fis.close();
}
}
else if(ae.getActionCommand().equals("Save"))
{
if(dir.length()>0 && filename.length()>0){
fd=new FileDialog(this,"Save Dialog",FileDialog.SAVE);
fd.show();
dir=fd.getDirectory();
filename=fd.getFile();
FileOutputStream fos=new FileOutputStream(dir+filename+"."+extention);
fos.write(barr);
fos.close();
b.setLabel("Open");
}
}
}catch(Exception e){System.out.println(e);}
}
public static void main(String[] args)
{
SaveFile sf=new SaveFile();
sf.setSize(400,400);
sf.setVisible(true);
}
}
22 years ago