1

I have few text boxes and 4 to 5 Buttons in my jsp page, based on the button click and data in the form i need to perform operations.

But I am not able to use request.getParameter() in my servlet

My JSP code :

    <form action="UsageEngine" method="post" target="console" enctype="multipart/form-data">
        <input class="form-control" type="file" multiple name="filepath"    
            required>
<br> <input class="form-control" type="text"
            name="Grepfilename" placeholder="Command Line.."><br>
        <!-- <input class="form-control" type="text" name="FileToBeDownloaded" placeholder="File to be Downloaded"><br> -->
        <select class="form-control" name="env">
            <option>pinDap75a</option>
            <option>pinIap04a</option>
            <option>pinDap71a</option>
        </select><br>
        <button class="btn btn-danger" name="action" id="Irel"
            value="test" onclick="show()">Env Check</button>
        <button class="btn btn-success" name="action" value="process"
            onclick="show()">Process SFTP</button>
        <button class="btn btn-warning" name="action" id="grep" value="Grep"
            onclick="show()">Grep</button>
        <button class="btn btn-primary" name="action" id="Download"
            value="Download" onclick="show()">Init Irel</button>
        <button class="btn btn-info" name="action" id="Irel"
            value="completeIrel" onclick="show()">Complete Irel</button>
</form>

File upload is working fine. But I need to pass filename , environment details, and value of the button as well. For all these getParameter was very helpfull, but due to Multipart/form-data I am not able to use that.

upload.parseRequest(request); helps in listing down all the fields but I cannot use it for my application.

For my application it is like IF 1st button clicked{ do this function } , if s2nd Button{ do another action} like that.

Every Button tag has same name "Action" but with different value, so I read the value from the action and perform my function calls with if else conditions

COde :

 if (Action.equalsIgnoreCase("Grep")) {
            String Filenames = request.getParameter("Grepfilename");
            String[] GrepFileName = Filenames.split(",");
            try {
                for (int i = 0; i < GrepFileName.length; i++) {

                    GrepOutput.addAll(ExecEngine.ExecuteGrep(GrepFileName[i],
                            env));

                }

            } catch (JSchException e) {
                e.printStackTrace();
            } catch (SftpException e) {
                e.printStackTrace();
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (ArrayIndexOutOfBoundsException e) {
                e.printStackTrace();
            }

            request.setAttribute("consoleOutputForSFTP", GrepOutput);
            request.getRequestDispatcher("/consoleOutput.jsp").forward(request,
                    response);
        } else if (Action.equalsIgnoreCase("Download")) {
            try {
                consoleOutputForSFTP = ExecEngine.deployIrelWrapper(env);
                request.setAttribute("consoleOutputForSFTP",
                        consoleOutputForSFTP);
                request.getRequestDispatcher("/consoleOutput.jsp").forward(
                        request, response);
            } catch (JSchException | InterruptedException e) {
                e.printStackTrace();
            }
        } else if (Action.equalsIgnoreCase("completeIrel")) {
            String GrepFileName = request.getParameter("Grepfilename");
            try {
                consoleOutputForSFTP = ExecEngine.deployFinalIrelWrapper(
                        GrepFileName, env);
                request.setAttribute("consoleOutputForSFTP",
                        consoleOutputForSFTP);
                request.getRequestDispatcher("/consoleOutput.jsp").forward(
                        request, response);
            } catch (JSchException | InterruptedException e) {
                e.printStackTrace();
            }
T Kanagaraj
  • 245
  • 5
  • 20

2 Answers2

2

Assuming that you are using Apache Commons FileUpload to process the multipart data, you get a List<FileItem> from upload.parseRequest(request). However, FileItem has a method isFormField() to check if the field (use getFieldName() to retrieve its name) is a form field or not.

So, you have two options:

  1. Iterate over the List<FileItem> until you found the appropriate paramter and its value.
  2. Instead of using parseRequest(request) you could use parseParameterMap(request) with will return a Map<String,List<FileItem>> where the key should be the request parameter name from you html form. You then only need to take the value from the first element of the value's item.

Edit: Assuming that you can be parsing the multipart like this:

ServletFileUpload upload = new ServletFileUpload();
Map<String,List<FileItem>> paramMap = upload.parseParameterMap(request);

Then you can have a helper method like this which does the same as a request.getParameter(name), passing the paramMap as a parameter:

public String getParameterFromMap(String paramName, Map<String,List<FileItem>> map) {
    if ((paramName == null) || (map == null) || (map.isEmpty() == true)) {
        return null;
    }
    List<FileItem> items = map.get(paramName);
    if ((items == null) || (items.isEmpty() == true)) {
        return null;
    }
    FileItem firstItem = items.get(0);
    if ((firstItem == null) || (firstItem.isFormField() == false)) {
        return null;
    }
    return firstItem.getString();
}
jCoder
  • 2,289
  • 23
  • 22
  • As I have 4 buttons, i need to perform different actions based on which button is clicked For ex : IF 1st button is clicked I need to upload file and do SFTP for which I need to pass environment details and file name. Iterator fetches all the parameter values – T Kanagaraj May 26 '15 at 10:56
  • The parameter named "action" will contain the "value" of the button which was clicked. Even though the form contains 4 buttons named "action" only the value of the clicked one is transferred in the request. So you can just compare its value with your action and do one thing or the other. – jCoder May 26 '15 at 11:19
  • Yes to do that i need to use request.getParameter() which I am not able to use – T Kanagaraj May 26 '15 at 12:44
  • I've updated my answer to provide a helper method which does the same as the `request.getParameter(name)` when using a parsed map. – jCoder May 26 '15 at 13:16
0

In that case you can take a hidden variable to set the value of button clicked by javascript, and can fetch that hidden parameter value on servlet by item.getString() value.

Chetan Verma
  • 735
  • 5
  • 10