Excel File Servlet的創(chuàng)建代碼
1、File->New...->W(wǎng)eb->雙擊Standard Servlet圖標(biāo),啟動(dòng)創(chuàng)建標(biāo)準(zhǔn)Servlet的向?qū)А?/P>
指定Servlet類名為Excel File Servlet,將包名設(shè)為bookstore.servlet,按Next到下一步。
2、選擇覆蓋doGet()處理方法。
◆Servlet:creates content type:unspecified,設(shè)定Servlet的生成文檔的類型,由于這個(gè)Servlet作為一個(gè)Excel文件并以附件的形式下載,需要我們手工設(shè)定Servlet的響應(yīng)內(nèi)容類型。
◆implements methods:doGet(),這樣向?qū)⑸梢粋€(gè)doGet()方法框架。
按Next到下一步。
3、定義Servlet的URL參數(shù)。
點(diǎn)擊Add Parameter在參數(shù)列表出現(xiàn)一個(gè)新行,在新行中定義Servlet的URL參數(shù),其中Name為URL所帶的參數(shù)名,而Variable為 Servlet中對(duì)應(yīng)的變量名,此外還可以通過Desc和Default為變量指定注釋和默認(rèn)值,在Type欄中指定變量的類型。
我們定義了兩個(gè)URL參數(shù),分別是year和month,指定需要下載日志的年份和月份。按Next到下一步。
4.指定servlet的訪問路徑
接受第4步向?qū)O(shè)定的Servlet的名字和訪問路徑,它們分別是:
◆Name:Excel File Servlet
◆URL pattern:/Excel File Servlet
直接按Finish創(chuàng)建Excel File Servlet,其代碼如下所示:
代碼Excel File Servlet.java:
- package bookstore.servlet;
- import javax.servlet.*;
- import javax.servlet.http.*;
- import java.io.*;
- import java.util.*;
- public class ExcelFileServlet
- extends HttpServlet
- {
- //Initialize global variables
- public void init()
- throws ServletException
- {
- }
- //Process the HTTP Get request
- public void doGet(HttpServletRequest request,
HttpServletResponse response)- throws ServletException, IOException
- {
- //年份
- String year = request.getParameter("year");
- if (year == null)
- {
- year = "2005";
- }
- //月份
- String month = request.getParameter("month");
- if (month == null)
- {
- month = "1";
- }
- PrintWriter out = response.getWriter();
- //@todo implement GET
- }
- //Clean up resources
- public void destroy()
- {
- }
- }
【編輯推薦】