简体   繁体   English

从jsp到控制器依次调用2种形式

[英]calling 2 forms from a jsp to a controller one after another

I have created two forms in my jsp. 我在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,然后单击“提交”按钮,将文件名以及其他详细信息传递给其他页面。

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 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: 我的下一个提交的Contoller按钮需要值:

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 谢谢,女士

First, after UploadDownloadFileServlet successfully receives and process the upload, you should make it go back to the JSP. 首先,在UploadDownloadFileServlet成功接收并处理了上载之后,应该使它返回到JSP。 You need to "redirect it" to "MyJsp.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. 请记住,您不会为相同的响应发送sendRedirect()forward()

You can pass as many parameters as you want together with the file name. 您可以根据需要传递任意数量的参数以及文件名。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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