从jsp到控制器一个接一个地调用2个表单

时间:2022-12-02 00:02:25

I have created two forms in my jsp. 1st one is an upload functionality and other is the submit page functionality. My requirement is to upload the file using upload functionality. and if upload is successful . The pass the file name back to jsp and on submit button pass the file name along with other details to other page.

我在jsp中创建了两个表单。第一个是上传功能,另一个是提交页面功能。我的要求是使用上传功能上传文件。如果上传成功。将文件名传递回jsp并在提交按钮上将文件名和其他详细信息一起传递给其他页面。

My code: MyJsp.jsp

我的代码:MyJsp.jsp

    </tr>
    <tr>
    <td colspan="2" rowspan="2" align="center">                         <form action="UploadDownloadFileServlet" method="post"
                                            enctype="multipart/form-data" class="CSSTableGenerator">
                                            Select the Raw Data Sheet of Customer : <input type="file"
                                                name="fileName" class="button"> <input type="submit" value="Upload"
                                                class="button">
        </form>                         

    <form action="DataController" method="post" >
                                            <input type="submit" name="listUser" value="Confirm Selection"
                                                class="button" align="middle">
        </form>
        </td>
    </tr>
   </table>

My Controller (Servlet):

我的控制器(Servlet):

UploadDownloadFileServlet.java

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    if(!ServletFileUpload.isMultipartContent(request)){
        throw new ServletException("Content type is not multipart/form-data");
    }

    response.setContentType("text/html");
    PrintWriter out = response.getWriter();
    out.write("<html><head></head><body>");
    try {
        List<FileItem> fileItemsList = uploader.parseRequest(request);
        Iterator<FileItem> fileItemsIterator = fileItemsList.iterator();
        while(fileItemsIterator.hasNext()){
            FileItem fileItem = fileItemsIterator.next();
            System.out.println("FieldName="+fileItem.getFieldName());
            System.out.println("FileName="+fileItem.getName());
            System.out.println("ContentType="+fileItem.getContentType());
            System.out.println("Size in bytes="+fileItem.getSize());
            String fileName = fileItem.getName().substring(fileItem.getName().lastIndexOf("\\") + 1);
            System.out.println("FILE NAME>>>>"+fileName);
            File file = new File(request.getServletContext().getAttribute("FILES_DIR")+File.separator+fileName);
            System.out.println("Absolute Path at server="+file.getAbsolutePath());
            fileItem.write(file);
            HttpSession session = request.getSession();
            session.setAttribute("Success", "Success");
            getServletContext().getRequestDispatcher("/Welcome.jsp").forward(request, response);
            /*out.write("File "+fileName+ " uploaded successfully.");
            out.write("<br>");
            out.write("<a href=\"UploadDownloadFileServlet?fileName="+fileItem.getName()+"\">Download "+fileName+"</a>");*/
        }
    } catch (FileUploadException e) {
        out.write("Exception in uploading file.");
        HttpSession session = request.getSession();
        session.setAttribute("Failed", "Failed");
        getServletContext().getRequestDispatcher("/Welcome.jsp").forward(request, response);
    } catch (Exception e) {
        out.write("Exception in uploading file.");
        HttpSession session = request.getSession();
        session.setAttribute("Failed", "Failed");
        getServletContext().getRequestDispatcher("/Welcome.jsp").forward(request, response);
    }
    out.write("</body></html>");/**/
}

}

My Next Contoller for submit button which needs value:

我需要提交按钮的下一个控制器按钮:

protected void doPost(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {

        String upload = (String)request.getAttribute("Success");
        if(upload.equalsIgnoreCase("Success")){


        System.out.println("SERVLET DOPOST");
        String action = (String) request.getAttribute("DownLoadToExcel");
        System.out.println(action);
        String[] kpi = request.getParameterValues("kpi");

how is it possible in jsp to know that upload was successful and submit should go forward else give an error.

怎么可能在jsp中知道上传成功并且提交应该继续其他给出错误。

Awaiting reply.

Thanks, MS

1 个解决方案

#1


0  

First, after UploadDownloadFileServlet successfully receives and process the upload, you should make it go back to the JSP. You need to "redirect it" to "MyJsp.jsp".

首先,在UploadDownloadFileServlet成功接收并处理上载之后,您应该返回JSP。您需要将其“重定向”到“MyJsp.jsp”。

HttpServletResponse.sendRedirect("MyJsp.jsp?fileName2="+fileName);
//- you can also call sendRedirect from a PrintWriter -

Then, in the 2nd form in the JSP you could use something (javascript, scriptlets, a tag library, a custom tag, etc) to detect the parameter "fileName2" and set a hidden input with the name of the file.

然后,在JSP的第二种形式中,您可以使用某些东西(javascript,scriptlet,标记库,自定义标记等)来检测参数“fileName2”并使用文件名设置隐藏输入。

Keep in mind you don't sendRedirect() and forward() for the same response.

请记住,您不会为同一响应发送reddirect()和forward()。

You can pass as many parameters as you want together with the file name.

您可以根据需要传递任意数量的参数和文件名。

#1


0  

First, after UploadDownloadFileServlet successfully receives and process the upload, you should make it go back to the JSP. You need to "redirect it" to "MyJsp.jsp".

首先,在UploadDownloadFileServlet成功接收并处理上载之后,您应该返回JSP。您需要将其“重定向”到“MyJsp.jsp”。

HttpServletResponse.sendRedirect("MyJsp.jsp?fileName2="+fileName);
//- you can also call sendRedirect from a PrintWriter -

Then, in the 2nd form in the JSP you could use something (javascript, scriptlets, a tag library, a custom tag, etc) to detect the parameter "fileName2" and set a hidden input with the name of the file.

然后,在JSP的第二种形式中,您可以使用某些东西(javascript,scriptlet,标记库,自定义标记等)来检测参数“fileName2”并使用文件名设置隐藏输入。

Keep in mind you don't sendRedirect() and forward() for the same response.

请记住,您不会为同一响应发送reddirect()和forward()。

You can pass as many parameters as you want together with the file name.

您可以根据需要传递任意数量的参数和文件名。