Click here to Skip to main content
15,890,995 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
there are two buttons in my jsp page

1.
XML
<tr>
                 <td>
                     Album Name
                 </td>
                 <td>
                     <input type="text" name="Album" placeholder="Album name"/>
                 </td>
             </tr>
             <tr>
                 <td>

                 </td>
                 <td>
                    <input type="submit" name="adddir" value="submit">
                 </td>
             </tr>


2. to upload
XML
<tr>
                   <td>Upload audio: </td>
                   <td><input type="file" name="dataFile" id="fileChooser"/></td>
               </tr>
               <tr>
                   <td></td>
                   <td>
                       <input type="submit" value="Upload" name="upload" />
                   </td>
               </tr>




but only one button work at a time
<form action="uploadservlet" method="post" enctype="multipart/form-data">

servlet code to make a directory which works fine if i write it in

C#
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
           throws ServletException, IOException {
       response.setContentType("text/html;charset=UTF-8");
         try (PrintWriter out = response.getWriter()) {
      String newfolder = request.getParameter("Album");
           if(request.getParameter("adddir") !=  null)
           {
               File dir = new File("C:/Users/Documents/NetBeansProjects/Registrationform/web/nameoffolder/shreya/" + newfolder);
               dir.mkdir();
               response.sendRedirect("mypage1.jsp");
           }
         }



and for uploading

XML
protected void doPost(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    response.setContentType("text/html");
    PrintWriter out = response.getWriter();
    HttpSession sess = request.getSession();
    String foldername = sess.getAttribute("uname").toString();
    webTempPath = "C:\\Users\\Documents\\NetBeansProjects\\Registrationform\\web\\nameoffolder" +"\\"+ foldername;
    try {
    //    if(request.getParameter("upload") != null)
        {
        MultipartRequest mpr = new MultipartRequest(request, webTempPath, 10 * 1024 * 1024);
        response.setContentType("text/html");
        out.println("<html>");
        out.println("<head>");
        out.println("<title>Server upload</title>");
        out.println("</head>");
        out.println("<body>");
        response.sendRedirect("mypage1.jsp");
        out.println("</body>");
        out.println("</html>");
        }
    }
    catch (Exception e) {
        out.println(e);
        response.sendRedirect("Error.jsp");
   //     request.getRequestDispatcher("/Error.jsp").forward(request, response);
        e.printStackTrace();
    }




here only one button works at a time. if i write make a directory code in httprequest then it works fine. and the other uploading does not work.if i don't write "make a driectory code" then uploading works fine. But i want user to use both the buttons as per their requirement.
Posted
Updated 22-Oct-15 22:40pm
v3
Comments
Sergey Alexandrovich Kryukov 23-Oct-15 2:38am    
And the question is..?
It's a bad idea to handle exceptions so locally. Let me ask you: aren't you handing exceptions in all functions?
—SA
Member 9671810 23-Oct-15 3:10am    
No. but i want to make an error page for dilaying all exceptions, but why isn't this code working? can you figure out cause there are two submit buttons.

1 solution

When you click any of the buttons the servlet will call the doPost or doGet method specified by you in the form.

HTML
<form method="POST">
...
</form>

<form method="GET">
...
</form>


As the two buttons have the same type submit and I suppose that are into the form, any of your action will call any of that methods. That is the reason why you must implement the two options inside any of those methods, I mean validate when you press "adddir" or "upload" button. Hope this help.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900