Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 i.e. 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.

like image 791
vinit Avatar asked May 03 '11 11:05

vinit


1 Answers

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);
    }
}
like image 68
ace Avatar answered Nov 08 '22 08:11

ace