java - Serlvet mixing session responses -



i'm developing android printing application. part of application local server receive files users.
implemented server in tomcat java servlet.

problem when 2 devices sending 2 files instantaneously server, there 2 possible results:
1. 1 client receives response, , second client receives empty response.
2. 1 client receives response of second client , vise versa.

here servlet code:

    protected void dopost(final httpservletrequest request, final httpservletresponse response) throws servletexception, ioexception {         new runnable() {              @override             public void run() {                 try {                     request.setcharacterencoding("utf-8");                 } catch (unsupportedencodingexception e2) {                     // todo auto-generated catch block                     e2.printstacktrace();                 }                 response.setcharacterencoding("utf-8");                 try {                     writer = response.getwriter();                 } catch (ioexception e1) {                     // todo auto-generated catch block                     e1.printstacktrace();                 }                 try {                     // access file uploaded client                     part p1 = request.getpart("file");                     inputstream = p1.getinputstream();                      // read filename sent part                     part p2  = request.getpart("metadata");                     scanner s = new scanner(p2.getinputstream());                     string stringjson = s.nextline();    // read filename stream                     s.close();                      json = new jsonobject(stringjson);                     filename = new string(json.getstring("filename").getbytes("utf-8"));                     filedirectory = base + request.getsession().getid();                     file dir = new file(filedirectory);                     dir.mkdir();                     // filename use on server                     string outputfile = base + dir.getname() + "/" + filename;  // path on server                     fileoutputstream os = new fileoutputstream (outputfile);                      // write bytes taken uploaded file target file                     byte[] buffer = new byte[1024];                     int ch = is.read(buffer);                     while (ch != -1) {                          os.write(buffer);                         ch = is.read(buffer);                     }                     os.close();                     is.close();                 }                 catch(exception ex) {                     writer.println("exception -->" + ex.getmessage());                 }                 {                      try {                         myrequest = request;                         try {                             printfile(request.getsession().getid());                         } catch (ioexception e) {                             // todo auto-generated catch block                             writer.println("exception -->" + e.getmessage());                         }                         writer.close();                     } catch (interruptedexception e) {                         writer.println("exception -->" + e.getmessage());                     }                 }                            }         }.run();      } 

the tomcat server running on ubuntu 13.04 virtual machine.
idea?

i don't think use of runnable makes difference it's kind of pointless. can't see declared writer. if that's instance variable of servlet (i.e. not in post method) 1 prone session swapping when used 2 sessions @ same time. try declaring within post method.


Comments

Popular posts from this blog

php - Why I am getting the Error "Commands out of sync; you can't run this command now" -

linux - Does gcc have any options to add version info in ELF binary file? -

java - Are there any classes that implement javax.persistence.Parameter<T>? -