I have a gwt web application with a download servlet and I count downloads of every file in my db. When user sends a download request to server, my download servlet runs twice and my download counter counts 2 every time. but the file that user has sent request for it, has been downloaded once. I don't know whay my servlet runs 2 time for 1 request.
my download servlet:
public class DownloadServlet extends HttpServlet {
private static final String UPLOAD_DIRECTORY = "c:\\update\\";
private Connection con;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
Statement select=null;
ResultSet result=null;
try{
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/laplasdb?useUnicode=true&characterEncoding=UTF-8","gwt","root");
}catch(Exception ex){
}
String query = "SELECT * FROM TBL_Drive_Files WHERE id = '"+req.getParameter("fileid")+"'", filename="";
InputStream filecontent=null;
int filesize=0;
try {
select = con.createStatement();
result = select.executeQuery(query);
while (result.next()) {
filename = result.getString(2);
filesize = result.getInt(4);
filecontent = result.getBinaryStream(5);
}
result.close();
} catch(SQLException e) {
}
BufferedOutputStream output = null;
try {
resp.reset();
resp.setContentType("application/octet-stream");
resp.setContentLength(filesize);
resp.setHeader("Content-disposition", "attachment; filename=\"" + filename + "\"");
output = new BufferedOutputStream(resp.getOutputStream());
for(int data; (data=filecontent.read()) != -1;) {
output.write(data);
}
output.flush();
query = "Update TBL_Drive_Files SET count = count + 1 WHERE id = '"+req.getParameter("fileid")+"'";
try {
select = con.createStatement();
select.execute(query);
select.close();
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
catch (IOException e) {
e.printStackTrace();
}
finally {
}
}
}
and in client side:
Button bd = new Button("Download", new ClickHandler(){
public void onClick(ClickEvent event){
final String link = GWT.getModuleBaseURL() + "download?fileid=3";
RequestBuilder builder = new RequestBuilder(RequestBuilder.GET,link);
try {
builder.sendRequest(null, new RequestCallback() {
public void onError(Request request, Throwable t) {
Window.alert("Error bei getExcel");
}
public void onResponseReceived(Request request,Response response){
int statuscode = response.getStatusCode();
if(statuscode == 200) {
Window.Location.replace(link);
} else if(statuscode == 404) {
Window.alert("Service not available.");
}
}
});
} catch (RequestException re) {
Window.alert(re.toString());
}
}
});
RootPanel.get().add(bd);
You make two requests. One by calling builder.sendRequest(null, new RequestCallback()
and the other in the callback with status 200 Window.Location.replace(link)
Btw i hope this isnt a code that will be deployed.
There are many other points, but you should fix this at first and then u will be able to find errors and maintain the code by yourself
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With