Solved

Serializable not working....

Posted on 2002-05-30
20
493 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
 
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
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 third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

744 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now