简体   繁体   中英

calling 2 forms from a jsp to a controller one after another

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.

My code: 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):

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.

Awaiting reply.

Thanks, MS

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".

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.

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

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

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