• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

value from servlet to dropdown in jsp

 
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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


 
Ranch Hand
Posts: 368
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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);
 
Nitin Belaram
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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().
 
Ranch Hand
Posts: 249
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?)
 
Dawn Charangat
Ranch Hand
Posts: 249
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 249
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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]
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nitin,

Please try and use the "Post Reply" button on the bottom left of the page to post your replies.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic