2

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?

PepeHands
  • 1,368
  • 5
  • 20
  • 36
  • @Pshemo I'm using Google App Engine – PepeHands Dec 17 '15 at 19:02
  • Honestly I am new to JavaEE and I never used that platform. But just to make sure I understand your problem correctly, you claim that `doGet` with `getAttribute` works fine, but `doPost` with `getAttribute` is not and you are asking why is that? (anyway proper method to read `` *parameters* is by using `getParameter` instead of `getAttribute` in both `do...` methods). – Pshemo Dec 17 '15 at 19:08
  • @Pshemo yes. And I used both `getParameter` and `getAttribute` in both `do` methods. It works in `doGet` but not in `doPost` – PepeHands Dec 17 '15 at 19:26
  • After testing your code it looks like problem may be related to `enctype="multipart/form-data"`. I am not web developer so my knowledge here is very limited but when I removed this information from `form` I was able to use `getParameter` method to read all data sent with `post`. I tested it on Tomcat so my tests may results may be different than yours. And like I said I am not web dev so removing `enctype="multipart/form-data"` may not be best solution (maybe there are other/better options - maybe changing charsets - so lets wait to see what others will say) – Pshemo Dec 17 '15 at 19:55
  • @Pshemo in my case this solution did not help too. Anyway, thanks for trying. I think I just will keep it `get`. – PepeHands Dec 17 '15 at 20:04

2 Answers2

1

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.

Community
  • 1
  • 1
Alex Martelli
  • 854,459
  • 170
  • 1,222
  • 1,395
  • Sorry for stupid question (I don't have much experience in real world web developing) but isn't `application/x-www-form-urlencoded` default enctype? If yes then removing `enctype` as suggested in my comments earlier should correct that problem, but OP claims that he still can't receive proper data even using `getProperty`. – Pshemo Dec 17 '15 at 23:46
  • @Pshemo, yes that is indeed the default for enctype (as per the w3.org doc I pointed to) -- I just think that "explicit is better than implicit". I am unable to reproduct the specific symptoms reported by the OP so I imagine that, as in 99.4% of such cases, he has not in fact tried all combinations (getParameter and the proper enctype). – Alex Martelli Dec 17 '15 at 23:51
0

Try this:

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