Following is a form in a resultSet loop. So, there might be multiple forms according to range of results.
/* RESULT SET LOOP STARTED with `i` as iterator running from 1 to 5 */
<form action='Jaga' method='post' >
<input name='input-<%=i%>' />
</form>
/* RESULT SET LOOP ENDED */
So, on form-submission, Jaga Servlet receives information. How do i get to know which 'input-iterator' combination was used from which form.
request.getParameter('here');
What do i fill in-place of 'here' in Jaga Servlet to get correct input box value from correct form?
If you take a look at
https://docs.oracle.com/javaee/6/api/javax/servlet/ServletRequest.html#getParameter(java.lang.String)
You can see that it returns a String or null if parameter doesn't exist, so you can simply make a for loop and check for the first not null return . Something like:
for(int i=1;i<=5;i++)
if(request.getParameter("input-"+i)!=null)
// handle stuff
EDIT: For getting the parameter names, try:
PrintWriter out = response.getWriter();
Enumeration<String> parameter = request.getParameterNames();
while(parameter.hasMoreElements())
out.println(parameter.nextElement());
Related
I'm building my first web app in Java. I came across this problem. When I have a get attribute like ?page=2 correctly submitted it goes missing after calling another get request. How can I keep the first one and append another one? Here are pics that help clear out my question
Before
After
Desired
Here are code snippets of my forms in .jsp page. This is used to sort the table with passing a column name:
<form action="GetUsersServlet" method="get">name
<input type="hidden" name="column" value="name">
<button class="sort" type="submit"></button>
</form>
How can I append the value to existing ?page=x attribute?
Use URLSearchParams method set on event click and set window.location.search to parsed params.
document.querySelector("button").onclick = () => {
const urlParams = new URLSearchParams(window.location.search);
urlParams.set("page", 2)
window.location.search = urlParams.toString()
}
I'm totally new in jsp/servlet and working with an application that I have many jsp pages and servlet.
In the first jsp page I choose a customer.
<select id ="sel" name="customer">
<option>customer1</option>
<option>customer2</option>
<option>customer3</option>
<option>customer4</option>
</select>
In the second jsp page it shows me the menu related to this customer(for example I choose configuration and goes to the third jsp page )
<% HttpSession session = request.getSession(true);
String chos_cust=request.getParameter("customer");
session.setAttribute("cust_menu",chos_cust); %>
....
<%= session.getAttribute("cust_menu")%>
In the third jsp page I choose cateories related to this customer(there are many categories for each customer)
An sql query runs here to show categories as radio buttons:
<input type="radio" name="chos_gr" value="${groups.group_name}"checked > ${groups.group_name}
In the forth jsp page according to the chosen category it will show subcategories. In this jsp page I have three div
in html form with hidden type.In the first div list of products has been shown , the second div is for adding a product and in the last div products can be deleted.
String config_gr = request.getParameter("chos_gr");
session.setAttribute("config_menu",config_gr);
I have used a servlet to do these operations(add, delete). It works well but when I add or delete a product and use requestDispstcher/forward in servlet to back to the last jsp to see the result(list of products) it
shows null for category and nothing in list of products.
If I should return the value of category in the servlet to the last jsp?
Could someone tell me what the problem is?
I can't add a comment for this post. As #underdog suggested I used ${sessionScope.config_menu} in the last jsp and it still shows null for category. It's weird because I get the customer value when I return to the last jsp to see the result but nothing for category.
You are adding all the object/values in a session attribute. With request dispatcher you forward the flow to a jsp with request & response objects. The request object contains no info of the customer you have set in the session object.
In the jsp try accessing the values from the session scope.
${sessionScope.config_menu} would give you the desired output.
Instead of using a response.sendRedirect(url), consider using following:
RequestDispatcher dispatcher = request.getRequestDispatcher(url);
dispatcher.forward(request,response);
Above code with the getRequestDispatcher() will allow you to include your calling servlet's request by using dispatcher.forward, which will include the session attributes you were missing.
[NOTE: OP was already doing the above]
[UPDATE]
Based on the way you were suggesting to code certain parameters in <input type="hidden" name="myfield"> fields, I suspect, you did nothing else to store the values. Which means, the first time around, they might have been set using the parameters from the previous request, but when using the dispatcher.forward, this did not happen.
For fields to be initiated with the appropriate value, they require to be made available either through request parameters (e.g. GET parameters, encoded in the URL), or otherwise set within the JSP itself trough <input type="hidden" value="${whatever}" name="myfield"> or similar code. I am also missing that part of the code to be able to validate.
Using a dispatcher.forward(), you might not have required parameters set.
I get list on my jsp using
<%List selectedArray = (List) session.getAttribute("clist");%>
is [4,5].And I am sending this list to javascript using hidden variable in jsp
<input type='hidden' id="agencycontactid" name="agencycontactid" value="<%=selectedArray%>" />
and i am taking this in javascript var abc=$('#agencycontactid').val(); .
I want to send this abc to servlet using ajax call that is through data.And i want this list in simple array format in servlet.
Please help me.
Thanks
If you wanna pass an actual array (ie. an indexed array) then you can do:
$.post('/url', {'someKeyName': ['value','value']});
You can also build a param string by looping other the data (in my case a multi-select)
$(".choosenItems option").each(function() {
chosenStr = chosenStr + "&chItems=" + $(this).val();
});
so if you create a queryString of
...?name=Fred&name=Joe&name=Sally
then in your servlet you can do
String names[] = request.getParameterValues ("name");
I have an ArrayList of a class Room. I need to send it from a jsp to a servlet.
It seems the only way an html or a jsp can send values to a servlet is via a form, the method I tried was to pass it as a hidden parameter as follows:
<input type="hidden" name="allRooms" value="<%=request.getAttribute("allRooms") %>" />
But in the servlet to which i submit this form I get a compile error "String cannot be converted to List" for the following:
List<Room> allRooms=(List<Room>)request.getParameter("allRooms");
Just converting the parameter to an Object type first and then converting it to a List as shown below gives the same exception but this time as a Runtime Exception:
Object a=(Object)request.getParameter("allRooms");
List<Room> allRooms=(List<Room>)a;
Is there any method to pass the List to the servlet or I will have to set it as a session variable in the JSP ?
Thanks in advance.
Is there any method to pass the List to the servlet or I will have to set it as a session variable in the JSP ?
Use session.That is one best solution.
There is no way to represent an ArrayList in HTML to send via html form. Use session instead.
I think if you pass the following params, and array is formed in servlet side
param[0]=ss , param[1]=ssw
You could do this.
List<String> roomParams =(List<String>)request.getParameter("param");
But to make this.
List<Room> allRooms=(List<Room>)request.getParameter("allRooms");
That I think is not possible, so you should use Session attributes
session.setAttribute("allRooms", new ArrayList<Room>());
I believe you should not be sending the List to Servlet directly and should look out for other options.
How are you generating the ArrayList on the browser page? If it is generated from a multi-select element from UI, then you can access the request parameter values as Array in Servlet.
Shishir
<%for (String st : geocodePhoto.keySet()) {%>
alert(<%=st%>); // not work
alert(<%=geocodePhoto.get(st).getX()%>); // work fine
alert(<%=geocodePhoto.get(st).getY()%>); // work fine
alert(<%=geocodePhoto.get(st).getDate()%>); // not work
<%}%>
getX is return double value and getDate return String value like 'yy:mm:dd hh:mm:ss'
st has same form 'yy:mm:dd hh:mm:ss'
2,3 line alert is work fine but 1,4 line alert is doesn't work
what is wrong?
The <%= %> tag in JSP acts as if it calls String.valueOf() with the expression in the tag as the parameter, and writes the returned value to the output. So, your generated JavaScript source probably looks something like this:
alert(13:11:23 10:30:17);
alert(-0.06);
alert(51.5);
alert(13:11:23 10:30:17);
You're trying to pass text to the first and last calls to alert, but you aren't putting the text in quotes - so, you're getting a syntax error. The middle two calls are writing numbers into your JavaScript source - as a numeric constant is valid JavaScript, they work without being quoted.
So, your JSP code should look like this:
alert("<%=st%>");
alert(<%=geocodePhoto.get(st).getX()%>);
alert(<%=geocodePhoto.get(st).getY()%>);
alert("<%=geocodePhoto.get(st).getDate()%>");
Pass string through ""
alert("<%=st%>");
alert("<%=geocodePhoto.get(st).getDate()%>");