File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes value from servlet to dropdown in jsp Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "value from servlet to dropdown in jsp" Watch "value from servlet to dropdown in jsp" New topic
Author

value from servlet to dropdown in jsp

Nitin Belaram
Ranch Hand

Joined: Mar 24, 2009
Posts: 67
I want values in dropdown of my jsp page

JSP page :



2 :values are generating via method and the output is getting printed on console. This method is defined in a java class UserDetails.java



The servlet


The values for listOfDepts are not getting printed in servlet.

Please help me to get value in drop down


Shailesh Narkhede
Ranch Hand

Joined: Jul 10, 2008
Posts: 368
Nitin,

Actually not getting problem,
but one thing I noticed that, on jsp line number 03 gettting as
Vector dept_Vect = (Vector)request.getAttribute("dept_Vect");

but while setting in listAllDepartments method you are setting at line number 29

request.setAttribute("dept_Vect", listOfDepts);

Thanks,
Shailesh
Nitin Belaram
Ranch Hand

Joined: Mar 24, 2009
Posts: 67
Shailesh Narkhede wrote:Nitin,

Actually not getting problem,
but one thing I noticed that, on jsp line number 03 gettting as
Vector dept_Vect = (Vector)request.getAttribute("dept_Vect");

but while setting in listAllDepartments method you are setting at line number 29

request.setAttribute("dept_Vect", listOfDepts);


I had a form in which department is dropdown
i want value in that dropdown from database for which i had defined method listAllDepartments() in UserDetails.java page
where it is generating the output,

But when i am setting this value for retreival in dropdown i am not getting value.

In servlet i am calling method listAllDepartments().
Dawn Charangat
Ranch Hand

Joined: Apr 26, 2007
Posts: 249
Nitin, lets take this one staep at a time, okay...

Fine, the first thing :

Change the signature of your method: listAllDepartments(String sql, Object dept_Vect, HttpServletRequest request)

change it to :

public static Vector listAllDepartments() throws DaoException, ClassNotFoundException {

// put the existing code here itself

return dept_Vect;
}

Why because, you are not using any of the parameters passed to this method, for the logic in the method. Okay... did you understand that ?
Moreover, you shouldnt ideally use the Http objects within other classes other than servlets [generally a bad practice, thats all]. Instead of returning a string from this method, return the vector itself [dont set it in the session in the method]

Once you have done that, change the way the doPost method in the servlet is coded.

// add this:
Vector listOfdepartments = UserDetails.listAllDepartments();
request.setAttribute("dept_Vect", listOfdepartments);

Now try it out, and let me know what you see.
Nitin Belaram
Ranch Hand

Joined: Mar 24, 2009
Posts: 67
Dawn Charangat wrote:Nitin, lets take this one staep at a time, okay...

Fine, the first thing :

Change the signature of your method: listAllDepartments(String sql, Object dept_Vect, HttpServletRequest request)

change it to :

public static Vector listAllDepartments() throws DaoException, ClassNotFoundException {

// put the existing code here itself

return dept_Vect;
}

Why because, you are not using any of the parameters passed to this method, for the logic in the method. Okay... did you

understand that ?
Moreover, you shouldnt ideally use the Http objects within other classes other than servlets [generally a bad practice, thats all]. Instead of returning a string from this method, return the vector itself [dont set it in the session in the method]

Once you have done that, change the way the doPost method in the servlet is coded.

// add this:
Vector listOfdepartments = UserDetails.listAllDepartments();
request.setAttribute("dept_Vect", listOfdepartments);

Now try it out, and let me know what you see.


I am getting error

HTTP Status 500 -

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

type Exception report

message

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

exception

java.lang.NullPointerException
mydao.UserDetails.listAllDepartments(UserDetails.java:220)
mycontroller.RegisterServlet.doPost(RegisterServlet.java:63)
javax.servlet.http.HttpServlet.service(HttpServlet.java:710)
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
Mohamed Inayath
Ranch Hand

Joined: Nov 22, 2004
Posts: 124
Change accordingly :
You need not have to pass the paramters.
As you are not using any of those effectively.



Is this called before the JSP is called or how is that..
Nitin Belaram
Ranch Hand

Joined: Mar 24, 2009
Posts: 67
Mohamed Inayath wrote:Change accordingly :
You need not have to pass the paramters.
As you are not using any of those effectively.



Is this called before the JSP is called or how is that..


Flow of application:

I had a jsp in which details are filled ,In this jsp page Department is drop down in which i want value from database for which i defined listAllDepartments() .

After filling the details and on submit button data are inserted into database via servlet

Problem : I am not getting value in dropdown from database.

Data is getting inserted in database with select value in department field
Mohamed Inayath
Ranch Hand

Joined: Nov 22, 2004
Posts: 124
All this fine.

What is the first screen you are using?

See currently what is happening is the servlet which is getting the dropdown data is called after the JSP is displayed.
I believe JSP is displayed first and user has to key in the required details and then submit to Register Servlet.

But Drop down list is retrieved during register servlet.
And more over you dont have a Controler Servlet where can pocess the required data for the view.



Nitin Belaram
Ranch Hand

Joined: Mar 24, 2009
Posts: 67
Mohamed Inayath wrote:All this fine.

What is the first screen you are using?

See currently what is happening is the servlet which is getting the dropdown data is called after the JSP is displayed.
I believe JSP is displayed first and user has to key in the required details and then submit to Register Servlet.

But Drop down list is retrieved during register servlet.
And more over you dont have a Controler Servlet where can pocess the required data for the view.





Yes You had got the flow which is happening,

But i want value dropdown value before the submit button called register Servlet.

Please do let me know what changes i must do .

thanks
Mohamed Inayath
Ranch Hand

Joined: Nov 22, 2004
Posts: 124
1. Have a Controller Servlet. - read some materials on this.
2. Controller Servlet has to retreive all the data required for the view to be displayed.
In your case for ex : Data for the dropdown.
3. After retrieving the necessary data for the view dispatch the request to the view.
4. View then will be displayed
5. Then it has to be submitted to the Register Servlet.

Nitin Belaram
Ranch Hand

Joined: Mar 24, 2009
Posts: 67
Mohamed Inayath wrote:1. Have a Controller Servlet. - read some materials on this.
2. Controller Servlet has to retreive all the data required for the view to be displayed.
In your case for ex : Data for the dropdown.
3. After retrieving the necessary data for the view dispatch the request to the view.
4. View then will be displayed
5. Then it has to be submitted to the Register Servlet.



Can you suggest how to write a controller servlet for my drop down field.

and how will the flow then will be

Now my jsp calling Registerservlet for entering data in database
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Nitin, rather than us just repeating information that is widely available else where I suggest you do a little research before proceding. For example this is a very useful resource to go through. And this explains the interactions you need quite well.

(Also a small point - why are you using a Vector? Do you need acess to your Collection to be synchronized?)


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Dawn Charangat
Ranch Hand

Joined: Apr 26, 2007
Posts: 249
and please stop quoting everything the others type... the volunteers are not visually challenged.

Your code will work only if the servlet gets invoked before the JSP. Because, it is from the servlet the data is being populated.
After the listAllDepartments() method call, when the control reaches the servlet, can you verify if the vector object returned has the
department information you need in the drop down list ?
Nitin Belaram
Ranch Hand

Joined: Mar 24, 2009
Posts: 67
Dawn Charangat wrote:and please stop quoting everything the others type... the volunteers are not visually challenged.

Your code will work only if the servlet gets invoked before the JSP. Because, it is from the servlet the data is being populated.
After the listAllDepartments() method call, when the control reaches the servlet, can you verify if the vector object returned has the
department information you need in the drop down list ?




How can i populate servlet befor jsp and how data will be inserted into database,
DO i need to create one more servlet which will handle insertion of data
Dawn Charangat
Ranch Hand

Joined: Apr 26, 2007
Posts: 249
Dude... you don't understand it do you ? I asked... sorry requested you to stop quoting unnecessarily !!!

In the web.xml file, where you map your URL to servlets/JSPs, you map your URL to this servlet, fill your vector object, and then forward the request from there to the JSP page.

[I have a strong feeling that your next question is going to be about mapping the URLs to servlets]
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10329
    
    8

Nitin,

Please try and use the "Post Reply" button on the bottom left of the page to post your replies.


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: value from servlet to dropdown in jsp