简体   繁体   中英

Couldn't get the values from jsp to servlet

If I run jsp, while exporting the contents to excel, I am not getting the values in downloaded excel file. It is simply empty. Here what I tried..

How to pass the table values to servlet?

Excel.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
 <%@ page import ="javax.swing.JOptionPane"%>   
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Export to Excel - Demo</title>
</head>
<body>

    <table align="left" border="2">
        <thead>
            <tr bgcolor="lightgreen">
                <th>Sr. No.</th>
                <th>Text Data</th>
                <th>Number Data</th>
            </tr>
        </thead>
        <tbody>
            <%
                for (int i = 0; i < 10; i++) {
            %>
            <tr bgcolor="lightblue">
                <td align="center"><%=i + 1%></td>
                <td align="center">This is text data <%=i%></td>
                <td align="center"><%=i * i%></td>
            </tr>
            <%
                }
            %>
        </tbody>
    </table>

    <p>
    fsfndfkdsfdkfjsfksfskfsfskfsfksfskf
    dfkdjfkfksfkkkkkkkkkjjjjjjjjj
    </p>

    <a href="Sample?exportToExcel=YES">Export to Excel</a>

</body>
</html>

Sample.java (Servlet)

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class Sample
 */
public class Sample extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public Sample() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub


        String exportToExcel = request.getParameter("exportToExcel");

        if (exportToExcel != null
                && exportToExcel.toString().equalsIgnoreCase("YES")) {
            response.setContentType("application/vnd.ms-excel");
            response.setHeader("Content-Disposition", "inline; filename="
                    + "excel.xls");

        }  
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub


    }

}

Try this:

<%@ page language="java" contentType="application/vnd.ms-excel"
pageEncoding="ISO-8859-1"%>
 <%@ page import ="javax.swing.JOptionPane"%>   
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"       "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Export to Excel - Demo</title>
</head>
<body>

<table align="left" border="2">
    <thead>
        <tr bgcolor="lightgreen">
            <th>Sr. No.</th>
            <th>Text Data</th>
            <th>Number Data</th>
        </tr>
    </thead>
    <tbody>
        <%
            for (int i = 0; i < 10; i++) {
        %>
        <tr bgcolor="lightblue">
            <td align="center"><%=i + 1%></td>
            <td align="center">This is text data <%=i%></td>
            <td align="center"><%=i * i%></td>
        </tr>
        <%
            }
        %>
    </tbody>
</table>

<p>
fsfndfkdsfdkfjsfksfskfsfskfsfksfskf
dfkdjfkfksfkkkkkkkkkjjjjjjjjj
</p>

<a href="Sample?exportToExcel=YES">Export to Excel</a>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM