3

As described in the title I need to pass data from my JSP page to my servlet. I load data out of a database into a form of my JSP page. Now the user should be able to change that data. So I have to send the changed data back to my servlet to update my database. Therefore I want to use the doPost() method in my servlet

This is my JSP:

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <meta http-equiv="content-script-type" content="text/javascript" />
    <meta http-equiv="content-style-type" content="text/css" />
    <meta http-equiv="content-language" content="de" />

    <link href="../resources/css/basic.css" type="text/css" rel="stylesheet" />

    <title>Edit Movie</title>
</head>

<body>
    <div id="wrapper">
        <h2 id="title">Edit Person</h2>
        <br></br>
        <br></br>
        <form id="1" class="appnitro"  method="post" action="">                     
            <ul>
                <li id="li_1" >
                    <label class="description" for="element_1">Name</label>
                    <div>
                        <input id="element_1" name="element_1" class="element text large" type="text" maxlength="255" value="${requestScope.person.name}"/> 
                    </div> 
                </li>       
                <li id="li_2" >
                    <label class="description" for="element_2">Deparment</label>
                    <div>
                        <input id="element_2" name="element_2" class="element text large" type="text" maxlength="255" value="${requestScope.person.department}"/> 
                    </div> 
                </li>   
                <li id="li_3" >
                    <label class="description" for="element_3">Job</label>
                    <div>
                        <input id="element_3" name="element_3" class="element text large" type="text" maxlength="255" value="${requestScope.person.job}"/> 
                    </div> 
                </li>       
                <li id="li_4" >
                    <label class="description" for="element_4">Biographie</label>
                    <div>
                        <textarea id="element_4" name="element_4" class="element textarea medium">${requestScope.person.biography}</textarea> 
                    </div> 
                </li>
                <li class="buttons">
                    <input type="hidden" name="form_id" value="652973" />
                    <input id="saveForm" class="button_text" type="submit" name="submit" value="Submit" />
                </li>
            </ul>
        </form>                 
    </div>
</body>
</html>

And this is my Servlet without the doPost() method:

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import de.hof.university.spj.model.People;
import de.hof.university.spj.model.PeopleDAO;

public class SinglePersonEditServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
    private PeopleDAO peopleDao = new PeopleDAO();

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        String name = "id";
        String value = request.getParameter(name);

        int id = Integer.parseInt(value);

        People people = peopleDao.getPerson(id);

        request.setAttribute("person", people);

        RequestDispatcher reqDispatcher = request.getRequestDispatcher("../jsp/singlePersonEdit.jsp");
        reqDispatcher.forward(request, response);
    }
}

After the submit button was pressed I want to send the changed data to my servlet so I can store it in my database.

informatik01
  • 16,038
  • 10
  • 74
  • 104
Mulgard
  • 9,877
  • 34
  • 129
  • 232

1 Answers1

5

Why String name = "id"; String value = request.getParameter(name); ? I can't seem to find any input in your JSP that's name = "id" ...

In the servlet, you should have this (for example), this :

String element_1_value = request.getParameter("element_1") ;

Either you forgot the input with id name or I am missing something. In any case, this is what you need to fix within your code.

Not to mention that you forgot inserting the name of the servlet in the action attribute of the form tag, so you had this :

<form id="1" class="appnitro"  method="post" action="">

Which should become this :

<form id="1" class="appnitro"  method="post" action="SinglePersonEditServlet">

Finally, your action method is "post" (as shown in the above two code lines), in the piece of servlet of your question you work with doGet, you ought to put your code in doPost unless that's done, otherwise it's sufficient to call doGet inside doPost.

I am a beginner myself, so I recognize one when I see it, we all started somewhere and I would recommand you this totu or any good search about "handling form data with servlet".

Note : duplicate of this, check it out for further learning :).

Regards.

Community
  • 1
  • 1
Akheloes
  • 1,352
  • 3
  • 11
  • 28
  • This is from another site!!! i have a site people.jsp with links to single persons: String name="id" i need to select the person from the database with the given id and give it to my jsp site postet above. my first input is correct, cause i want the textfields to show the actually saved data out of the database. the user now should be able to change the textfields input. this is what i want to give to my servlet. – Mulgard Jun 23 '13 at 13:45
  • That's alright then, keep the `value="${requestScope.person.name}"` in you'r elements. But for the rest, I think your missing few basic notions on how to make it function. I edited my answer take a look :) – Akheloes Jun 23 '13 at 13:51
  • Otherwise : where's the element with the attribute `name` of value id ? – Akheloes Jun 23 '13 at 13:52
  • the code in the doget method is for another mechanism... the doget code can be totally ignored for this problem here ^^ i need to write a dopost method but i actually dont know how to get the informations from my form into that dopost method. but i think you wrote it already: "String element_1_value = request.getParameter("element_1") ;" i will test it – Mulgard Jun 23 '13 at 13:57
  • Oh, so I think you need to clarifiy your question a bit, because you want to get data from a form into a servlet (which will do with it whatever you want to do). Indeed, you need to pass a value of a name attribut of some input in your form `getParameter`. Do not forget to fill the action attribut as I said :). – Akheloes Jun 23 '13 at 14:01