简体   繁体   中英

reading the form data in servlet. data posted with post method and servlet called with ?q=test1

Hey I am trying to read the form data in a servlet sent with post method. And the servlet is called as OnlineExam?q=saveQuestion . Now the servlet is working as:

public class OnlineExam extends HttpServlet {
protected void doGet(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
if(request.getParameter("q").equals("saveQuestion")){
                /*
                 * Save the question provided with the form as well as save the uploaded file if any.
                 */
                saveQuestion(request);
            }
}

protected void doPost(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
//      doGet(request, response);
        saveQuestion(request);
    }
public String saveQuestion(HttpServletRequest request){         
        System.out.println(request.getParameter("question"));

        return "";

    }       
}

HTML form:

<form action="OnlineExam?q=saveQuestion" method="post">
        <fieldset>
        <legend>Question</legend>
        <textarea class="questionArea" id="question" name="question">Enter Question.</textarea>
        <br class="clearFormatting"/>               
        <input class="optionsInput" value="optionA" name="optionA" onfocus = "clearValues('optionA')" onblur = "setValues('optionA')"/>
        <br class="clearFormatting"/>

        <input class="optionsInput" value="optionB" name="optionB" onfocus = "clearValues('optionB')" onblur = "setValues('optionB')"/>
        <br class="clearFormatting"/>

        <input class="optionsInput" value="optionC" name="optionC" onfocus = "clearValues('optionC')" onblur = "setValues('optionC')"/>
        <br class="clearFormatting"/>

        <input class="optionsInput" value="optionD" name="optionD" onfocus = "clearValues('optionD')" onblur = "setValues('optionD')"/>
        <br/>
        <input class="optionsInput" value="answer" name="answer" onfocus="clearValues('answer')" onblur="setValues('answer')"/>     
        <input type="submit" value="Save" />
        <input type="reset" value="Cancel" />
        <button style="display: none" onclick="return deleteQuestion()" >Delete</button>
        </fieldset>
        </form>

So can anyone illustrate how the servlet is actually called. I mean what is the flow of control ie how the things works in this servlet.

And how could i read the param1 there in servlet.

ps: i don't want to post form with get method.

You should get the value of q in your doPost not in your doGet . Because you use method="post" then in the servlet the doPost is the one that called not the doGet . Remove the code in your doGet then insert it to doPost . And you doPost must be something like below code.

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    if(request.getParameter("q").equals("saveQuestion")){
        saveQuestion(request);
    }
}

if you POST data to servlet. doPost will get invoked.

Inside doPost() you can access request param like

request.getParameter("param1");

Is this solved?

I am facing same problem. I tried

Enumeration paramNames = request.getParameterNames();
while(paramNames.hasMoreElements()) {
    System.out.println((String)paramNames.nextElement());
}

and it's showing 0 elements, so form data is not being read by servlet.

I got the answer in other thread. enctype=multipart/form-data was causing this. After removing it from form, was able to read data.

doPost() {
  processRequest(request, response);
  //to do
}

Remove/comment processRequest(request, response) and try it again. Now you should not get null values.

I know this is an old thread but could not find an answer to this when I searched so I am posting my solution for someone who may have the same issue with getting null from form parameters in the dopost function of their servlet.

I had a similar issue getting null values when using the request.getParameters("param1"); functions. After hours of playing around with it I realized that the param1 that I was using was the ID for the input tag I was requesting. That was wrong. I had to use the NAME attribute of the input tag to get the correct value of the input box. That was all it was. I just had to add a name and get the parameter using this name and that fixed the issue.

Hope this helps someone.

When you click the "submit" button on your form, the doPost method of your servlet will be called - this is dictated by the method that you put on the "form" in your HTML page. The URL parameters ( q=saveQuestion ) will still be available to your code in the doPost method. You seem to be under the impression that the URL parameters will be processed by the doGet method and the form parameters by the doPost method. That is not the case.

Vinit try the below code request.getParameter("param1");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM