This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
You must be getting ClassCastException. If you carefully observe in the servlet you have an ArrayList of ArrayList but in the jsp you are trying to access the list as just a list of Strings and that might be the reason why you are seeing no output. Alternatively you can have one class for your Resultset data- say MyClass and each of the attributes are the columns from the resultset. And then you can have an arraylist declared as ArrayList<MyClass>.
Also try to remove the Java code dangling withing the JSP- You might want to make use of JSP tag library for traversing through the lists.
as far as separate class for result set,i am new to java and quite apprehensive regarding making so many classes ,i think i will get confuse :P i know you are telling me the correct approach but can i fix this problem and then make my code better by making separate classes.
and can you give me some example how to use tag library for traversing through the lists.
You have to typecast it to ArrayList and then traverse. You can add an instanceof check before typecasting and if the instanceof fails then you can tell the user that there was some issue in retreiving the data. It will not affect if you have too many classes. It will only help you modularize your application.
For JSTL you can visit this: http://jstl.java.net/ Its basically set of JSP tags which you can use instead of these Java code.
Why use two ArrayList instances here?
Try with one array list instance, then you may populate the list as you need. Also move your request dispatcher code out of the result set access code.
And you better off populating the appropriate object with the data and add it to the array list then retrieve them in the JSP. As already noted move all the Java code from the JSP instead use the JSTL, EL , standard.custom actions for data manipulations in JSP.
In order to use JSTL
i have added <%@taglib prefix="c" uri="http://java.sun.com/jstl/core"%>
but its showing and error:
"The absolute uri:http://java.sun/jstl/core cannot be resolved in either web.xml or the jar files deployed with application"
i have already added two jar files
jstl-impl-1.2.jar and jstl-api-1.2.jar
Bhardwaj Shweta wrote:
and servlet is working fine
but in my jsp page its showing value of String s6 = rs.getString("priority") in all the fields..
error is in my logic
You are using the same variable for all the values in the list and consequently the variable will end up having the value of the last field. So you need not use a variable instead just do an get on the list.
Thanks for replying
umm can you please give some example.
if i have <input type="text" name="pname" id="pname" size="25" value="<%=##%>" disabled>
<input type="text" name="remarks" size="25" value="<%=##%>" disabled>
how should i retrieve respective value from list object??
As I said encapsulate all the fields for a given record in an object (add it to the list) and access that object in the JSP. This way you can have many records retrieved easily in the JSP. But with this code, retrieve the data to different variables(since this is only for one record you are using) for each field and use them in the HTML elements.
Since you are learning the JSTL etc... you can re-factor the code to use them once you get this to work.
With the current implementation- You can declare different variables like mystring for each column in the resultSet and then use them alongside the html code. I cant think of other way with the current implementation. The one i suggested using the list.get might not work
Vijitha Kumara wrote:As I said encapsulate all the fields for a given record in an object (add it to the list) and access that object in the JSP. This way you can have many records retrieved easily in the JSP. But with this code, retrieve the data to different variables(since this is only for one record you are using) for each field and use them in the HTML elements.
This is a better approach and the code thus written is more readable. And I remember suggesting this.