如何将参数传递给jsp页面

时间:2014-05-27 16:44:25

标签: jsp view

我有一个这样的表格:

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!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=UTF-8">
        <title>File Upload</title>
    </head>

    <body> 
        <div>
            <h3> Choose File to Upload in Server </h3>
            <form action="upload" method="post" enctype="multipart/form-data">
                <input type="file" name="file" />
                <input type="submit" value="upload" />
            </form>          
        </div>

    </body>
</html>
带有支持类的

,它读取上传的.csv文件:

public class FileUploadHandler extends HttpServlet 
{
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException 
    {

        //process only if its multipart content
        if(ServletFileUpload.isMultipartContent(request))
        {
            try 
            {
                List<FileItem> multiparts = new ServletFileUpload(
                                         new DiskFileItemFactory()).parseRequest(request);

                for(FileItem item : multiparts)
                {
                    if(!item.isFormField())
                    {
                        File tempFile = File.createTempFile(item.getName(), null);
                        List<String[]> valueStrings = readCSVFile(tempFile);
                        //the line I am having trouble with
                    }
                }

               //File uploaded successfully
            } 
            catch (Exception ex) 
            {
                //TODO: handle exception
            }          

        }
        else
        {
            request.setAttribute("message",
                                 "Sorry this Servlet only handles file upload request");
            request.getRequestDispatcher("/error.jsp").forward(request, response);
        }
    }
    ....
}

我想将数组values传递给另一个视图,看起来像这样:

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <body>
        <%
        for(String[] line : values)
        {
            for(String value : line)
            {
                System.out.print(value+" ");
            }
            System.out.print("<br/>");
        }
        %>
</body>
</html>

我该怎么做?

1 个答案:

答案 0 :(得分:0)

您有3个解决方案:

<强> 1。使用转发:

//我遇到问题

request.setAttribute(&#34; values&#34;,valueStrings);

request.getRequestDispatcher(&#34; /path_to_jsp.jsp")。forward(request,response)

<强> 2。如果重定向

,请使用HTTPSession
  • 在HttpSession中缓存valueStrings并执行重定向到jsp文件

第3。将valueStrings包含为查询参数

  • 对于此解决方案。您需要将valueStrings转换为String并将此String包含为查询参数。在其他jsp文件中,您需要将String转换回valueStrings。