gpt4 book ai didi

java - 使用两个不同的 servlet 但使用单个提交按钮处理两个不同的作业

转载 作者:行者123 更新时间:2023-12-02 07:49:51 25 4
gpt4 key购买 nike

以下 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);
}
}

}

最佳答案

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

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

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

您应该做的是在辅助类中提取第二个 servlet 的功能,然后从第一个 servlet 中将其作为简单的 java 方法调用来调用。

关于java - 使用两个不同的 servlet 但使用单个提交按钮处理两个不同的作业,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10344941/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com