在这节里,我们对welcome.jsp页面进行改造,在页面中添加一个链接,这个链接调用servlet下载上节所记录的系统日志文件。
通过向导创建excelfileservlet
1、file->new...->web->双击standard servlet图标,启动创建标准servlet的向导。
指定servlet类名为excelfileservlet,将包名设为bookstore.servlet,按next到下一步。
2、选择覆盖doget()处理方法。
图 11 覆盖servlet方法 |
·servlet:creates content type:unspecified,设定servlet的生成文档的类型,由于这个servlet作为一个excel文件并以附件的形式下载,需要我们手工设定servlet的响应内容类型。
·implements methods:doget(),这样向导将生成一个doget()方法框架。
按next到下一步。
3、定义servlet的url参数。
图 12 定义servlet的url参数 |
1. package bookstore.servlet; 2. import javax.servlet.*; 3. import javax.servlet.http.*; 4. import java.io.*; 5. import java.util.*; 6. 7. public class excelfileservlet 8. extends httpservlet 9. { 10. //initialize global variables 11. public void init() 12. throws servletexception 13. { 14. } 15. 16. //process the http get request 17. public void doget(httpservletrequest request, httpservletresponse response) 18. throws servletexception, ioexception 19. { 20. //年份 21. string year = request.getparameter("year"); 22. if (year == null) 23. { 24. year = "2005"; 25. } 26. 27. //月份 28. string month = request.getparameter("month"); 29. if (month == null) 30. { 31. month = "1"; 32. } 33. printwriter out = response.getwriter(); 34. //@todo implement get 35. } 36. 37. //clean up resources 38. public void destroy() 39. { 40. } 41. } |
1. <web-app> 2. … 3. <servlet> 4. <servlet-name>excelfileservlet</servlet-name> 5. <servlet-class>bookstore.servlet.excelfileservlet</servlet-class> 6. </servlet> 7. <servlet-mapping> 8. <servlet-name>excelfileservlet</servlet-name> 9. <url-pattern>/excelfileservlet</url-pattern> 10. </servlet-mapping> 11. … 12. </web-app> |
1. package bookstore.servlet; 2. import javax.servlet.*; 3. import javax.servlet.http.*; 4. import java.io.*; 5. 6. public class excelfileservlet 7. extends httpservlet 8. { 9. … 10. public void doget(httpservletrequest request, httpservletresponse response) 11. throws servletexception, ioexception 12. { 13. //年份 14. int year; 15. try 16. { 17. year = integer.parseint(request.getparameter("year")); 18. } catch (numberformatexception e) 19. { 20. year = 2005; 21. } 22. 23. //月份 24. int month; 25. try 26. { 27. month = integer.parseint(request.getparameter("month")); 28. } catch (numberformatexception e) 29. { 30. month = 1; 31. } 32. string filename = "log_" + year + "_" + month +".xls"; 33. file file = new file("d://serverlog//"+filename); 34. response.setcontenttype("application/x-msdownload"); 35. response.setcontentlength( (int) file.length()); 36. response.setheader("content-disposition", "attachment;filename="+filename); 37. 38. fileinputstream fis = new fileinputstream(file); 39. bufferedinputstream fbis = new bufferedinputstream(fis); 40. byte abyte0[] = new byte[1024]; 41. int k = 0; 42. outputstream out = response.getoutputstream(); 43. while ( (long) k < file.length()) 44. { 45. int j = fbis.read(abyte0, 0, 1024); 46. k += j; 47. out.write(abyte0, 0, j); 48. } 49. out.flush(); 50. } 51. … 52. } |
1. <%@page contenttype="text/html; charset=gbk"%> 2. <%@taglib uri="http://jakarta.apache.org/taglibs/datetime-1.0" prefix="dt"%> 3. … 4. 现在的时间是<dt:format pattern="mm/dd/yyyy hh:mm"><dt:currenttime/></dt:format> 5. <br><a href="/webmodule/excelfileservlet?year=2005&month=1">下载系统启动日志</a> 6. <br>点击<a href="quit.jsp">这里</a>退出系统 7. </body> 8. </html> |
图 13 附件下载日志链接的welcome.jsp页面 |
图 14 下载系统日志文件对话框 |
新闻热点
疑难解答