0

This is a dropdownlist in which multiple values can be selected -- http://paste.ubuntu.com/7845559/ A loop has been used to create the options in the list accessing values from database.

This is the javascript function I am trying to use to read the multiple datas selected in the list -- http://paste.ubuntu.com/7845571/ I am not sure if the variable str in the javascript function is storing the values from dropdownlist. My questions are--

  1. How can I assign the javascript variable str to java string variable ?
  2. After doing 1 how can I send the java variable to a servlet ? I need to send this information to servlet to update infos in database.
  3. If this approach is wrong which one is a better way to access datas from the list and send them to servlet ? A simple code snippet will be very helpful.
Pavel
  • 138
  • 2
  • 4
  • 16
  • It would help if you'd explain what you're trying to achieve. From what I understand, you're trying to use `java` insted of this `javascript`. This isn't possible because this `javascript` code is manipulating dom element in your browser and AFAIK currently this is possible only with `client side javascript`. – Marko Gresak Jul 24 '14 at 04:57
  • 1
    Why do you want to assign javascript variable to java variable, you can send the data contained in your JS variable using ajax, and receive it using jsp/servlet (which has a java variable), or you could directly send parameters using GET/POST to the servlet – Mustafa sabir Jul 24 '14 at 05:06
  • I am trying to send the selected datas in the dropdownlist to the servlet. Is there any function like getParameter() available so that I can get the values of the list from the servlet ? – Pavel Jul 24 '14 at 05:45
  • Check http://scn.sap.com/thread/26524, it should help you. – Tanveer Shaikh Jul 24 '14 at 05:49

1 Answers1

0

You are mixing up client-side and server-side code. A JSP is can be thought of as 2 programs intermingled. The program which runs on the server (anything that is inside scriptlets <% or output by custom tags) and the program which is executed by the user's browser when they receive the response.

Things the Server-side code can do:

  • Access the database
  • Create Java variables (which are stored as request or session attributes)
  • Interact with other Java objects
  • Output HTML and in-line JavaScript

Things the Client-side code can do:

  • Create and manipulate JavaScript variables
  • Dynamically edit the HTML DOM
  • Submit Forms

Things the Client-side code can't do:

  • Access the database
  • Create Java variables
  • Interact with other Java objects

Therefore, your Server-side code should output the HTML code for a Form, and this Form can be filled out by the user, submitted and the data will be sent back to a Servlet on the server which can access the database, make the changes and generate more HTML to send back to the user (eg validation error or thank-you page).

Please see this question (although it is PHP, the ideas are the same):

What is the difference between client-side and server-side programming?

Community
  • 1
  • 1
Si Kelly
  • 703
  • 3
  • 9