Convert a List to String to populate webpage in JSP - java

I have DAO bean rows retrieved in a List. In my JSP I am accessing the List to iterate thru to populate my page. My JSP can't access the List because it says it must be a String when I execute a request.getParameter. How I convert this to String eventually populate my page?
public List getAccessRequest()
{
List accessRequesttList = new ArrayList()); // parse List to string
//AccessRequest accessrequest = null;
AccessRequest accessRequest = new AccessRequest());
try
{
System.out.println("Try statement begins AccessRequestDAO");
PreparedStatement accrqststmt = super.getConnection().prepareStatement(AccRqstSqlStmt);
ResultSet resultSet = accrqststmt.executeQuery();
while (resultSet.next())
{
// Creating an instant of job follows
accessRequest = new Accessrequest();
accessRequest.setJOB_NAME(resultSet.getString("job_name"));
accessRequest.setRequest_ts(resultSet.getTime("request_ts"));
accessRequestList.add(accessRequest);
Iterator iterator = accessRequestList.iterator();
while (iterator.hasNext())
{
accessRequest = (Accessrequest) iterator.next();
}
}
return (accessRequestList);
My JSP look like below:
<%
List jobList = request.getParameter("acccessrequests"); // parse List to String
Iterator iterator = jobList.iterator();
while (iterator.hasNext())
{
accessRequest = (AccessRequest) iterator.next());
%>
<tr>
<td><input type="checkbox" name="<%accessRequest.getApproval_ind(); %>"></td>
<td><input type="text" id="jobname' name="accessRequests" value="job_name"></td>

HttpServletRequest#getParameter() returns a String, not a List. So the compiler is right.
I am not sure how you have ever set the List as a request parameter, there's no such method like HttpServletRequest#setParameter(). So you're probably misinterpreting something. The normal approach is to set the list as request attribute by HttpServletRequest#setAttribute() and access it in JSP by EL (expression language) like as ${attributeName}. You also normally iterate over the list using JSTL <c:forEach> tag.
Assuming that you've set the list in the request scope using a Servlet like follows...
request.setAttribute("list", list);
...here's a kickoff example how to iterate over the list:
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
...
<table>
<c:forEach items="${list}" var="item">
<tr>
<td>${item.property1}</td>
<td>${item.property2}</td>
<td>${item.property3}</td>
</tr>
</c:forEach>
</table>

Alhamdulillah, thanks God!
This help me a lot. I try to build my own java Web framework.
Before reading this QA, I don't know how to access an object (say, row of table) from a JSP.
I just redirect it, and leave the database code in JSP, generated by DreamWeaver. Now I know how to do it. For example, this is a BiroController, which display data from Biro Table :
public void index() throws IOException, ServletException {
List list=new ArrayList();
list.add(BiroModel.create("1", "SDM"));
list.add(BiroModel.create("2", "Keuangan"));
request.setAttribute("list", list);
super.index();
}
firstly, I populate an array (subsequently, this will come from database table). and then set request attribute, then call superclass index method :
public void index() throws IOException, ServletException {
RequestDispatcher rd = request.getRequestDispatcher(viewPage);
if(rd!=null){
rd.forward(request, response);
}else{
PrintWriter out = response.getWriter();
out.print("can not dispatch to " + viewPage);
}
//OLD Code : response.sendRedirect(ServletUtil.getBaseUrl(request) + viewPage)
}
And, I did as you instructed in the JSP :
<c:forEach items="${list}" var="item">
<tr>
<td>${item.idbiro}</td>
<td>${item.biro}</td>
</tr>
</c:forEach>
I use Netbeans, so I can easily pick JSTL library from the list of available library
It works charmingly.. :) tq

Related

How could i print after i used request.getAttribute in a jsp?

In my servlet, i have:
List list = new ArrayList();
....
request.getSession().setAttribute("list",list);
RequestDispatcher dispatcher=request.getRequestDispatcher("result.jsp");
dispatcher.forward(request,response);
And in my result.jsp file, i wanted to print out the checks on website, so i tried :
String[] str = (String[])request.getAttribute("list");
But there is a error said
org.apache.jasper.JasperException: java.lang.ClassCastException: java.util.ArrayList cannot be cast to [Ljava.lang.String;
So what should i do to print the list?
Thank you.
Actually list is of type ArrayList not an Array, so try this instead :
<%
ArrayList<String> list = (ArrayList<String>) request.getSession().getAttribute("list") ;
//do something ...
%>
And make sure you are allowing your jsp to access to the Session using : <%# page session="true" %>
However as #JBNizet said it's so preferable to use jstl expression over than Java code in the jsp pages :
in the servlet :
List<String> list = new ArrayList<>();
request.setAttribute("list" , list);
RequestDispatcher dispatcher=request.getRequestDispatcher("result.jsp");
dispatcher.forward(request,response);
In the Jsp :
<%# taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<c:forEach items="${list}" var="element">
//use the element here...
${element}
</c:forEach>

Iterating and setAttribute on a request Scope

I've got a List of Products obtained from a database query.
In each Product there is a Date_in and a Date_out variable of Calendar type.
What I want to do is pass these Products to a jsp view, so that I can build a table with all their informations. To do so I need to convert the Calendar dates to a more suitable format (like a String), and I've got an utility for this.
My problem is: How can I pass each converted date_in/date_out to the request.setAttribute, for each Product in the List?
Here's what I've got.
in ProductAction:
List<Product> products = service.viewActiveProducts();
for(Product product: products) {
String date_inToString = DateConversionUtility.calendarDateToString(product.getDate_in());
String date_outToString = DateConversionUtility.calendarDateToString(product.getDate_out());
}
request.setAttribute("products", products);
in the jsp:
<table>
<tr class="index">
<td>Id</td>
<td>Date IN</td>
<td>Date OUT</td>
</tr>
<c:forEach items="${requestScope.products}" var="product">
<tr>
<td>${product.oid}</td>
<td>${product.date_in}</td>
<td>${product.date_out}</td>
</tr>
</c:forEach>
</table>
(Right now product.date_in and product.date_out are in a Calendar format. I want them in a String format).
You can create a custom JSTL function and use it directly in your JSP EL tags.
<td>${product.oid}</td>
<td>${nspace:formatDate(product.date_in)}</td>
<td>${nspace:formatDate(product.date_out)}</td>
Where nspace is the xml namespace assigned to your library and formatDate your method. A couple of links:
http://www.noppanit.com/how-to-create-a-custom-function-for-jstl/
http://anshulsood2006.blogspot.com.es/2012/03/creating-custom-functions-in-jsp-using.html
You can pass entire list to jsp by request.setAttribute(String,Object);
List<Product> products = service.viewActiveProducts();
request.setAttribute("products", products);
Your Product class should be implementing Serializable interface
To get your properties date_in and date_out in string format, convert them into string and have two more properties in Product class with names say dateInString and dateOutString and set their values like
for(Product product: products) {
product.setDateInString(DateConversionUtility.calendarDateToString(product.getDate_in()));
product.setDateOutString(DateConversionUtility.calendarDateToString(product.getDate_out()));
}
In jsp, you can user JSTL to get the properties or field of Products object iterating list
<c:forEach items="${products}" var="product">
<tr>
<td>${product.oid}</td>
<td>${product.dateInString}</td>
<td>${product.dateOutString }</td>
</tr>
</c:forEach>
Make a getDate_inString() and getDate_outString methods in Product bean that will print what you want. Then, in the jsp you can use ${product.date_inString}

More concise way to iterate this treemap data structure in JSTL

I am using the following data structure to store JDBC results in a Servlet controller prior to displaying in a JSP view using JSTL.
TreeMap
- TreeMap
- String[]
Four columns of data are returned per row.
"CATEGORY","OSDIRECTORY","FILENAME","DESCRIPTION"
"CATEGORY","OSDIRECTORY","FILENAME","DESCRIPTION"
"CATEGORY","OSDIRECTORY","FILENAME","DESCRIPTION"
etc.
The goal is to store the results in the data structure as
Category
- FILENAME
- OSDIRECTORY
- DESCRIPTION
And to display the final results in the View as
Category A
Hyperlink
Hyperlink
Hyperlink
Category B
Hyperlink
Hyperlink
etc.
Relevant Servlet Controller Code Snippet
...
TreeMap treeMap = new TreeMap();
rs = stmt.executeQuery(query);
// Gather raw data
while(rs.next()){
if(!treeMap.containsKey(rs.getString("CATEGORY"))){
treeMap.put(rs.getString("CATEGORY"), new TreeMap());
}
String[] tmp = { rs.getString("OSDIRECTORY"), rs.getString("DESCRIPTION") };
((TreeMap)treeMap.get(rs.getString("CATEGORY"))).put(rs.getString("FILENAME"), tmp);
}
request.setAttribute("filemap", treeMap);
RequestDispatcher rd = request.getRequestDispatcher(VIEW_URL);
rd.forward(request, response);
...
Relevant JSP View JSTL Snippet
<c:forEach var="f" items="${filemap}">
<h1><c:out value="${f.key}"/></h1>
<c:forEach var="g" items="${filemap[f.key]}">
<a href="TBD">
<c:out value="${filemap[f.key][g.key][0]}"/>
<c:out value="${filemap[f.key][g.key][1]}"/>
</a>
</c:forEach>
</c:forEach>
I am wondering of there is a more concise way to express some of the JSTL expressions.
For example ${filemap[f.key][g.key][0]} just seems too verbose to me.
I would make objects to represent your data instead of using a map of maps. Since you have categories that contain files, make a category and file objects that look something like this:
public class Category {
private String name;
private List<DbFile> files = new ArrayList<DbFile>();
// getters & setters for each property go here
}
public class DbFile {
private String filename;
private String osDirectory;
private String description;
// getters & setters for each property go here
}
Then in your data access code you can iterate over the result set and build up a list of categories objects like this:
Map<String, Category> categoryMap = new TreeMap<String, Category>();
while(rs.next()){
String categoryName = rs.getString("CATEGORY");
Category category = categoryMap.get(categoryName);
if(!categoryMap.containsKey(categoryName)){
category = new Category();
category.setName(categoryName);
}
DbFile file = new DbFile();
file.setFilename(rs.getString("FILENAME"));
file.setOsDirectory(rs.getString("OSDIRECTORY"));
file.setDescription(rs.getString("FILENAME"));
category.getFiles().add(file);
categoryMap.put(categoryName, category);
}
request.setAttribute("categories", Arrays.asList(categoryMap.values()));
Then the code on your page is much more straightforward:
<c:forEach var="category" items="${categories}">
<h1><c:out value="${category.name}"/></h1>
<c:forEach var="file" items="${category.files}">
<a href="TBD">
<c:out value="${file.osDirectory}"/>
<c:out value="${file.description}"/>
</a>
</c:forEach>
</c:forEach>
After some later investigation and experimentation, I found this to be a cleaner approach than the JSTL posted in the question.
<c:forEach var="f" items="${filemap}">
<h1><c:out value="${f.key}"/></h1>
<c:forEach var="g" items="${f.value}">
<a href="TBD">
<c:out value="${g.value[0]}"/>
<c:out value="${g.value[1]}"/>
</a>
</c:forEach>
</c:forEach>

retrieve data from database in struts

I am retrieving all databases data with struts framework,code is working good.
i just put list(Array list) of data in request for JSP(as above code).i know we can get data with logic tag library but i don't have much idea of that.and is it okay to put data in request?and how we can retrive data with logic library?or any other way?
HashMap hm =null;
List list = new ArrayList();
String uname="";
int no=0;
while(rs.next())
{
uname=rs.getString(1);
no = Integer.parseInt(rs.getString(2));
hm=new HashMap();
hm.put("name", uname);
hm.put("number", no);
list.add(hm);
}
request.setAttribute("myList",list);
jsp page.
<%
try
{
DataForm d= new DataForm();
HashMap hm;
List list = new ArrayList();
if(request.getAttribute("myList")!=null)
list=(List)request.getAttribute("myList");
else
out.print("No Data Found..");
for(int i=1;i<list.size();i++)
{
hm=(HashMap)list.get(i);
%><tr> <td><%out.println(hm.get("name"));%></td>
<td><%out.println(hm.get("number"));%></td></tr><%
}
}
catch(Exception e){e.printStackTrace();}
%>
The logic tags are not used to retrieve the data: They're used to implement some logic in the JSP without using scriptlets, which are considered bad practice.
It's indeed the job of the controller (the Struts action), not to retrieve the data from the database, but to call objects that will, and to put the list of objects in the request in order for the view to display them.
The struts logic tag, however, are almost all deprecated in favor of the JSP EL and the JSTL. Your scriptlet code could be replaced by the following lines:
<c:choose>
<c:when test="${myList == null}">
No Data Found.
</c:when>
<c:otherwise>
<c:forEach var="element" items="${myList}">
<tr>
<td><c:out value="${element.name}"/></td>
<td><c:out value="${element.number}"/></td>
</tr>
</c:forEach>
</c:otherwise>
</c:choose>
Which is much more readable and escapes the data correctly.
I would also use objects instead of HashMaps to hold your data. Java is an OO language. Use objects and encapsulation.
If I correctly understand what you are trying to do, I must say that you are not making effective use of a HashMap.
A Hashmap is used to store pairs so that you are able to retrieve the values using a key fast ~ O(1). Keys are unique in a hashmap. If you try to store multiple pairs one after the other using the same key, the data will be be overwritten and in the end you will see only the last pair.
In this example, you are using a HashMap just as a container to hold "name" and "number" and hence I don't see a real need for the hashMap at all.
This is what you can do:
Define a class that holds name and number:
class Data
{
private String name;
public int uNo;
public Data(String name, int uNo)
{
this.name = name;
this.uNo = uNo;
}
public getName()
{
return this.name;
}
public getUNo()
{
return this.uNo;
}
}
Your Java Code :
List<Data> list = new ArrayList<Data>();
while(rs.next())
{
String uname=rs.getString(1);
int no = Integer.parseInt(rs.getString(2));
list.add(new Data(uname, no));
}
request.setAttribute("myList",list);
Your Jsp Code:
<%
try
{
DataForm d= new DataForm();
List<Data> list = new ArrayList<Data>();
if(request.getAttribute("myList")!=null)
{
list = (List<Data>)request.getAttribute("myList");
for(Data data in list)
{
%><tr><td><%out.println(data.getName());%></td>
<td><%out.println(data.getUNo());%></td></tr><%
}
}
else
{
out.print("No Data Found..");
}
}
catch(Exception e){e.printStackTrace();}
%>
Now, coming to the Logic Tag Library.
This is what you will need to make the following modifications:
i) In your jsp, you may do this:
Add these lines in the beginning of your jsp:
<%# taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<%# taglib uri="http://struts.apache.org/tags-logic" prefix="logic"%>
Use these to write your data:
<logic:present name="myList">
<logic:iterate name="myList" id="myListId">
<tr>
<td><bean:write name="myListId" property="name"/></td>
<td><bean:write name="myListId" property="uNo"/></td>
<tr>
</logic:iterate>
</logic:present>
<logic:notPresent name="myList">
No Data found.
</logic:notPresent>
Ideally, I would have used JSTL to show the list of User/No.
I have not tested this code. But it delivers what I have in mind.

Error passing ArrayList from Servlet to JSP

I have an important question but firs sorry for my english, I only know the basic. Well my problem is that I have an error passing an ArrayList from a servlet to jsp page:
<% ArrayList<Usuario> u= (ArrayList<Usuario>)session.getAttribute("listado");%>
<table align="left" cellpadding="0" cellspacing="1">
<tr bgcolor="blue">
<td>Usuario</td><td>Nombre</td>
<td>Apellido</td><td>Clave</td>
</tr>
<% for(int i=0;i<u.size();i++){ %>
<% Usuario usuario = u.get(i); %>
<tr>
<td> <%= usuario.getUsuario() %></td>
<td> <%= usuario.getNombre() %></td>
<td> <%= usuario.getApellido() %></td>
<td> <%= usuario.getClave() %></td>
</tr>
<%} %>
</table>
That's how I'm doing this but I receive an error in:
<% for(int i=0;i<u.size();i++){ %>
What I'm doing wrong? also my servlet Method is like this:
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
RequestDispatcher rd;
try {
Connection cn = MySQLConnection.obtenerConexion();
String sql = "select * from tb_usuario";
PreparedStatement ps = cn.prepareStatement(sql);
ResultSet rs = ps.executeQuery();
ArrayList<Usuario> listado = new ArrayList<Usuario>();
while (rs.next()){
Usuario usu = new Usuario(rs.getString(1),rs.getString(2),rs.getString(3),rs.getString(4));
listado.add(usu);
}
request.setAttribute("listado", listado);
request.getRequestDispatcher("/listado.jsp");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
I hope you could help me!
You should not use scriptlets in your JSP. You should use EL and tags in your JSP.
e.g.
${listado}
You are setting the variable in request object while retrieving from session, which is not present hence the issue.
You are setting the attribute in doPost as below":
request.setAttribute("listado", listado);
You are retrieving the attribute in your JSP as below":
<% ArrayList<Usuario> u= (ArrayList<Usuario>)session.getAttribute("listado");%>
Please use the same scope session or request in both places.
you are setting the value in to request scope
request.setAttribute("listado", listado);
but then trying to access it in session scope.
session.getAttribute("listado");
due to this u might get a null pointer exception in
u.size()...
try to access it in request scope
request.getAttribute("xxxxxx")
try to avoid adding java code inside JSP whihc is a bad practice. use EL and JSTL instead. you can to the casting part inside the code too..
scriptletsare discouraged to use in ajsp page, use JSTL tags instead. use c-foreach tag to iterate over your arrayList in your jsp page. and you are setting an attribute in a request scope and trying to get it in session scope in your jsp.
heres the link which explains c-foreach tag

Categories

Resources