SWFUpload using jsp/struts/java
Swfupload is a great tool for uploading files to your server if you want to use a Flash uploader instead of the default HTML file uploader. It’s a small Javascript/Flash library which gives you a lot of options to customize the upload, a file progress bar being the most useful. Although a great tool, but it works best with php/asp, and not java/struts. The developers of this tool are definitely not java guys either.
Anyway, I tried implementing an swfupload upload for my struts web application, and faced a lot of problems which are outlined:
1) Swfupload won’t send the cookies (it’s a flash problem rather than swfupload’s), so my session could not be validated on the server side. I was using firefox/safari on mac. Doesn’t really matter because it was Flash who was sending the request and not the browser itself. Use firebug, and verify for yourself
2) “post_params” would not be sent along with the request. So any hope of capturing the form data were gone.
So I decided to solve the problem myself, and here is the solution:
For the cookies not being sent to the server, I had to send the JSESSIONID along with the URL. Simple enough, in the JSP, just send
JSESSIONID:"<%=session.getId()%>"
. But on the server side, getting a JSESSIONID is not good enough to get the session, because getSessionContext() is deprecated, and returned a null session
request.getSession().getSessionContext().getSession
(request.getParameter("JSESSIONID"));
So, I decided to manage these sessions myself, so that any user session could be obtained just by using the “JSESSIONID”. For this, I had to add a session listener to the webapp which actively monitors the user sessions, and stores the session in an application level hashmap. Doing this is straightforward, go to your web.xml and add the following code:
<listener> <listener-class> com.anujrathi.pc.filters.SessionListener </listener-class> </listener>
And add this class implementing HttpSessionListener:
package com.anujrathi.pc.filters;
import java.util.HashMap;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class SessionListener implements HttpSessionListener{
Log log = LogFactory.getLog(SessionListener.class);
public void init(ServletConfig config){
log.info("initialed sessionlistener");
}
/**
* Adds sessions to the context scoped HashMap when they begin.
*/
public void sessionCreated(HttpSessionEvent event){
HttpSession session = event.getSession();
ServletContext context = session.getServletContext();
HashMap<String, HttpSession> users = (HashMap)context.getAttribute("users");
users.put(session.getId(), session);
log.info("setsession: "+session.getId());
context.setAttribute("users", users);
}
/**
* Removes sessions from the context scoped HashMap when they expire
* or are invalidated.
*/
public void sessionDestroyed(HttpSessionEvent event){
HttpSession session = event.getSession();
ServletContext context = session.getServletContext();
HashMap users = (HashMap)context.getAttribute("users");
users.remove(session.getId());
log.info("removed session: "+session.getId());
}
}
Go to next page to view the rest of the solution: