This question already has answers here:
How do I call a specific Java method on a click/submit event of a specific button in JSP?
(4 answers)
How do I pass current item to Java method by clicking a hyperlink or button in JSP page?
(1 answer)
Closed 3 years ago.
The code below is from the jsp file.
<table class="table table-striped table-hover table-responsive ezoo-datatable">
<thead>
<tr>
<th class="text-center">Schedule ID</th>
<th class="text-center">Feeding Time</th>
<th class="text-center">Recurrence</th>
<th class="text-center">Notes</th>
<th class="text-center">Food</th>
<th class="text-center">Animal ID</th>
<th></th>
</tr>
</thead>
<% int counter = 0; %>
<tbody>
<form action="feedingSchedules" method="post">
<c:forEach var="schedule" items="${feeding_schedules}">
<tr>
<td><c:out value="${schedule.schedule_ID}" /></td>
<td><c:out value="${schedule.feeding_time}" /></td>
<td><c:out value="${schedule.recurrence}" /></td>
<td><c:out value="${schedule.notes}" /></td>
<td><c:out value="${schedule.food}" /></td>
<td><c:out value="${schedule.animalID}" /></td>
<td><button class="btn-danger-stale" name="btn${counter}" value="val${counter}">Delete Schedule</button></td>
<% counter++; %>
</tr>
</c:forEach>
<input type="hidden" name="numSchedules" value="${counter}"/>
</form>
</tbody>
</table>
This code builds a table of data. I have a servlet to populate the table by fetching data from a database in a call to a dao method. I need to add buttons to the table to delete the row corresponding to the button. I have the buttons in place, but I'm not sure how to get them to perform the actual deletion.
#WebServlet(description = "This servlet is the main interface into the Feeding Schedules System", urlPatterns = { "/feedingSchedules" })
public class FeedingSchedulesServlet extends HttpServlet{
private static final long serialVersionUID = 1L;
#Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// Grab a list of Animals from the Database
FeedingScheduleDAO dao = DAOUtilities.getFeedingScheduleDao();
List<FeedingSchedule> schedules = dao.getAllSchedules();
// Populate the list into a variable that will be stored in the session
request.getSession().setAttribute("feeding_schedules", schedules);
request.getRequestDispatcher("feedingScheduleHome.jsp").forward(request, response);
}
#Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
FeedingScheduleDAO dao = DAOUtilities.getFeedingScheduleDao();
List<FeedingSchedule> schedules = dao.getAllSchedules();
//Get Parameters
System.out.println("got here");
int count = Integer.parseInt(request.getParameter("numSchedules"));
for(int i = 0; i < count; i++) {
String btn = null;
btn = request.getParameter("btn" + i);
if(btn == ("val" + i)) {
System.out.println("got here");
// call delete method from DAO
try {
dao.deleteSchedule(schedules.get(i));
request.getSession().setAttribute("message", "Schedule successfully deleted");
request.getSession().setAttribute("messageClass", "alert-success");
response.sendRedirect("feedingSchedules");
} catch (Exception e) {
e.printStackTrace();
request.getSession().setAttribute("message", "There was a problem deleting the schedule at this time");
request.getSession().setAttribute("messageClass", "alert-danger");
request.getRequestDispatcher("feedingScheduleHome.jsp").forward(request, response);
}
}
}
}
}
The above code is the servlet. The print lines I put in the overridden doPost method do not show in the console when I click the buttons, so I do not believe the method is being called properly. Does anyone know what I'm doing wrong? I've spent a few hours staring at this and could use some fresh eyes.
Assign an id to your form e.g.
<form id="myForm" action="feedingSchedules" method="post">
And replace
<button class="btn-danger-stale" name="btn${counter}" value="val${counter}">Delete Schedule</button>
with
<button class="btn-danger-stale" name="btn${counter}" value="val${counter}" onclick="document.getElementById('myForm').submit();">Delete Schedule</button>
Alternatively,
Assign an id to your form as mentioned above and also to your button as mentioned below:
<button id="myButton" class="btn-danger-stale" name="btn${counter}" value="val${counter}">Delete Schedule</button>
and add the following javascript in your jsp file:
var form = document.getElementById("myForm");
document.getElementById("myButton").addEventListener("click", function () {
form.submit();
});
Related
I have created a table in the jsp, for the table the data will be loaded from database and rendered into the jsp or web page to show the all appointments , in this particular scenario i want to delete the selected row from database .
The tried code is below but couldn't make it happen and no error is shown as well:
JSP:
<main>
<h1 style="margin-left:5rem; text-align: center; margin-top:4%;"> Scheduled Appointments </h1>
<div class="title-box">
<form method="POST">
<c:choose>
<c:when test="{empty list}">
<h1>No upcoming Appointments</h1>
</c:when>
<c:otherwise>
<table class="styled-table" style="margin-bottom: 15px; margin-top:15px;">
<thead>
<tr>
<th>Patient Name</th>
<th>Appointment Date</th>
<th>Delete</th>
</tr>
</thead>
<c:forEach items="${list}" var="record">
<c:if test="${record.ageInDays lt 1}">
<tbody>
<tr class="active-row">
<td>${record.name}</td>
<td>${record.date}</td>
<td><input type="" name="id" value="${record.appointmentId}"><i class="fas fa-trash-alt"></i></td>
</tr>
</tbody>
</c:if>
</c:forEach>
</table >
</form>
</c:otherwise>
</c:choose>
</div>
</main>
Servlet:
#Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
if(request.getServletPath().contains("/delete")){
/* deleting the appointment*/
int id= Integer.parseInt(request.getParameter("id"));
if(id>0){
StaffManager.getInstance().deleteAppointments(id);
List list = new ArrayList<>();//Creating a list to hold data
list = StaffManager.getInstance().getAppointments();//MVC method implementation for Searching patient
request.setAttribute("list", list);
request.getRequestDispatcher("/Appointments.jsp").forward(request, response);
}
}
}
The staff Manager:
public void deleteAppointments(int id) {
staff.deleteAppointments(id);
}
The Dao Class:
public void deleteAppointments(int id) {
try{
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/hms", "root", "");
PreparedStatement st=conn.prepareStatement("DELETE FROM appointments WHERE AppointmentsID = ? ");
st.setInt(1, id);
st.executeUpdate();
}catch(SQLException e){
e.printStackTrace();
}
}
I'm still new into java web development , so please excuse for not always following the best practices here .
And would really appreciate help to make this work , thank you in advance.
I'm trying to send a variable from my JSP to my Servlet using the post method. However, the value is continually returning null. I've put in prints and another type of hidden input to check for other errors, but it's just that the inputs are null in the servlet. Why is this?
JSP: (this code is within a table)
<c:set var="counter" value="0"/>
<tbody>
<form id="myForm" action="feedingSchedules" method="post">
<c:forEach var="schedule" items="${feeding_schedules}">
<tr>
<td><c:out value="${schedule.schedule_ID}" /></td>
<td><c:out value="${schedule.feeding_time}" /></td>
<td><c:out value="${schedule.recurrence}" /></td>
<td><c:out value="${schedule.notes}" /></td>
<td><c:out value="${schedule.food}" /></td>
<td><c:out value="${schedule.animalID}" /></td>
<td><button id="myButton" class="btn-danger-stale" name="btn${counter}" value="val${counter}">Delete Schedule</button></td>
<c:set var="counter" value="${counter + 1}"/>
<c:out value="${counter }"/>
</tr>
</c:forEach>
<c:out value="${counter }"/>
<input type="hidden" name="hi" id="hi" value="hi"/>
<input type="hidden" name="numSchedules" id="numSchedules" value="${counter}"/>
</form>
</tbody>
</table>
<script type="text/javascript">
var form = document.getElementById("myForm");
document.getElementById("myButton").addEventListener("click", function () {
form.submit();
});
</script>
Servlet: ('test' variable is null; code crashes at 'count' declaration because parseInt can't parse a null value)
#Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
FeedingScheduleDAO dao = DAOUtilities.getFeedingScheduleDao();
List<FeedingSchedule> schedules = dao.getAllSchedules();
//Get Parameters
System.out.println("got here");
String test = request.getParameter("hi");
System.out.println(test);
int count = Integer.parseInt(request.getParameter("numSchedules"));
for(int i = 0; i < count; i++) {
String btn = null;
btn = request.getParameter("btn" + i);
if(btn == ("val" + i)) {
System.out.println("got here");
// call delete method from DAO
try {
dao.deleteSchedule(schedules.get(i));
request.getSession().setAttribute("message", "Schedule successfully deleted");
request.getSession().setAttribute("messageClass", "alert-success");
response.sendRedirect("feedingSchedules");
} catch (Exception e) {
e.printStackTrace();
request.getSession().setAttribute("message", "There was a problem deleting the schedule at this time");
request.getSession().setAttribute("messageClass", "alert-danger");
request.getRequestDispatcher("feedingScheduleHome.jsp").forward(request, response);
}
}
}
}
You can try debugging the code by making the form method=GET. This will help you to know the values that are going to Servlet. Then you can just check the URL to see if the data is there or not. Try this if you cannot see the data in the url that means that the form submit is not working properly.
I use a html page with a textfield where you have to enter your name and then click on a login button.
On the same page is a list with all names.
Now I have to send the name by clicking on the login button to a servlet. The servlet have to add the name into the playerlist.
The servlet already receives the entered name but it post it on hole new HTML page. How do I have to change the code so that name will be added to playerlist on the same HTML page not on new page?
#WebServlet("/Playerlist")
public class Playerlist extends HttpServlet {
private static final long serialVersionUID = 1L;
public Playerlist() {
// TODO Auto-generated constructor stub
}
/**
* #see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String name = request.getParameter("name");
PrintWriter out = response.getWriter();
out.println("<html><Body>Hallo" + name + "</body></html>");
out.flush();
}
/**
* #see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
Textfield:
<div id= "loginFormular" >
<form action="Playerlist" method="get" >
<label>
Name:
<input class="textbox" id="loginbox" type="text" name="name" size="30" maxlength="30">
</label>
<br>
<input id=buttonLogin class="loginbutton" type="submit" value="Login" name="loginname" />
<input id=startButton class="loginbutton" type="submit" value="Start" onclick="showQuestion()" />
</form>
</div>
Playerlist:
<div class="highscore" style="float:right">
<h4>
<span class="glyphicon glyphicon-star" aria-hidden="true"></span>
<span>Highscore</span>
</h4>
<hr/>
<table id="highscoreTable" class="table table-hover">
<thead>
<tr>
<td>Player</td>
<td>Score</td>
</tr>
</thead>
<tbody id="tablePlayerlistBody">
</tbody>
</table>
</div>
I konw there are better ways instead of using a servlet, but I have to it this way.
Your page is too static for what you want to do. I am assuming that you are doing this for practice.
What you are doing is
submitting your name from a static page.
Getting the submitted name on servlet side.
Sending new content to client.
Instead what you need to do is
Create a dynamic page (JSP) with a text field and list.
Submit the field value to servlet.
Receive the value and persist it on server side. Maintain some type of list on server side and add this value to it and set it on either of the servlet contexts.
redirect to the same page using request dispatcher and then iterate over the list of values maintained on server side.
Something as below.
Servlet
String name = request.getParameter("name");
serverSideList.add(name );
JSP
<table id="highscoreTable" class="table table-hover">
<thead>
<tr>
<td>Player</td>
<td>Score</td>
</tr>
</thead>
<tbody id="tablePlayerlistBody">
<% for(int i=0 ; i< serverSideList.size(); i++){ %>
<tr>
<td>
<%=serverSideList.get(i)%>
</td>
<td>Some Score</td>
</tr>
<%}%>
</tbody>
</table>
For persisting you can do two things.
Simple : Maintain server side list in Application Context
Standard : Persist player detail in database.
Please feel free to ask if you have any questions.
I was trying to paging my product list into several pages. Each page contains 12 records. I used servlet to retrieve data from jsp file and JSTL to display data in JSP file. But my jsp file cant display anything. The main problem seems to be with "productList" not being available to the jsp
This is my servlet.
public class ProductServlet extends HttpServlet{
private static final long serialVersionUID=1L;
public ProductServlet(){
super();
}
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
int page=1;
int rowsPerPage=12;
if (request.getParameter("page") != null) {
page = Integer.parseInt(request.getParameter("page"));
}
ProductDAO pd = new ProductDAO();
List<Product> list = pd.getAllPaging((page - 1) * rowsPerPage, rowsPerPage);
int noOfRows = list.size();
int noOfPages = noOfRows / rowsPerPage;
request.setAttribute("productList", list);
request.setAttribute("noOfPages", noOfPages);
request.setAttribute("currentPage", page);
RequestDispatcher view = request.getRequestDispatcher("productlist.jsp");
view.forward(request, response);
}
}
This is my productlist.jsp file.
<table border="1" cellpadding="5" cellspacing="5">
<tr>
<th>Game ID</th>
<th>Game name</th>
<th>price</th>
<th>Realease date</th>
</tr>
<c:forEach var="product" items="${productList}">
<tr>
<td><c:out value="${product.gameid}"/></td>
<td><c:out value="${product.gamename}"/></td>
<td><c:out value="${product.price}"/></td>
<td><c:out value="${product.releasedate}"/></td>
</tr>
</c:forEach>
</table>
<%--For displaying Previous link except for the 1st page --%>
<c:if test="${currentPage != 1}">
<td>Previous</td>
</c:if>
<%--For displaying Page numbers.
The when condition does not display a link for the current page--%>
<table border="1" cellpadding="5" cellspacing="5">
<tr>
<c:forEach begin="1" end="${noOfPages}" var="i">
<c:choose>
<c:when test="${currentPage eq i}">
<td>${i}</td>
</c:when>
<c:otherwise>
<td>${i}</td>
</c:otherwise>
</c:choose>
</c:forEach>
</tr>
</table>
And finally, my web.xml
<servlet>
<servlet-name>ProductServlet</servlet-name>
<servlet-class>product.ProductServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ProductServlet</servlet-name>
<url-pattern>/productlist</url-pattern>
</servlet-mapping>
I debugged my method getAllPaging() and it works perfectly, I checked and rechecked my jsp with JSTL syntax. It's ok. So I think the problem belongs to my servlet but I cant figure out what it is. Thank you for your help.
I have a web form in a jsp that is supposed to get values from a servlet, but it is printing out null values. I am including the code for the jsp and for the servlet below. Can anyone show me how to fix the code below so that it prints out the values from the request object instead of printing null?
Here is the code for my.jsp:
<jsp:useBean id="errors" scope="request" type="java.util.Map" class="java.util.HashMap" />
<form method="post">
<table>
<tr>
<td width=302>
</td>
<td width=250>
<table>
<tr>
<td width=150 align="right">tha: </td>
<td><input type="text" name="tha" value="c3t" size="15" />
<%if (errors.containsKey("tha")) {out.println("<span class=\"error\">" + errors.get("tha") + "</span>");}
else{out.println(request.getParameter("tha"));}%>
</td>
</tr>
<tr>
<td width=150 align="right">min: </td>
<td><input type="text" name="min" value="0" size="15" />
<% if (errors.containsKey("min")) {out.println("<span class=\"error\">" + errors.get("min") + "</span>");}
else{out.println(request.getParameter("min"));}%>
</td>
</tr>
<tr>
<td width=150 align="right">max: </td>
<td><input type="text" name="max" value="2*pi" size="15" />
<% if (errors.containsKey("max")) {out.println("<span class=\"error\">" + errors.get("max") + "</span>");}
else{out.println(request.getParameter("max"));}%>
</td>
</tr>
<tr>
<td></td>
<td align="right">
<input type="submit" name="submit-button" value="Click To Plot" />
</td>
</tr>
</table>
</td>
</tr>
</table>
</form>
And here is the code for the servlet:
public class PlotPolarServlet extends HttpServlet{
private RequestDispatcher jsp;
public void init(ServletConfig config) throws ServletException {
ServletContext context = config.getServletContext();
jsp = context.getRequestDispatcher("/WEB-INF/jsp/my.jsp");
}
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
jsp.forward(req, resp);
}
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException{
Map<String, String> errors = validate(req);
if (!errors.isEmpty()){
jsp.forward(req, resp);
return;
}
resp.sendRedirect("my");
}
public static Map<String, String> validate(HttpServletRequest req){
HashMap<String, String> errors = new HashMap<String, String>();
req.setAttribute("errors", errors);
String tha = req.getParameter("tha");
if (tha == null || tha.trim().length() == 0){
errors.put("tha", "tha required.");
}
String min = req.getParameter("min");
if (min == null || min.trim().length() == 0){
errors.put("min", "min required.");
}
String max = req.getParameter("max");
if (max == null || max.trim().length() == 0){
errors.put("max", "max required.");
}
return errors;
}
}
I originally misread your question. The issue is when parameters are input correctly, not when there is an error. The else part of this code
<% if (errors.containsKey("min")) {out.println("<span class=\"error\">" + errors.get("min") + "</span>");}
else{out.println(request.getParameter("min"));}%>
can't print anything but null because, in this request, there are no parameters with those keys.
A HttpServletResponse#sendRedirect(String) returns a 302 HTTP status code which would make your browser send a new HTTP request to the location described by the String parameter. The request parameters would not be in the new request (the request context/scope is cleared once the request is handled). You would need to put them in the session attributes.
/*
for every parameter, put it in the session attributes
*/
req.getSession(true).setAttribute("myParam", req.getParameter("myParam"));
resp.sendRedirect("my");
This is known as flash scope and flash attributes and it can be implemented as explained here with a servlet Filter. You basically store attributes you want to reuse between 2 requests, then delete them.
As for Edit:
Unless you've copy-pasted the code in your edit incorrectly
<td><input type="text" name="yX" value="cx" size="15" />
<%if (errors.containsKey("yX")) { // errors doesn't contain yX as a Key, it contains imageParam1
out.println("<span class=\"error\">" + errors.get("yX") + "</span>");
}else{out.println(request.getParameter("yX"));}%> // will print the value of the request parameter with key yX
</td>
you're POSTing a parameter called yX, but looking for imageParam. Your doPost() method will create an errors request attribute and then forward (instead of sendRedirect).
errors.put("imageParam1", "imageParam1 required.");
...
if (!errors.isEmpty()){
jsp.forward(req, resp);
return;
}
not yX. Therefore, the else gets evaluated and the parameter exists since it's the same request.
MORE EXPLANATION
In your my example:
If you didn't enter a required field, the doPost() method gets called, the errors map is populated and your code does jsp.forward(req, resp); which uses the same request and the parameters are available when the jsp is rendered.
If you entered all the required fields, the doPost() is called and resp.sendRedirect("my"); is executed. This causes your browser to send a new GET HTTP request with new parameters/attributes. This causes the doGet() to be called to process the new request, which forwards to your jsp. The original request parameters aren't included in this request so there is nothing to show, thus null when else{out.println(request.getParameter("min")); gets rendered.
In your myother example, because of the difference between the <input> parameter name in the jsp and the parameter name you're looking for in the servlet, you're getting completely irrelevant results which I've explained above. Disregard this servlet. It is not doing what you thought it was doing correctly.