Get checkbox values from JSP Page - java

I'm trying to get all the values from a list of selected checkboxs in a JSP Page, to a Java Class.
That's My JSP Page:
<table border="1" cellpadding="5" cellspacing="1" style="width: 877px; ">
<tr>
<th>Code</th>
<th>Name</th>
<th>Price</th>
<th>Select</th>
<th>Edit</th>
<th>Delete</th>
<th>Show</th>
</tr>
<c:forEach items="${productList}" var="product" >
<tr>
<td>${product.code}</td>
<td>${product.name}</td>
<td>${product.price}</td>
<td>
<input type="checkbox" name="ProductItem" value="${product.code}">
<c:url value="ShowProductList" var="url">
<c:param name="ProductItemP" value="${product.code}"/>
</c:url>
</td>
<td>
Edit
</td>
<td>
Delete
</td>
<td>
Show
</td>
</tr>
</c:forEach>
</table>
Show Items
As you can see there is a Table with a list of items with a check box in each line. At the end of the table there is button "Show Items" that triggers the user's request.
That's the servlet class that perform the request:
#WebServlet(urlPatterns = { "/ShowProductList" })
public class ShowProductList extends HttpServlet {
private static final long serialVersionUID = 1L;
public ShowProductList() {
super();
}
#Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
Connection conn = MyUtils.getStoredConnection(request);
String[] paramValues = request.getParameterValues("ProductItemP");
System.out.println(paramValues.length);
response.sendRedirect(request.getContextPath() + "/productList");
}
#Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
When an user select two or more checkbox and click on the button "Show item" I can have only the last product code with the checkbox clicked and not all the codes with the box selected. If I Try request.getParameterValues("ProductItem"); I have a null value. I'd prefer to not have code in the JSP Page (if it is possible.)
Can someone help me find a solution ?
Thanks for your patience.
`

Related

arraylist to select an option in jsp

How to assign an arraylist to select an option in jsp? I found an example(how to assign arraylist to select option in jsp) but still get empty options.
IndexServlet.java
#WebServlet("/IndexServlet")
public class IndexServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
List<String> databaseArrayList = new ArrayList();
databaseArrayList.add("1");
databaseArrayList.add("2");
databaseArrayList.add("3");
request.setAttribute("databaseList", databaseArrayList);
request.getRequestDispatcher("AddManager.jsp").forward(request, response);
System.out.println("in index servlet");
}
}
JspCode
<tr>
<td>Subordinate employees:</td>
<td>
<select name="database1">
<c:forEach items="${databaseList}" var="databaseValue">
<option value="${databaseValue}">
${databaseValue}
</option>
</c:forEach>
</select>
</td>
</tr>
You are not forwarding the request..
RequestDispatcher rd=request.getRequestDispatcher("/AddManager.jsp");          
rd.forward(request, response);  

How i can put other img to JSP file to table

Okay, at first excuse me for my English skill.
I have a problem. Servlet load to jsp ArrayList with file address and directory address. I need to for director was one image and for files other image. Now it's look's like this pic
p.s. If you can help me with drop down list (when click on directory open and change image from close folder to open folder) I'll be very happy.
yours respectfully
JSP file:
<table border = "1">
<c:forEach items = "${fath}" var = "fath">
<th> <a href="/back?name=${fath}">
<img src="opfolder.png"> </a>
</th>
<th>
${fath}
</th>
</c:forEach>
<c:forEach items= "${FileZ}" var="FileZ">
<tr>
<td>
<a href = "open?name=${FileZ.father}">
<img src ="clfolder.png"/>
</a>
</td>
<td>${FileZ.name}</td>
</tr>
</c:forEach>
</table>`
Java Servlet file :
#WebServlet(name = "ToOut" , urlPatterns = "/fileman")
public class ToOut extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setAttribute("FileZ",AllFiles.getDir(AllFiles.getAdress()));
request.getRequestDispatcher("fileman.jsp").forward(request,response);
}}

get file path from local drive with jsp and servlet

I have a problem at the moment when I want to get the file path. This is my code:
public void service(HttpServletRequest request, HttpServletResponse res)
throws ServletException, IOException {
String cabArch = req.getParameter("fRutaArch");
String rutaArch = getFileName(filePart);
}
And in jsp I have this:
<td align="left" class="e2">
<input type="file" name="fRutaArch" id="fRutaArch" title="Seleccionar archivo">
</td>
<td>
<button type="submit" name="bCargar" id="bCargar">Cargar</button>
</td>
I just need the full file path, please any advice?
You can add a hidden field in the html code and modify the service function
the code follows below
<td align="left" class="e2">
<input type="file" name="fRutaArch" id="fRutaArch" title="Seleccionar archivo" onchange="document.getElementById('filepath').value=this.value" >
</td>
<td>
<button type="submit" name="bCargar" id="bCargar">Cargar</button>
</td>
<input type='hidden' name='filepath' id='filepath'/>
then the function service
public void service(HttpServletRequest request, HttpServletResponse res)
throws ServletException, IOException {
String cabArch = req.getParameter("filepath");
String rutaArch = getFileName(cabArch);
}

how to make an .jsp invoke a method using eclipse Google app engine

hello iam having problem invoking function in my project ![enter image description here]
<form action="register" method="post">
<table>
<tr>
<td colspan="2" style="font-weight:bold;">UserID:</td><td><input type="text" name="Id"></td>
</tr>
<tr>
<td colspan="2" style="font-weight:bold;">Name :</td><td><input type="text" name="name"></td>
</tr>
<tr>
<td colspan="2" style="font-weight:bold;">Password</td><td><input type="password" name="pass"></td>
</tr>
<tr>
<td><input type="button" value="sumbitk"></td>
</tr>
</table>
</form>
<hr>
$
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws IOException {
resp.setContentType("text/plain");
resp.getWriter().println("Hello, world");
}
public void doPost(HttpServletRequest req, HttpServletResponse resp)
throws IOException, ServletException {
//RequestDispatcher dispatcher;
String id=req.getParameter("id");
String name=req.getParameter("name");
String password=req.getParameter("pass");
System.out.println("user id : "+id+" name "+name+" pass "+password);
resp.sendRedirect("/index.html");
//dispatcher=getServletContext().getRequestDispatcher("/index.html");
//dispatcher.forward(req, resp);
}
}
i made form action on register and calls function post but it not seems to work when i click sumbit i dont know why .
First try write "submit" correctly in last input, if don't work try chage the type of last input to "submit" instead "button"

Passed data from JSP to Servlet returning null in Servlet

<table border="1" cellpadding="5" id="newtable">
<!-- <caption id="tablehead">Rooms are available!</caption> -->
<!-- <tr class="hover"> -->
<tr>
<th>Room No</th>
<th>AC</th>
<th>Deluxe</th>
<th>Tariff</th>
</tr>
<c:forEach var="room" items="${myrooms}">
<tr bgcolor="#4B476F" onMouseOver="this.bgColor='gold';" onMouseOut="this.bgColor='#4B476F';">
<td class="nr">1</td>
<td name="ac"><c:out value="${room.ac}" /></td>
<td name="deluxe"><c:out value="${room.deluxe}" /></td>
<td>₹<c:out value="${room.price}" /></td>
<td><button type="button" class="mybutton" onclick="location.href='passtopayment'">Pay</button> </td>
</tr>
</c:forEach>
</table>
I want to get the td value for AC and Deluxe column on click of the corresponding row. However, when the execute the following servlet code I get, null null is printed. Please help!
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String ac = request.getParameter("ac");
String deluxe = request.getParameter("deluxe");
out.println(ac);
out.println(deluxe);
}
/**
* #see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
You are using location.href='passtopayment' that is not the correct way to submit a form.
It's just a like a separate request to the Servlet nothing will be send to the Servlet.
You should use form and submit the request to the Servlet.
<form action="passtopayment" method="post">
<!-- HTML controls -->
<input type="submit" value="Submit"/>
</form>
Here is detailed Example

Categories