-1

I have a .jsp file that sends data via the post method to a servlet, but when i try to access the data in the servlet via request.getAttribute(), it prints out "null"

my .jsp file looks like this:

            <form name="update" action="UpdateServlet" method="post"
            accept-charset="utf-8">
            <label>Name:</label><input type="text" name="input_name"    id="input_name"><br>
            <label>Beschreibung:</label> <input type="text" name="input_beschreibung" id="input_beschreibung"><br>
            <input type="hidden" name="input_id" id="input_id">
            <input type="submit" value="Okay">
            <button type="button" onclick="closeDialog()">Abbrechen</button>
            </form>

my servlet looks like this:

public class UpdateServlet extends HttpServlet {

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html");

    System.out.println((String)request.getAttribute("input_id"));
    System.out.println((String)request.getAttribute("input_name"));
    System.out.println((String)request.getAttribute("input_beschreibung"));

    RequestDispatcher rd=request.getRequestDispatcher("LoadServlet");  
    rd.forward(request, response);

}

part of web.xml:

  <servlet>
  <servlet-name>UpdateServlet</servlet-name>
  <servlet-class>wochenplaner.UpdateServlet</servlet-class>
  </servlet>
  <servlet-mapping>
  <servlet-name>UpdateServlet</servlet-name>
  <url-pattern>/UpdateServlet</url-pattern>
  </servlet-mapping>

i dont understand why the attributes of the request are empty. Thanks in advance!

Mick
  • 113
  • 1
  • 8

3 Answers3

0

You are confusing request.getAttribute("") and request.getParameter(""). You need to use the latter:

public class UpdateServlet extends HttpServlet {

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html");

    System.out.println(request.getParameter("input_id"));
    System.out.println(request.getParameter("input_name"));
    System.out.println(request.getParameter("input_beschreibung"));

    RequestDispatcher rd=request.getRequestDispatcher("LoadServlet");  
    rd.forward(request, response);

}

See here for some further discussion:

https://stackoverflow.com/a/5243833/1356423

Community
  • 1
  • 1
Alan Hay
  • 22,665
  • 4
  • 56
  • 110
0

try this:

request.getParameter("input_id");
Rahim Dastar
  • 1,259
  • 1
  • 9
  • 15
0

You should be using request.getParameter() not request.getAttribute()

Difference between getAttribute() and getParameter()

Community
  • 1
  • 1
Ruelos Joel
  • 2,209
  • 3
  • 19
  • 33