Jump to content

java convert string to Object?

xiaofanku's Photo
Posted Nov 27 2009 03:56 AM
10239 Views

i put Employee.toString in Servlet Cookie.what use this String re-Building Employee Object
String like this
id:1,realname:administrator,username:admin,ismanager:false

Employee code:
public class Employee implements Serializable {
	private int id;
	private String realname;                         
        private String username;                          
        private String password="123456";                 
	private Department depart;                        
	private boolean ismanager;   
        //etc get/set                     

3ks everybody's help!
I'Love O'REILLY Book And something

Tags:
0 Subscribe


2 Replies

+ 1
  syndeticdotorg's Photo
Posted Nov 30 2009 09:38 AM

I assume you mean that you want to convert the given string (stored inside a cookie) into an Employee domain object. First, I wouldn't do it the way that you're trying to do it. Instead, I would only store a session ID string inside the cookie, and then maintain a reference on the server that can take the client's session ID and retrieve an appropriate Employee object. You're storing and sending too much sensitive information in that cookie, and if you're using that cookie data for authorization purposes (i.e.: ismanager: false|true, user ID, etc.), then people can override that information and give themselves escalated privileges. Just store the Employee object as session data; it's already marked as Serializable, so take advantage of that and let the servlet container serialize/deserialize (freeze/thaw) the object for you so that whenever the user returns, said Employee object will be immediately available to your application code.
If you still want to store said string, you'll have to parse the comma-separated cookie string and manually construct/reconstruct a new Employee each time.
 : Dec 06 2009 04:59 PM
Thank you for your help!good luck for you