Solved

Serializable not working....

Posted on 2002-05-30
20
498 Views
Last Modified: 2013-11-23
hi ,
 I have following code and it does not work,when i try to read the create a hashmap it crashes saying ClassnotFound.
Also , if I do not use Hashmap and read the records one by one it crashes at second readobject.

Here is the servlet code :

import java.io.* ;
import java.util.* ;
import javax.servlet.* ;
import javax.servlet.http.* ;



class userInfo implements Serializable
{

      private String userName ;
      private      String password ;
      private String lastName ;
      private      String firstName ;
      private      String email ;
      
      public userInfo(String uName, String pswd, String lName, String fName, String email)
      {
            userName = uName ;
            password = pswd ;
            lastName = lName ;
            firstName = fName ;
            email = email ;
      }

      public String getName() { return userName ; }
      public String getPsswd() { return password ; }
}


public class guestBook extends HttpServlet implements Serializable
{
public static final String ACCOUNT = " Account1.LIST" ;
      
public void doGet(HttpServletRequest req, HttpServletResponse res)throws ServletException, IOException
      {
      
PrintWriter out = res.getWriter() ;
out.println(" I am starting... \n") ;

res.setContentType("text/html") ;
HttpSession session = req.getSession(true) ;
//session.removeValue(ACCOUNT) ;
try
{
String userName = req.getParameter("userName") ;
if ( userName == null )
throw new RuntimeException("Please enter the userName !!!" ) ;
userName = userName.trim() ;
if ( userName.equals("") )
throw new RuntimeException("UserName can't be blank !") ;
String PSSWD = req.getParameter("Password") ;
if (PSSWD == null)
throw new RuntimeException("Enter the password !!!!") ;
PSSWD = PSSWD.trim() ;
if ( PSSWD.equals("") )
throw new RuntimeException("Password can't be blank !") ;
String LastName = req.getParameter("LastName") ;
if ( LastName == null )
throw new RuntimeException("Please enter the lastName !!!") ;
LastName = LastName.trim() ;
if ( LastName.equals("") )
throw new RuntimeException("LastName can't be blank !") ;
String FirstName = req.getParameter("FirstName") ;
if ( FirstName == null )
throw new RuntimeException("Please enter the FirstName !!!") ;
FirstName = FirstName.trim() ;
if ( FirstName.equals("") )
throw new RuntimeException("FirstName can't be blank !") ;
String email = req.getParameter("Email") ;
if ( email == null )
throw new RuntimeException("Please enter the email address !!!") ;
email = email.trim() ;
if ( email.equals("") )
throw new RuntimeException("email can't be blank !") ;
File f = new File("test1") ;
out.println(" aboutr to create ") ;
Map tuser = new java.util.HashMap(); //CRASHES HERE : CLASSNOTFOUND

int i = 0 ;

if ( f.exists() )
{
      out.println(" created ") ;
      FileInputStream in = new FileInputStream(f);
      out.println(" created2 \n") ;
      ObjectInputStream s = new ObjectInputStream(in);
      out.println(" aboutr to read\n ") ;
      out.flush() ;
      tuser = (HashMap)s.readObject();
      out.println("read ...\n") ;
      out.flush() ;
      
      while( i < tuser.size())
      {      
      out.println(tuser.size() + "\n") ;
      if ( tuser.containsKey(userName))
      {
      throw new RuntimeException("UserName already Exists!") ;
      }      
       else
      {
      out.println(" am reading object..\n") ;
      //tuser = (userInfo)s.readObject();
      i++ ;
      out.println(" read !") ;
      }
}
in.close() ;
}
else
{
out.println("about to in else  created \n ") ;
tuser = new HashMap() ;
out.println(" created\n") ;
}
userInfo user = new userInfo(userName,PSSWD,LastName, FirstName, email) ;
                              
FileOutputStream os = new FileOutputStream("test1") ;
ObjectOutputStream o = new ObjectOutputStream(os);
out.println(" put \n") ;
tuser.put(userName,user) ;
out.println(" about to write \n") ;
o.writeObject(tuser) ;
out.println(" written \n") ;
os.close() ;
out.println(" <H1> Welcome " + userName + " ! </H1>") ;
out.println(" Your Profile : <P>") ;
out.println(" UserName :  " + userName + " <BR>") ;
out.println(" FirstName : " + FirstName + " <BR>") ;
out.println(" LastName :  " + LastName + " <BR>") ;
out.println(" Email :     " + email + " <BR>") ;
      
}
            catch(RuntimeException e)
            {
                  out.println("<BR> <H1> Invalid information !  </H1> <BR> Please Press Back and ReEnter the information ! ") ;
                  out.println(e.getMessage()) ;
            }
            catch(Exception e)      
            {
                  e.printStackTrace(out);
                  out.println("<BR> <H1> User Info not found.Contact Web Master ! </H1>") ;
            }
            finally { out.flush() ;       }
      }

      public void doPost(HttpServletRequest req, HttpServletResponse res)throws ServletException, IOException
      {
            doGet(req,res) ;

      }

}


and this is the html file

<HTML>
<HEAD>
<TITLE>User Login</TITLE>
</HEAD>

<BODY>
<H3> Welcome to My Site! </H3> <H4>To go Further Please Create Ur Profile...</H4>
<BR>
<BR>
<FORM METHOD=POST ACTION="http://localhost:8080/servlet/guestBook">
<H5>
UserName
<INPUT TYPE="TEXT" NAME="userName">          
<P>
Password                    
<INPUT TYPE="PASSWORD" NAME="Password"> <P>
LastName            
<INPUT TYPE="TEXT" NAME="LastName"> <P>
FirstName                
<INPUT TYPE="TEXT" NAME="FirstName"> <P>
Email    
<INPUT TYPE="TEXT" NAME="Email"> <P>
</H5>
<P>

<INPUT TYPE="SUBMIT" VALUE="Create Profile">
</FORM>
</BODY>
</HTML>

      
0
Comment
Question by:xzyeena
  • 5
  • 5
  • 4
  • +3
20 Comments
 
LVL 18

Expert Comment

by:bobbit31
ID: 7045292
how bout:
java.util.Map tuser = new java.util.HashMap();
0
 

Author Comment

by:xzyeena
ID: 7045353
that didn't work either.
0
 
LVL 19

Expert Comment

by:Jim Cakalic
ID: 7045376
You should flush the ObjectOutputStream before closing. In the code you posted, you write to the ObjectOutputStream and then close the FileOutputStream. Try this instead:

    o.writeObject(tuser);
    out.println(" written \n") ;
    o.close();

Best regards,
Jim Cakalic
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 19

Expert Comment

by:Jim Cakalic
ID: 7045388
Alternatively, as the javadoc for ObjectOutputStream suggests:

    o.writeObject(tuser);
    out.println(" written \n");
    o.flush();
    os.close();

Either way (this or my previous post) should work as the implementation of ObjectOutputStream.close first calls flush and then closes the OutputStream that it decorates -- the FileOuputStream in this case. My preference is usually the former -- my first post -- because once I've decorated another object, unless I _really_ need that object for another reason, I prefer to forget that it exists.

Jim
0
 

Author Comment

by:xzyeena
ID: 7045396
that didn't work either.
0
 
LVL 19

Expert Comment

by:Jim Cakalic
ID: 7045433
I just noticed, and I apologize for that, you said the problem occurs when attempting to create a java.util.HashMap? Do you have the stack trace on the exception?
0
 

Author Comment

by:xzyeena
ID: 7045450
that didn't work either.
0
 
LVL 35

Expert Comment

by:girionis
ID: 7045452
 Well I just copied/pasted your code on my machine, compiled it and ran it. Everything seems fine. The result I am getting is:

I am starting...

 aboutr to create
 created
 created2

 aboutr to read
 
read ...

1

 am reading object..

 read !
 put

 about to write

 written


Welcome girionis !
Your Profile :
UserName :  girionis
FirstName : girionisFirstNa me
LastName :  girionisLastName
Email :     girionis@yahoo.com

  Everything seems fine to me. What exactly is this supposed to display? Are you sure that the classloaders load the proper classes and do npot interfere with other non-java standards?

  Hope it helps.
0
 
LVL 92

Expert Comment

by:objects
ID: 7045647
Sounds like you may be running a servlet container that only supports Java 1.1, or there's a problem with your servlet container setup.

Try:

java.util.Hashtable tuser = new java.util.Hashtable();

What servlet container are you using?
 
0
 

Author Comment

by:xzyeena
ID: 7045786
I am using java web server 2.0.
0
 

Author Comment

by:xzyeena
ID: 7045806
I am using java web server 2.0.
0
 
LVL 92

Expert Comment

by:objects
ID: 7045864
Did you try using a Hashtable instead of a HashMap?
0
 

Expert Comment

by:plrf
ID: 7046263
  beware with class that implements Serializable interface.

  For that a class be completly Serializable your propierties that be object would be too Serializables. To verify the java' documentation.
0
 
LVL 35

Accepted Solution

by:
girionis earned 200 total points
ID: 7046465
 Please pirf do not propose answers as this locks the question and it is difficult for other experts to add their comments. Propose comments instead as they can be accepted as answers.

  xzyeena java web server is an old reference implementation of the servlets specification. Why not try to use Tomcat instead. And always download the latest Servlet APIs.

  Hope it helps.
0
 
LVL 19

Expert Comment

by:Jim Cakalic
ID: 7047337
Good point, objects. Some of us -- me for one -- have been using JDK 1.2+ for so long we forget that anything else even existed!

I found the Java Web Server 2.0 release notes
    http://wwws.sun.com/software/jwebserver/techinfo/jws20/release_notes.html

and read:

----- begin quote -----
Java 2 now supported
Java Web Server 2.0 supports, but does not bundle, the new JavaTM 2 SDK, Standard Edition, v 1.2 (formerly code-named "JDK 1.2"). To run the Java Web Server with the Java 2 SDK, you must use the following startup command:

jserv -javahome <path to Java 2>

When using -javahome option, you must point to a full JDK image (which includes javac). For Java 2 support on Solaris, this means that you must install the development tools package (SUNWjsdev) in addition to the runtime package (SUNWj2rt).

For the latest Solaris Java 2 SDK and associated Operating System patches, please see:

http://www.sun.com/solaris/java 

New version of JRE being bundled
Java Web Server 2.0 now installs, and uses by default, Java Runtime Environment (JRE) 1.1.7. The new JRE is expected to further enhance the Java Web Server's performance.
----- end quote -----

Maybe give that suggestion a try. The release notes has other info about using JDK 1.2 on Windows so you might take a read through that just to beware.

Jim
0
 
LVL 92

Expert Comment

by:objects
ID: 7047970
A lot easier to just use a Hashtable :-) , I didn't see any other Java2 dependencies in the code.
And considering JWS is dead, moving to Tomcat sounds like a good idea.
0
 
LVL 35

Expert Comment

by:girionis
ID: 7051884
 Thank you for the points. I am glad I helped :-)
0
 
LVL 92

Expert Comment

by:objects
ID: 7052374
As I mentioned earlier:

Sounds like you may be running a servlet container that only supports Java 1.1.
0
 
LVL 18

Expert Comment

by:bobbit31
ID: 7053821
objects: you win some, you lose some ;)
0
 
LVL 92

Expert Comment

by:objects
ID: 7054792
Yes it often happens that way :-)
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question