Hi!
I have made a resource servlet to handle static content outside of tomcat,
wicket. It looks like this
package se.edgesoft.hairless.web.resource;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.context.support.WebApplicationContextUtils;
import se.edgesoft.hairless.application.HairlessApplicationSettings;
/**
* Resource servlet for getting images and flash movies
* @author Mathias Nilsson
*
*/
public class FileResourceServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private static ServletConfig config;
File file;
protected Object getBean(String name) {
Object obj =
WebApplicationContextUtils.getWebApplicationContext(config.getServletContext()).getBean(name);
return obj;
}
public void destroy() {
config = null;
}
public ServletConfig getServletConfig() {
return config;
}
public String getServletInfo() {
return "Resource servlet for Hairless";
}
public void init(ServletConfig servletConfig ) throws ServletException {
config = servletConfig;
}
public HairlessApplicationSettings getHairlessApplicationSettings(){
return (HairlessApplicationSettings)getBean(
"hairlessApplicationSettings" );
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException {
try {
file = new File(
getHairlessApplicationSettings().getFileResourcePath() ,
request.getRequestURI().replace( request.getContextPath(), "" ) );
ServletContext context =
getServletConfig().getServletContext();
String mimetype = context.getMimeType(
file.getAbsolutePath()
);
response.setContentType( (mimetype != null) ? mimetype :
"application/octet-stream" );
response.setContentLength( (int)file.length() );
int length = 0;
ServletOutputStream op = response.getOutputStream();
byte[] bbuf = new byte[1024];
DataInputStream in = new DataInputStream(new
FileInputStream(file));
while ((in != null) && ((length = in.read(bbuf)) != -1))
{
op.write(bbuf,0,length);
}
in.close();
op.flush();
op.close();
} catch (Exception e) {
//e.printStackTrace();
}
}
}
Maybe I'm missing something but I think it is a little slow. Should I take
something more into consideration?
--
View this message in context:
http://www.nabble.com/Faster-resource-servlet-tp18079759p18079759.html
Sent from the Tomcat - User mailing list archive at Nabble.com.
---------------------------------------------------------------------
To start a new topic, e-mail: [email protected]
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]