I need help in displaying a text box dynamically near a check box value in jsp page without using JSTL implementation.
Work Flow:
1.The user enters a value in check box and clicks search button
2.On clicking search the servlet is called. The servlet focuses on the database implementation and forward the result set values to the same jsp page from where the request comes.
3.The result set values are displayed in the form of a check box in jsp page. Near to each check box a text box should be displayed.
Issue:
The result set values are getting displayed in the form of a check box in my jsp page.But I need to add a text box near to my check box value in my jsp page dynamically.i.e. If I have 3 check box then a text box should be displayed nearer to each of the 3 check box values in jsp page.
This is my code.
Productlist.jsp
<%@page import="java.util.List"%>
<%@page import="web.Products"%>
<%@page import="java.util.ArrayList"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Products</title>
</head>
<body>
<form method="post" align="center" action="ProductList">
Company Name:<input type="text" size="20" id="company" name="company" />
<input type="submit" value="search"/>
<%
List<Products> pdts = (List<Products>) request.getAttribute("list");
if(pdts!=null){
for(Products prod: pdts){
out.println("<input type=\"checkbox\" name=\"prod\" value=\"" + prod.getProductname() + "\">" + prod.getProductname() + "<br>");
}
}
%>
</form>
</body>
</html>
Servlet Code
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.http.HttpSession;
public class ProductList extends HttpServlet {
static final String dbURL = "jdbc:mysql://localhost:3306/pdt";
static final String dbUser = "root";
static final String dbPass = "root";
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
ResultSet rs = null;
Connection connection = null;
List<Products> pdt = new ArrayList<Products>();
try{
String company =request.getParameter("company");
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection (dbURL,dbUser,dbPass);
String sql="select product_pck from comp_pdt_list where company_name='"+company+"'";
PreparedStatement prep = connection.prepareStatement(sql);
rs=prep.executeQuery();
if(rs!=null)
{
while(rs.next())
{
Products prod=new Products();
prod.setProductname(rs.getString("product_pck"));
pdt.add(prod);
}
request.setAttribute("list",pdt);
RequestDispatcher rd=request.getRequestDispatcher("Productlist.jsp");
rd.forward(request,response);
return;
}
prep.close();
}
catch(Exception E){
//Any Exceptions will be caught here
System.out.println("The error is"+E.getMessage());
}
finally {
try {
connection.close();
}
catch (Exception ex) {
System.out.println("The error is"+ex.getMessage());
}
}
}
}
I got the solution.
Instead of using if condition we can display the text box inside the for loop so that the text box will be displayed dynamically near the check box values based on the result set size.
Here is the code
<td> <b>
<%
List<Products> pdts = (List<Products>) request.getAttribute("list");
if(pdts!=null){
for(Products prod: pdts){
out.println("<input type=\"checkbox\" name=\"prod\" value=\"" + prod.getProductname() + "\">" + prod.getProductname()+"<br>");
}
%>
</b></td>
<td>
<%
for(Products prod: pdts){
out.println("<input type=\"text\" name=\"free\" style=\"width:50px; height:22px\"/><br/>");
} }
%>
</td>