Portál AbcLinuxu, 29. listopadu 2025 11:12
@WebServlet(name = "AddressBookServlet", urlPatterns = {"/do"})
public class AddressBookServlet extends HttpServlet {
private GroupManagerImpl groupManager = new GroupManagerImpl();
private ContactManagerImpl contactManager = new ContactManagerImpl();
@Resource(name = "jdbc/AddressBookManagerWeb")
private void setDataSource(DataSource dataSource) {
groupManager.setDataSource(dataSource);
contactManager.setDataSource(dataSource);
}
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
List<Group> groups = groupManager.getAllGroups();
request.setAttribute("groups", groups);
List<Contact> contact = contactManager.getAllContacts();
request.setAttribute("contacts", contact);
request.getRequestDispatcher("/list.jsp").forward(request, response);
}
protected void addContact(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
String name = request.getParameter("name");
String surname = request.getParameter("surname");
String phoneNumber = request.getParameter("phonenumber");
String email = request.getParameter("email");
//zpracování dat - vytvoření záznamu v databázi
try {
Contact contact = new Contact();
contact.setName(name);
contact.setSurname(surname);
contact.setPhoneNumber(phoneNumber);
contact.setEmail(email);
contactManager.createContact(contact);
} catch (Exception e) {
response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.getMessage());
}
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
addContact(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
addContact(request, response);
}
@Override
public String getServletInfo() {
return "Short description";
}
}
Inicializace formulare v jsp souboru. Nejsem s to sem dat celu obsah kvuli tagum, formular mi porad hazi same chyby typu:
Značky PRE a TH jsou překříženy!atd... proste nasleduje klasicky formular pro zadavani parametru
<form action="/servlet/do" method="post">
<table>
<tr>
Name:
<td><input type="text" name="name" value="<c:out value='${param.name}'/>"/>TD tag
</form>
Řešení dotazu:
<c:forEach items="${contacts}" var="contact" varStatus="loopStatus">
<tr class="${loopStatus.index % 2 == 0 ? 'odd' : 'even'}">
<td> <c:out value="${contact.name}"/</td>
<td><c:out value="${contact.surname}"/</td>
<td><c:out value="${contact.phoneNumber}"/></td>
<td><c:out value="${contact.email}"/></td>
<td>
<form action="<c:url value="/DeleteContact"/>" method="post">
<input type="hidden" id="id" value="${contact.id}" name="id"/>
<p><input type="Submit" value="Delete contact" name="submit"/></p>
</form>
<form action=" <c:url value="/UpdateContact"/>" method="post">
<input type="hidden" value="${contact.id}" name="id"/>
<p> <a href=" <c:url value="/UpdateContact"/>">Update Contact</a></p>
</form>
</td>
</tr>
</c:forEach>
Ja potrebuju do servletu dostat parametr
idTa nacte ID prislusne entity, takze kdyz chci smazat Contact, tak se mi t odari, v prislusne metode servletu mam:
Long id = Long.valueOf(request.getParameter("id"));
Jenze pro /Update Contactse mi id nepodari dostat. Vraci null. Ten UpdateContact presmerovava na formulat, ktery nacita nova data, ktera budou updatovat atributy entity Contact. V servletove metode pro Update pouzovam tutez metodu pro ziskani ID jako v metode pro delete:
Long id = Long.valueOf(request.getParameter("id")); Jde mi o to, abych mel u kazde entity tlacitko na DELETE nebo UPDATE. Ale pro update se mi z nejakeho duvodu nedari ziskat to ID. Nevedel by nekdo proc?
hreftagu:
http://stackoverflow.com/questions/11913609/hidden-input-in-jsp-produces-null-when-passing-it-to-the-servletA melo by se pouzit submit (tak, jak to mam pro DELETE). Jenze jak to v JSP napsat tak, aby se po kliknuti na SUBMIT do servletu nacetl hidden parametr a zobrazila se stranka s formularem, kam uzivatel vyplni udaje k updatu? Potreboval bych to ze SUBMIT presmerovat na muj .jsp soubor kde je formular pro update.
Tiskni
Sdílej:
ISSN 1214-1267, (c) 1999-2007 Stickfish s.r.o.