how to get variable value from javascript to jsp - java

i have created dynamic rows(textboxes) in jsp by using javascript. After giving the values to those fields how can i retrieve the values into my result jsp page.

You might be overlooking on something. What is the exact requirement?
I doubt that is even possible because
JavaScript executes on the client side after JSP page is already compiled and sent to the browser as response.

While creating your rows in js assign a unique name to each cell (something like 'text_[i]_[j]' if you are using two loops the outer running on i and the inner running on j)
Submit the page
Use request.getParameter("text_[i]_[j]") to retrieve the value.

Related

Several forms in a JSP page: how to avoid duplicated ids and tedious parameter processing at the same time?

I have a JSP page that displays a list of items, each one accompanied by a form that allows the user to edit the corresponding item's data. The changes made by the user are processed by a servlet.
Right now, the JSP page produces invalid HTML, because every input of every form share the same id. I can add some suffix to every id, but then the code at the servlet side that gathers all the parameters is not as simple as calling request.getparameter("constantValue") anymore.
I can use request.getParameterNames() and guess which input belongs every id to by its prefix, but I think it looks ugly and it's probably bad code.
Am I missing a better solution?
Right now, the JSP page produces invalid HTML, because every input of
every form share the same id
Id's are not considered here, as request.getparameter("Name") requires the attribute name . So make sure that you have unique name for the inputs in various form.
Several forms in a JSP page: how to avoid duplicated ids and tedious
parameter processing at the same time?
I believe you have submit button for each form. so that doesnt matter if you are not submitting to the servlet

How to assign javascript string to java string?

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--
How can I assign the javascript variable str to java string variable ?
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.
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.
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?

How to iterate java arraylist with javascript or jquery in jsp

I have Arraylist which is comming from controller to jsp. Now I have Arraylist in jsp.
Assume that Arraylist contains 100 records.Now I need to show only 10 records initially.
I am providing the show more link in the jsp,when user click on the link,I need to show
another 10 records should be append to previous one.
Here I don't want to use any ajax call, why because I already whole List in jsp.
I want to use js or jquery for this requirement.
Please provide me any suggestions,how to achieve this.
Thanks in advance.
Please understand what is on server side and what is on client side. The array list that you have is on server side in the servlet. Remember that All jsp's gets compiled to servlets.
You can use javascript or jQuery on client side only.
Now there are two scenarios how you implement your pagination.
Approach 1. Get all the records and than use jQuery etc to navigate the pages. There will be no server call.
Approach 2) Every time user hits show more you make server call and get the records and display.
which approach you choose depends on your application.

How to render HTML form fields and run Java code in background simultaneously in JSP

I have made a JSP page that has form fields and java code in scriplets .I have imported the java code in JSP page and made object to call the function of that java class.
When i execute the JSP, the JSP page remains blank and only when all the Java code has been executed then the html form fields are rendered on the view.
I have two questions:
1.)How can i quick this process and how can i show something like "Loading..." until that html is rendered
OR
2) How can I make HTML fields to appear as soon as JSP page is invoked while letting all the java code simultaneously running in background.(The java class whose object is made in JSP calls webservices and this may take time, meanwhile i want user should start inputing in the form fields).But user cannot see any form fields until the whole Java code has executed.
KINDLY HELP!
You can display a page first and then to load other content through Ajax.

parameters stability in jsp page

I am working on JSP-servlet application and now I am programming a page which edit information about registrars.
The senario is that there's an ArrayList I send form servlet to JSP page when I load the page. The ArrayList contains information about groups the registrar belongs, the ArrayList is resulted after making multiple SQL statements. When user try to edit some fields and make one required field empty and submit the form, the servlet makes validation and return error to edit page.
The problem I face is that all the groups I sent in the first time fly in the sky. So I have to make connection to DB again and make multiple queries to get the groups again and send it back to JSP page.
Is there's another simple way to make arrayList stable in JSP page ?
EDIT
Here's the code which I make the scope of the ArrayList in the session.
<c:set var="userGroups" value="${userGroups}" scope="session"></c:set>
Either store it in the session or just live with it. I really don't see any issues with that. If the concrete problem is that you have to copypaste the same code again or that the whole code is ugly to have in a Servlet class, then just refactor/hide that into an useable DAO class which you import/call/reuse in the Servlet the usual Java way.
Update: as per your update, this doesn't make sense. You just need to change your servlet code from
request.setAttribute("userGroups", userGroups);
to
request.getSession().setAttribute("userGroups", userGroups);
You don't need <c:set> for this.

Categories

Resources