aspose file tools*
The moose likes Servlets and the fly likes Value in dropdown from servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Value in dropdown from servlet" Watch "Value in dropdown from servlet" New topic
Author

Value in dropdown from servlet

Nitin Belaram
Ranch Hand

Joined: Mar 24, 2009
Posts: 67
I want department field value in dropdown in my jsp page ,But values are not coming in drop down.

The JSP is


<%
Vector dept_Vect = new Vector();
%>


<TD> DEPARTMENT :  
<SELECT NAME="department" tabindex ="3">
<Option value ="select"> Select Department --</option>
<% for(int i =0 ;i < dept_Vect.size(); i++)
{%> <option value="<%=dept_Vect.elementAt(i) %>"><%=(String)dept_Vect.elementAt(i) %>
out.println(dept_Vect + "value of dept");
</option>
<%}%>


and the servlet is


public New_USER find(String sql, Object dept_Vect) throws DaoException, ClassNotFoundException {
Connection connection =null;
PreparedStatement preparedStatement = null;
ResultSet resultSet =null;
New_USER newuser = null;
try{
newuser = new New_USER();
connection = connectsql.getConnection();
System.out.println(connection);
preparedStatement = connection.prepareStatement(SQL_DEPARTMENT);
System.out.println(SQL_DEPARTMENT);
System.out.println(preparedStatement + "checking for department");
resultSet =preparedStatement.executeQuery();
System.out.println(resultSet);
// dept_Vect.removeAllElements();
while(resultSet.next()){


String nextloc = (String)resultSet.getString(1);
dept_Vect.addElement(nextloc);





Database query is :

private static final String SQL_DEPARTMENT =
"select deptname from base_department" ;


Please suggest what i am doing wrong

Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Does your query return any results?


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Nitin Belaram
Ranch Hand

Joined: Mar 24, 2009
Posts: 67
Paul Sturrock wrote:Does your query return any results?



When i am running the query on query browser it is giving correct output , i want that output in my dropdown
Mohamed Inayath
Ranch Hand

Joined: Nov 22, 2004
Posts: 124


Will only create the vector.

From where you are getting the values.
Always the Vector will be empty right.
Nitin Belaram
Ranch Hand

Joined: Mar 24, 2009
Posts: 67
Mohamed Inayath wrote:

Will only create the vector.

From where you are getting the values.
Always the Vector will be empty right.




I had created vector in jsp page and defining method to get value in dropdown from database via using servlet
Mohamed Inayath
Ranch Hand

Joined: Nov 22, 2004
Posts: 124
Nitin Belaram wrote:
I had created vector in jsp page and defining method to get value in dropdown from database via using servlet


What do you mean by this..

What ever you add the code in the scriplet will go in the service method of the generated servlet.
And your vector creation is local.

Instead you store the value in Session after generating in the Servlet and get the values in the JSP.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Hi,

please look Mohamed Inayath post . you have to set the list which contains the values into the scope(request is prefere)on servlet , then get the list from scope and display on jsp
Dawn Charangat
Ranch Hand

Joined: Apr 26, 2007
Posts: 249
Nitin, what you are doing is correct, except for that glitch pointed out by Mohamed.

You are getting the data alright in your servlet after the database query, but you are not sending that data to the JSP page... Just by using the same name [dept_Vect] in both the servlet and JSP will not make sure that the data is transfered... You will have to probably store this information from the servlet to a request object or session object :

eg:

Vector dept_Vect = (Vector)request.getAttribute(the_name_you_gave_to_set_in_request); // if you are setting it in request

OR

Vector dept_Vect = (Vector)session.getAttribute(the_name_you_gave_to_set_in_session); // if you are setting it in session

try this out and see if your drop-down is populated correctly.
Mohamed Inayath
Ranch Hand

Joined: Nov 22, 2004
Posts: 124
If the values are static then try getting from the database once and use try using mutliple times.
If the values are dynamic and you are sure it will change every request then use request scope.

It really depends upon your requirement.
Nitin Belaram
Ranch Hand

Joined: Mar 24, 2009
Posts: 67
Dawn Charangat wrote:Nitin, what you are doing is correct, except for that glitch pointed out by Mohamed.

You are getting the data alright in your servlet after the database query, but you are not sending that data to the JSP page... Just by using the same name [dept_Vect] in both the servlet and JSP will not make sure that the data is transfered... You will have to probably store this information from the servlet to a request object or session object :

eg:

Vector dept_Vect = (Vector)request.getAttribute(the_name_you_gave_to_set_in_request); // if you are setting it in request

OR

Vector dept_Vect = (Vector)session.getAttribute(the_name_you_gave_to_set_in_session); // if you are setting it in session

try this out and see if your drop-down is populated correctly.





I am adding Vector dept_Vect = (Vector)request.getAttribute(deptname);


it is giving error

1: The method addElement(String) is undefined for type Object.

2: Duplicate local variable dept_Vect

3: deptname and request cant be resolved




public New_USER find(String sql, Object dept_Vect) throws DaoException, ClassNotFoundException {
Connection connection =null;
PreparedStatement preparedStatement = null;
ResultSet resultSet =null;
New_USER newuser = null;
try{
newuser = new New_USER();
connection = connectsql.getConnection();
System.out.println(connection);
preparedStatement = connection.prepareStatement(SQL_DEPARTMENT);
System.out.println(SQL_DEPARTMENT);
System.out.println(preparedStatement + "checking for department");
resultSet =preparedStatement.executeQuery();
System.out.println(resultSet);
// dept_Vect.removeAllElements();
while(resultSet.next()){



String nextloc = (String)resultSet.getString(1);
dept_Vect.addElement(nextloc);

Vector dept_Vect = (Vector)request.getAttribute(deptname);
}


// newuser.setDepartment(resultSet.getString(1));


}
catch (SQLException e) {
throw new DaoException(e);
}
}




Mohamed Inayath
Ranch Hand

Joined: Nov 22, 2004
Posts: 124
Nitin, Can you add the complete code where you are getting the mentioned errors?
Dawn Charangat
Ranch Hand

Joined: Apr 26, 2007
Posts: 249
Wonderful.... now that I look back, I think I might have confused you with a few things.

You need to put the dept_Vect object you created in the SERVLET to request or session.

eg: request.setAttribute("dept_Vect", dept_Vect) OR session.setAttribute("dept_Vect", dept_Vect)

and then, in your JSP, you should access it like :

Vector dept_Vect = (Vector)request.getAttribute("dept_Vect") OR Vector dept_Vect = (Vector)session.getAttribute("dept_Vect")

and NOT Vector dept_Vect = new Vector()

is it clear to you now ???
Nitin Belaram
Ranch Hand

Joined: Mar 24, 2009
Posts: 67
Mohamed Inayath wrote:Nitin, Can you add the complete code where you are getting the mentioned errors?


I am getting error where addElement ()is used


Nitin Belaram
Ranch Hand

Joined: Mar 24, 2009
Posts: 67
Dawn Charangat wrote:Wonderful.... now that I look back, I think I might have confused you with a few things.

You need to put the dept_Vect object you created in the SERVLET to request or session.

eg: request.setAttribute("dept_Vect", dept_Vect) OR session.setAttribute("dept_Vect", dept_Vect)

and then, in your JSP, you should access it like :

Vector dept_Vect = (Vector)request.getAttribute("dept_Vect") OR Vector dept_Vect = (Vector)session.getAttribute("dept_Vect")

and NOT Vector dept_Vect = new Vector()

is it clear to you now ???



i am attaching my code and the error:


public New_USER find(String sql, Object dept_Vect, Object request) throws DaoException, ClassNotFoundException {
Connection connection =null;
PreparedStatement preparedStatement = null;
ResultSet resultSet =null;
New_USER newuser = null;
try{
newuser = new New_USER();
connection = connectsql.getConnection();
System.out.println(connection);
preparedStatement = connection.prepareStatement(SQL_DEPARTMENT);
System.out.println(SQL_DEPARTMENT);
System.out.println(preparedStatement + "checking for department");
resultSet =preparedStatement.executeQuery();
System.out.println(resultSet);
// dept_Vect.removeAllElements();
while(resultSet.next()){



String nextloc = (String)resultSet.getString(1);
//dept_Vect.addElement(nextloc);


((Object) request).setAttribute("dept_Vect", dept_Vect) ;
// Vector dept_Vect= (Vector)request.getAttribute(deptname);
}


// newuser.setDepartment(resultSet.getString(1));


}
catch (SQLException e) {
throw new DaoException(e);
}


Here i am getting error:

The method setAttribute(String Object )is undefined for the type Object


The Jsp is


<%@ page language="java" %>
<HTML>
<%@ page import ="java.util.*" %>
<HEAD>

<Title> NEW USER </Title>
<script language = "java script">
</script>

<%

Vector dept_Vect = (Vector)request.getAttribute("dept_Vect");
// Vector dept_Vect = new Vector();
Vector desg_Vect = new Vector();

%>
</HEAD>

<BODY BGCOLOR ="LIGHTGREY">
<form Name ="new_user" Method ="POST" action ="RegisterServlet">

EMP F NAME :     <INPUT type="text" size="20" maxlength="15" name="empfname" > EMP L NAME :           <INPUT type="text" size="20" maxlength="15" name="emplname">
DEPARTMENT :   <SELECT NAME="department" tabindex ="3"> <Option value ="select"> Select Department --</option> <% for(int i =0 ;i < dept_Vect.size(); i++) {%> <option value="<%=dept_Vect.elementAt(i) %>"><%=(String)dept_Vect.elementAt(i) %> out.println(value); </option> <%}%> </SELECT> DESIGNATION :       <Select Name ="designation"> <option value ="select" >Select Designation --</option> <% for(int i = 0;i< desg_Vect.size();i++) { %> <option value ="<%= desg_Vect.elementAt(i) %>"> <%=(String)desg_Vect.elementAt(i) %></option> <%} %> </Select>
EMAIL-ID :            <INPUT type="text" size="20" maxlength="15" name="emailid" > ADDRESS :                <INPUT type="text" size="20" maxlength="15" name="address" >
PROJECT :            <INPUT type="text" size="20" maxlength="15" name="project" > MANAGER :              <INPUT type="text" size="20" maxlength="15" name="manager" >


<input type = "submit" name =Submit VALUE =" SUBMIT" onclick ="RegisterServlet" >
</form>
</BODY>
</HTML>



jsp

Mohamed Inayath
Ranch Hand

Joined: Nov 22, 2004
Posts: 124
Change the code with the below one and update..
Dawn Charangat
Ranch Hand

Joined: Apr 26, 2007
Posts: 249
I presume, that this method [public New_USER find()] is inside a servlet. In that case, you dont have to do :

((Object) request).setAttribute("dept_Vect", dept_Vect) ; //Which infact is wrong, and thats why the compiler complains

You should be using:

request.setAttribute("dept_Vect", dept_Vect) ; // Where "request" is of type HttpServletRequest
Nitin Belaram
Ranch Hand

Joined: Mar 24, 2009
Posts: 67
Mohamed Inayath wrote:Change the code with the below one and update..


After changing code and adding above line i am getting error

type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

org.apache.jasper.JasperException: Exception in JSP: /new_user.jsp:36

33: <td> DEPARTMENT :  
34: <SELECT NAME="department" tabindex ="3">
35: <Option value ="select"> Select Department --</option>
36: <% for(int i =0 ;i < dept_Vect.size(); i++)
37: {%> <option value="<%=dept_Vect.elementAt(i) %>"><%=(String)dept_Vect.elementAt(i) %>
38: out.println(value);
39: </option>


Stacktrace:
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:451)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:373)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:329)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:265)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)


root cause

java.lang.NullPointerException
org.apache.jsp.new_005fuser_jsp._jspService(new_005fuser_jsp.java:80)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:98)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:331)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:329)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:265)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)


note The full stack trace of the root cause is available in the Apache Tomcat/5.5.25 logs.


--------------------------------------------------------------------------------

Apache Tomcat/5.5.25
Nitin Belaram
Ranch Hand

Joined: Mar 24, 2009
Posts: 67
Dawn Charangat wrote:I presume, that this method [public New_USER find()] is inside a servlet. In that case, you dont have to do :

((Object) request).setAttribute("dept_Vect", dept_Vect) ; //Which infact is wrong, and thats why the compiler complains

You should be using:

request.setAttribute("dept_Vect", dept_Vect) ; // Where "request" is of type HttpServletRequest



I am getting error:

request cant be resolved

The method setAttribute(String Object) is undefined for type Object

The Code is


public New_USER find(String sql, Object dept_Vect, Object request) throws DaoException, ClassNotFoundException {
//Vector deptVector = (Vector) dept_Vect;
Connection connection =null;
PreparedStatement preparedStatement = null;
ResultSet resultSet =null;
New_USER newuser = null;
try{
newuser = new New_USER();
connection = connectsql.getConnection();
System.out.println(connection);
preparedStatement = connection.prepareStatement(SQL_DEPARTMENT);
System.out.println(SQL_DEPARTMENT);
System.out.println(preparedStatement + "checking for department");
resultSet =preparedStatement.executeQuery();
System.out.println(resultSet);
// dept_Vect.removeAllElements();
while(resultSet.next()){



String nextloc = (String)resultSet.getString(1);
((Vector) dept_Vect).addElement(nextloc);


request.setAttribute("dept_Vect", dept_Vect) ;
// Vector dept_Vect= (Vector)request.getAttribute(deptname);
}


// newuser.setDepartment(resultSet.getString(1));


}
catch (SQLException e) {
throw new DaoException(e);
}
return newuser;
}



Mohamed Inayath
Ranch Hand

Joined: Nov 22, 2004
Posts: 124
Change the code with the below one and update..


Nitin Belaram
Ranch Hand

Joined: Mar 24, 2009
Posts: 67
Mohamed Inayath wrote:Change the code with the below one and update..




HTTP Status 500 -

--------------------------------------------------------------------------------

I am getting the error

type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

org.apache.jasper.JasperException: Exception in JSP: /new_user.jsp:43

40: <td> DEPARTMENT :  
41: <SELECT NAME="department" tabindex ="3">
42: <Option value ="select"> Select Department --</option>
43: <% for(int i =0 ;i < dept_Vect.size(); i++)
44: {%> <option value="<%=dept_Vect.elementAt(i) %>"><%=(String)dept_Vect.elementAt(i) %>
45: out.println(value);
46: </option>


Stacktrace:
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:451)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:373)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:329)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:265)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)


root cause

java.lang.NullPointerException
org.apache.jsp.new_005fuser_jsp._jspService(new_005fuser_jsp.java:88)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:98)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:331)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:329)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:265)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)


note The full stack trace of the root cause is available in the Apache Tomcat/5.5.25 logs.


--------------------------------------------------------------------------------

Apache Tomcat/5.5.25


servlet code which i use


public void listAllDepartments(String sql, HttpServletRequest request) throws DaoException, ClassNotFoundException {
Connection connection =null;
PreparedStatement preparedStatement = null;
ResultSet resultSet =null;
try{

connection = connectsql.getConnection();
preparedStatement = connection.prepareStatement(SQL_DEPARTMENT);
System.out.println(SQL_DEPARTMENT);
System.out.println(preparedStatement + "checking for department");
resultSet =preparedStatement.executeQuery();
System.out.println(resultSet);

Vector listOfDepts = new Vector();

if(resultSet != null) {
while(resultSet.next()){
listOfDepts.addElement(resultSet.getString(1));
}
}

request.setAttribute("dept_Vect", listOfDepts);

}
catch (SQLException e) {
throw new DaoException(e);
}
}



Mohamed Inayath
Ranch Hand

Joined: Nov 22, 2004
Posts: 124
Have you changed the JSP as well??

Code you post the complete JSP within code tags..
Nitin Belaram
Ranch Hand

Joined: Mar 24, 2009
Posts: 67
Mohamed Inayath wrote:Have you changed the JSP as well??

Code you post the complete JSP within code tags..


jsp



<%@ page language="java" %>
<HTML>
<%@ page import ="java.util.*" %>
<HEAD>

<Title> NEW USER </Title>
<script language = "java script">
</script>


<%

Vector dept_Vect = (Vector)request.getAttribute("dept_Vect");
if(dept_Vect != null) {
dept_Vect = new Vector(); //Just defensive
}


//Vector dept_Vect = (Vector)request.getAttribute("dept_Vect");
// Vector dept_Vect = new Vector();
// Vector desg_Vect = new Vector();

%>
</HEAD>

<BODY BGCOLOR ="LIGHTGREY">
<form Name ="new_user" Method ="POST" action ="RegisterServlet">

EMP F NAME :     <INPUT type="text" size="20" maxlength="15" name="empfname" > EMP L NAME :           <INPUT type="text" size="20" maxlength="15" name="emplname">
DEPARTMENT :   <SELECT NAME="department" tabindex ="3"> <Option value ="select"> Select Department --</option> <% for(int i =0 ;i < dept_Vect.size(); i++) {%> <option value="<%=dept_Vect.elementAt(i) %>"><%=(String)dept_Vect.elementAt(i) %> out.println(value); </option> <%}%> </SELECT> DESIGNATION :       <Select Name ="designation"> <option value ="select" >Select Designation --</option> <% for(int i = 0;i< desg_Vect.size();i++) { %> <option value ="<%= desg_Vect.elementAt(i) %>"> <%=(String)desg_Vect.elementAt(i) %></option> <%} %> </Select>
EMAIL-ID :            <INPUT type="text" size="20" maxlength="15" name="emailid" > ADDRESS :                <INPUT type="text" size="20" maxlength="15" name="address" >
PROJECT :            <INPUT type="text" size="20" maxlength="15" name="project" > MANAGER :              <INPUT type="text" size="20" maxlength="15" name="manager" >


<input type = "submit" name =Submit VALUE =" SUBMIT" onclick ="RegisterServlet" >
</form>
</BODY>
</HTML>
Mohamed Inayath
Ranch Hand

Joined: Nov 22, 2004
Posts: 124
Oops..typo error :

Nitin Belaram
Ranch Hand

Joined: Mar 24, 2009
Posts: 67
Mohamed Inayath wrote:Oops..typo error :



iam getting

List of Departments are empty


but when i am running query on browser
it is giving two record
Mohamed Inayath
Ranch Hand

Joined: Nov 22, 2004
Posts: 124
So it means...
The values you set in the Request scope is getting lost.
Try setting in the session.

Inservlet :

In JSP



Nitin Belaram
Ranch Hand

Joined: Mar 24, 2009
Posts: 67
Mohamed Inayath wrote:So it means...
The values you set in the Request scope is getting lost.
Try setting in the session.

Inservlet :

In JSP



where to put these code in jsp and servlet
Nitin Belaram
Ranch Hand

Joined: Mar 24, 2009
Posts: 67
the error i am getting is

HTTP Status 500 -

--------------------------------------------------------------------------------

type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

org.apache.jasper.JasperException: Unable to compile class for JSP:
JSP FileName:/new_user.jsp
Java FileName:/D:/Gaurav/COE/Projects/.metadata/.plugins/org.eclipse.wst.server.core/tmp1/work/Catalina/localhost/test11//org/apache/jsp\new_005fuser_jsp.java

An error occurred at line: 12 in the jsp file: /new_user.jsp
Duplicate local variable session
9:
10:
11: <%
12: HttpSession session = getSession(false);
13:
14: if(session != null) {
15: Vector dept_Vect = (Vector)session.getAttribute("dept_Vect");

JSP FileName:/new_user.jsp
Java FileName:/D:/Gaurav/COE/Projects/.metadata/.plugins/org.eclipse.wst.server.core/tmp1/work/Catalina/localhost/test11//org/apache/jsp\new_005fuser_jsp.java

An error occurred at line: 12 in the jsp file: /new_user.jsp
The method getSession(boolean) is undefined for the type new_005fuser_jsp
9:
10:
11: <%
12: HttpSession session = getSession(false);
13:
14: if(session != null) {
15: Vector dept_Vect = (Vector)session.getAttribute("dept_Vect");

JSP FileName:/new_user.jsp
Java FileName:/D:/Gaurav/COE/Projects/.metadata/.plugins/org.eclipse.wst.server.core/tmp1/work/Catalina/localhost/test11//org/apache/jsp\new_005fuser_jsp.java

An error occurred at line: 54 in the jsp file: /new_user.jsp
dept_Vect cannot be resolved
51: <TD> DEPARTMENT :  
52: <SELECT NAME="department" tabindex ="3">
53: <Option value ="select"> Select Department --</option>
54: <% for(int i =0 ;i < dept_Vect.size(); i++)
55: {%> <option value="<%=dept_Vect.elementAt(i) %>"><%=(String)dept_Vect.elementAt(i) %>
56: out.println(value);
57: </option>

JSP FileName:/new_user.jsp
Java FileName:/D:/Gaurav/COE/Projects/.metadata/.plugins/org.eclipse.wst.server.core/tmp1/work/Catalina/localhost/test11//org/apache/jsp\new_005fuser_jsp.java

An error occurred at line: 55 in the jsp file: /new_user.jsp
dept_Vect cannot be resolved
52: <SELECT NAME="department" tabindex ="3">
53: <Option value ="select"> Select Department --</option>
54: <% for(int i =0 ;i < dept_Vect.size(); i++)
55: {%> <option value="<%=dept_Vect.elementAt(i) %>"><%=(String)dept_Vect.elementAt(i) %>
56: out.println(value);
57: </option>
58: <%}%>

JSP FileName:/new_user.jsp
Java FileName:/D:/Gaurav/COE/Projects/.metadata/.plugins/org.eclipse.wst.server.core/tmp1/work/Catalina/localhost/test11//org/apache/jsp\new_005fuser_jsp.java

An error occurred at line: 55 in the jsp file: /new_user.jsp
dept_Vect cannot be resolved
52: <SELECT NAME="department" tabindex ="3">
53: <Option value ="select"> Select Department --</option>
54: <% for(int i =0 ;i < dept_Vect.size(); i++)
55: {%> <option value="<%=dept_Vect.elementAt(i) %>"><%=(String)dept_Vect.elementAt(i) %>
56: out.println(value);
57: </option>
58: <%}%>


Stacktrace:
org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:98)
o
The jsp code:

<%
HttpSession session = getSession(false);

if(session != null) {
Vector dept_Vect = (Vector)session.getAttribute("dept_Vect");
if(dept_Vect == null) {
System.out.println("List of Departments are empty");
dept_Vect = new Vector(); //Just defensive
}

}

/* Vector dept_Vect = (Vector)request.getAttribute("dept_Vect");
if(dept_Vect == null) {
System.out.println("List of Departments are empty");
dept_Vect = new Vector(); //Just defensive
}
*/

//Vector dept_Vect = (Vector)request.getAttribute("dept_Vect");
// Vector dept_Vect = new Vector();
Vector desg_Vect = new Vector();

%>




The servlet is


public void listAllDepartments(String sql, HttpServletRequest request) throws DaoException, ClassNotFoundException {
Connection connection =null;
PreparedStatement preparedStatement = null;
ResultSet resultSet =null;
try{

connection = connectsql.getConnection();
preparedStatement = connection.prepareStatement(SQL_DEPARTMENT);
System.out.println(SQL_DEPARTMENT);
System.out.println(preparedStatement + "checking for department");
resultSet =preparedStatement.executeQuery();
System.out.println(resultSet);

Vector listOfDepts = new Vector();

if(resultSet != null) {
while(resultSet.next()){
listOfDepts.addElement(resultSet.getString(1));
}
}
System.out.println(listOfDepts);

HttpSession session = getSession(true);

session.setAttribute("dept_Vect", listOfDepts);

//request.setAttribute("dept_Vect", listOfDepts);
System.out.println(listOfDepts);
System.out.println(resultSet);

}
catch (SQLException e) {
throw new DaoException(e);
}
}


Mohamed Inayath
Ranch Hand

Joined: Nov 22, 2004
Posts: 124
Sorry dont need to have

HttpSession session = request.getSession(false) within JSP


Session is a impicit object..

Discard
HttpSession session = request.getSession(false)
Nitin Belaram
Ranch Hand

Joined: Mar 24, 2009
Posts: 67
Mohamed Inayath wrote:Sorry dont need to have

HttpSession session = request.getSession(false) within JSP


Session is a impicit object..

Discard
HttpSession session = request.getSession(false)



giving error : dept_Vect cant be resolved

<td> DEPARTMENT :  
<SELECT NAME="department" tabindex ="3">
<Option value ="select"> Select Department --</option>
<% for(int i =0 ;i < dept_Vect.size(); i++)
{%> <option value="<%=dept_Vect.elementAt(i) %>"><%=(String)dept_Vect.elementAt(i) %>
out.println(value);
</option>
<%}%>
Mohamed Inayath
Ranch Hand

Joined: Nov 22, 2004
Posts: 124
Nitin Belaram
Ranch Hand

Joined: Mar 24, 2009
Posts: 67
Mohamed Inayath wrote:


Again getting error


List of Departments are empty
Mohamed Inayath
Ranch Hand

Joined: Nov 22, 2004
Posts: 124
Hey,

Is it you are calling JSP and then submitting the form values to the Servlet.
JSP-->Servlet.

If this is true then yes it will always return empty list.

Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

So presumably you have not set it in the Session (do you need this Vector to be session scoped?).

Stick a break point in your code where the Vector is put in the session, see what is happening.

Nitin Belaram
Ranch Hand

Joined: Mar 24, 2009
Posts: 67
Mohamed Inayath wrote:Hey,

Is it you are calling JSP and then submitting the form values to the Servlet.
JSP-->Servlet.

If this is true then yes it will always return empty list.



My value are getting printed in console,But they are not visible in dropdown.

jsp is:





the page on which method is defined : UserDetails.java

Method for getting data from database :

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61430
    
  67

Please be sure to use code tags when posting code to the forums. Unformatted code is extremely hard to read and many people that might be able to help you will just move along to posts that are easier to read. Please read this for more information.

You can go back and change your post to add code tags by clicking the button on your post.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Nitin Belaram
Ranch Hand

Joined: Mar 24, 2009
Posts: 67
Bear Bibeault wrote:Please be sure to use code tags when posting code to the forums. Unformatted code is extremely hard to read and many people that might be able to help you will just move along to posts that are easier to read. Please read this for more information.

You can go back and change your post to add code tags by clicking the button on your post.




I want declare variable deptList ----done in my servlet
Please let me know method to
1. Fetch list from dao method and save in deptList
2 . set deptList in request

so i can get value in drop down of jsp page
Mohamed Inayath
Ranch Hand

Joined: Nov 22, 2004
Posts: 124
Nitin,

Is your JSP called first or the Servlet?

From the code what you have given here shows that JSP will be displayed first and then the Servlet.
In this case you will never get the drop down displayed.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Value in dropdown from servlet