PepeHands
PepeHands

Reputation: 1406

retrieving values from form post action

Suppose I have some form like this

<form action="/add_scores" method="post" enctype="multipart/form-data" id="add_link_form">
    <input type="text" name="subject" id="subject" placeholder="subject">
    <input type="text" name="link" id="link">
    <button type="submit" class="btn btn-success">Add</button>
</form>

And in my servlet

@Override
public void doPost(HttpServletRequest req, HttpServletResponse res)
        throws ServletException, IOException {
    String subject = (String)req.getAttribute("subject");
    String link = (String)req.getAttribute("link");
}

subject and link are always null when I post something. But if I use method="get" in form and rename doPost to doGet this code works fine and subject and link are good. (Also this happens if I change getAttribute() to getParameter()).

Why is this happening and how can I get my values in doPost?

Upvotes: 2

Views: 454

Answers (2)

varun
varun

Reputation: 4650

Try this:

@Override
public void doPost(HttpServletRequest req, HttpServletResponse res)
        throws ServletException, IOException {
    String subject = req.getParameter("subject");
    String link = req.getParameter("link");
}

Upvotes: 0

Alex Martelli
Alex Martelli

Reputation: 881635

As per Difference between getAttribute() and getParameter() , you should use getParameter, not getAttribute; and as per http://www.w3.org/TR/html401/interact/forms.html, your form should use enctype="application/x-www-form-urlencoded" -- the multipart encoding type is for forms used to upload files. BTW, neither of the two issues is at all dependent on whether you're using App Engine or other web servers.

Upvotes: 1

Related Questions