使用两个不同的servlet处理两个不同的作业,但只有一个提交按钮

时间:2012-04-27 04:56:08

标签: java html database forms servlets

以下HTML代码段将文件提交给 UploadHandler ,这是一个servlet。然后还有一个标题框也需要处理。我可以处理 UploadHandler 中的标题框,然后打开与数据库的连接并在那里提交。但我不想这样做。让上传处理程序处理文件上传。那么替代方案是什么?如何将标题提交到表格中?我想在处理这两项工作时创造一种并行感。

<form method="post" action="UploadHandler" enctype="multipart/form-data">
        <table>

            <tr>
                <td> <strong> Browse photo to submit </strong> </td>
                <td> <input type="file" name="ImageToUpload" value="Upload Photo"/> </td>
            </tr>

            <tr>
                <td> <strong> Give a Caption to this photo </strong>  </td>
                <td> <input type="text" name="caption box" size="40" /></td>
            </tr>

            <tr colspan="2">
                <td> <input type="submit" value="submit photo"/> </td>
            </tr>

        </table>
    </form>

有什么方法当我点击提交2个不同的工作时,它们由2个不同的servlet处理?从 UploadaHandler 创建一个新线程似乎不太好。

@Luiggi Mendoza评论后:

处理文件上传的Servlet:

package projectcodes;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.File;
import java.util.List;
import java.util.Iterator;
import org.apache.commons.fileupload.*;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.io.FilenameUtils;

public class UploadHandler extends HttpServlet {
@Override
public void doPost(HttpServletRequest request,HttpServletResponse response) throws IOException,ServletException {
    response.setContentType("text/plain");
    String path = request.getParameter("ImageToUpload");
    PrintWriter writer = response.getWriter();
    try {
        Boolean isMultipart = ServletFileUpload.isMultipartContent(request);
        if(!isMultipart) {
            Boolean AttemptToUploadFile = true;
            RequestDispatcher rd = request.getRequestDispatcher("portfolio_one.jsp");
            request.setAttribute("UploadAttempt", AttemptToUploadFile);
            rd.forward(request, response);
        } else {
            DiskFileItemFactory diskFileItem = new DiskFileItemFactory();
            ServletFileUpload fileUpload = new ServletFileUpload(diskFileItem);
            List list = null;

            try {
                list = fileUpload.parseRequest(request);
            }catch(Exception exc) {
                Boolean AttemptToUploadFile = true;
                RequestDispatcher rd = request.getRequestDispatcher("portfolio_one.jsp");
                request.setAttribute("UploadAttempt", AttemptToUploadFile);
                rd.forward(request, response);
            }

            Iterator iterator = list.iterator();
            while(iterator.hasNext()) {
                String emailOfTheUser = null;
                FileItem fileItem = (FileItem)iterator.next();
                if(!fileItem.isFormField()) {
                    String fieldName = fileItem.getFieldName();
                    String fileName = FilenameUtils.getName(fileItem.getName());
                    HttpSession session = request.getSession();
                    if(!session.isNew()) {
                        emailOfTheUser = (String)session.getAttribute("Email");
                    }
                    File file = new File("/home/non-admin/project uploads/project users/" + emailOfTheUser ,fileName);
                    fileItem.write(file);
                    RequestDispatcher rd = request.getRequestDispatcher("portfolio_one.jsp");
                    String message = "File Uploaded successfully !";
                    request.setAttribute("SuccessMessage", message);
                    rd.forward(request, response);
                }
            }
        }
    }catch(Exception exc) {
        Boolean AttemptToUploadFile = true;
        RequestDispatcher rd = request.getRequestDispatcher("portfolio_one.jsp");
        request.setAttribute("UploadAttempt", AttemptToUploadFile);
        rd.forward(request, response);
    }
}

}

4 个答案:

答案 0 :(得分:2)

我想如果你从第一个servlet转发到第二个servlet,可以使用:

getServletContext().getRequestDispatcher("/2ndServlet").forward(req, res);

但这不是一个好主意,因为它可能会触发过滤器,响应可能已经被提交等等。

你应该做的是在helper类中提取第二个servlet的功能,并从第一个servlet中调用它,作为一个简单的java方法调用。

答案 1 :(得分:0)

尝试使用UploadHandler。它可能会有所帮助

RequestDispatcher rd = getServletContext().getRequestDispatcher("pathToSecondServlet");
rd.forward(req, res);

答案 2 :(得分:0)

在你的HTML中:

<input type="text" name="captionBox" size="40" />

然后在你的servlet代码中

String captionBox = request.getParameter("captionBox");
String anotherField = request.getParameter("anotherField");
//all your code

正如@Bozho所建议的那样,文件上传代码应该封装在另一个类方法中并调用它,也许你需要在另一个servlet上上传文件。请记住keep your code DRY

答案 3 :(得分:0)

我同意Bozho的观点。但是他建议你可以实现helper类并在第一个servlet中实现所有opertaion(业务逻辑)。它比RequestDispatcher好得多。如果您的应用程序使用其他按钮(提交除外),那么就可以。

Take your application with a simple diversion.