JBuilder2005 servlet Development Download Type

Source: Internet
Author: User
servlet| Download

In this section, we retrofit the welcome.jsp page and add a link to the page that calls the servlet to download the system log file that is logged in the previous section.

  Creating Excelfileservlet with wizards

1. file->new...->web-> Double-click the standard servlet icon to start the wizard to create the standard servlet.

Specifies that the servlet class is named Excelfileservlet, sets the package name to Bookstore.servlet, and presses next to the next step.

2, choose the cover doget () processing method.


Figure 11 Overriding the Servlet method


· Servlet:creates content type:unspecified, set the type of the servlet's build document, as the servlet is downloaded as an Excel file and as an attachment, We need to manually set the response content type of the servlet.

implements Methods:doget (), so the wizard will generate a Doget () method framework.
Press next to the next step.

3, define the URL parameters for the servlet.


Figure 12 Defining the URL parameters for the servlet

Click Add parameter a new line appears in the parameter list, define the URL parameter for the servlet in the new row, where name is the name of the parameter with the URL, and variable is the corresponding variable name in the servlet. You can also specify comments and defaults for variables through desc and default, and specify the type of the variable in the Type column.

We have defined two URL parameters, year and month, which specify the years and months in which the log needs to be downloaded. Press next to the next step.

4. Specify the access path for the servlet

Accept the name and access path of the servlet set by the 4th Wizard, respectively:

· Name:excelfileservlet

· URL Pattern:/excelfileservlet

Create Excelfileservlet directly by finish, and the code looks like this:

Code Listing 8 Excelfileservlet.java

1. Package bookstore.servlet;
2. Importjavax. servlet.*;
3. Import Javax.servlet.http.*;
4. Importjava.io.*;
5. Import java.util.*;
6.
7. public class Excelfileservlet
8. Extends HttpServlet
9. {
//initialize Global Variables
One. public void init ()
Throws Servletexception
13. {
14.}
15.
//process the HTTP GET request
public void doget (HttpServletRequest request, httpservletresponse response)
Throws Servletexception, IOException
19. {
20.//year
String year = Request.getparameter ("year");
if (year = = null)
23. {
Year = "2005";
25.}
26.
27.//month
String month = Request.getparameter ("month");
if (month = null)
30. {
month = "1";
32.}
PrintWriter out = Response.getwriter ();
Implement//@todo get
.}

36.
//clean up
public void Destroy ()
39. {
40.}
41.}

The Doget () method on line 17th to 35th is the body part of the Excelfileservlet, where 20th to 32nd is the code that gets the URL parameter.

The deployment description information corresponding to this servlet will be generated in Web.xml, as follows:

Code Listing 9 Excelfileservlet Deployment Description configuration information

1. The
2...
3. The
4. Excelfileservlet
5. Bookstore.servlet.ExcelFileServlet
6. The
7. The
8. Excelfileservlet
9. /excelfileservlet
-

11...
A.

   Download log file code

In this section, we need to change the Doget () method of the servlet, specify the response format and read the corresponding log file contents from the log directory to the servlet's output stream.

Because the contents of the file are output in binary streams, the Servlet Wizard generates code in code listing 8, line 33rd:

PrintWriter out = Response.getwriter (); Is superfluous and we remove it. Add the following bold code:

Code Listing 10 download log file code

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...
public void doget (HttpServletRequest request, httpservletresponse response)
Throws Servletexception, IOException.
12. {
13.//year
year int;
Try
16. {
Year = Integer.parseint (Request.getparameter ("Year"));
A catch (NumberFormatException e)
19. {
Year of year = 2005;
21.}
22.
23.//month
-month int;
Try
26. {
month = Integer.parseint (Request.getparameter ("month"));
catch (NumberFormatException e)
29. {
month = 1;
31.}
String fileName = "Log_" + year + "_" + month + ". xls";
File File = new file ("d:\\serverlog\\" +filename);
Response.setcontenttype ("Application/x-msdownload");
Response.setcontentlength ((int) file.length ());
Response.setheader ("Content-disposition", "attachment;filename=" +filename);
37.
FileInputStream fis = new FileInputStream (file);
Bufferedinputstream fbis = new Bufferedinputstream (FIS);
-byte abyte0[] = new byte[1024];
A. int k = 0;
OutputStream out = Response.getoutputstream ();
A. while ((long) k file.length ())
44. {
-Int J = fbis.read (abyte0, 0, 1024);
K + J;
Out.write (abyte0, 0, J);
48.}
Out.flush ();

50.}
51...
52.}

Line 32nd through the URL parameter's worth to the log file name, the 34~36 row specifies the response header information so that the client downloads the log file in the form of a pop-up dialog box, 38~49 the contents of the log file into the response output stream.

   Transform welcome.jsp

Now that the servlet has been developed to download the log files, we need to add a link to the welcome.jsp page to access the Excelfileservlet.

Add the following bold code in Welcome.jsp, as follows:

Code Listing 11 Add welcome.jsp after link to download log

1. <% @page contenttype= "text/html; CHARSET=GBK "%>
2. <% @taglib uri= "http://jakarta.apache.org/taglibs/datetime-1.0" prefix= "DT"%>
3...
4. Now the time is
5.
download system startup log
6.
Click here exit system
7. The
8. The

For simplicity, the URL parameter for the year and month uses a fixed value, which, in practical applications, may come from the data component of the Web form.

Start the Web application, after the correct login, go to the welcome.jsp page, the page effect looks like this:


Figure 13 Attachment Download Log link welcome.jsp page

When you click on the download system log, a file download dialog box pops up, as shown in the following image:


Figure 14 Downloading the System Log file dialog box

Click the Save button to download the log file from the Web application server to your local hard disk.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.