The following HTML snippet submits the file to UploadHandler which is a servlet. Then there is a caption box that also needs to be handled. I can handle the caption box in UploadHandler and then open the connection with the database and submit it there. But i don’t want to do this . Let upload handler handle uploading of files. Then what is the alternative ? How do i submit the caption into the table ? I want to create a sense of parallelism in handling these two jobs.
<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>
Is there any way that when i click to submit 2 different jobs they are handled by 2 different servlets ? Creating a new thread from UploadaHandler doesn’t seem a good idea.
After the comment by @Luiggi Mendoza :
Servlet that handles Uploading of files :
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);
}
}
}
I guess you can if you forward from the first to the 2nd servlet, using:
But this is not a good idea, as it might trigger filter, the response may already by committed, etc.
What you should do is extract the functionality of the 2nd servlet in a helper class, and just invoke it, as a simple java method invocation, from the 1st servlet.