I want to send the identificator of a product to the shoppingcard page and then display the product on the shoppingcart page
I used both request.set attribute and session.setattribute and it doesn't work. If I use session.getattribute or request.getattribute.toString() the page is white nothing is displayed. If I only use request.getAttribute (without toString) the line "ok" is not displayed on the result which means that purchased is null.
ProductStore is a map containing the products we have. Same for shoppingcardStore. ProductBean is the class of the products
products page:
<h2><%=ptp.getName()%></h2>
<div class="product-btns">
<form method="GET" action="<%="WhishList.jsp"%>">
<button class="main-btn icon-btn" name="id" value="<%=ptp.getId()%>"><i class="fa fa-heart"></i></button>
</form>
<button class="main-btn icon-btn"><i class="fa fa-exchange"></i></button>
<form action="shoppingcard.jsp" method="get">
<p> <%= ptp.getId() %> </p>
<%Object product=ptp;
request.setAttribute("purchase", ptp.getId());
%>
<input type="submit" value="add to cart">
<button class="primary-btn add-to-cart"><i class="fa fa-shopping-cart"></i> Add to Cart</button>
</form>
</div>
shoppingcard page
ProductStore products = new ProductStore();
Map<String,ProductBean> prodList = products.getProducts();
ShoppingcardStore db = new ShoppingcardStore();
Map<String,ProductBean> list = db.getShoppingcard();
Object purchased = request.getAttribute("purchase").toString();
if(purchased!=null){
out.println("<h1>Ok</h1>");
//ProductBean x = (ProductBean) purchased;
String x=(String) purchased;
db.Purchase(x);
//TODO confirm product has been added to the shoppingcart.
}%>
<!-- Product Slick -->
<div class="col-md-9 col-sm-6 col-xs-6">
<div class="row">
<div id="product-slick-1" class="product-slick">
<% if(list != null){
Object[] Shoppingcardlist = list.values().toArray();
ProductBean ptp;
for(int i = 0; i<Shoppingcardlist.length; i++){
ptp = (ProductBean)Shoppingcardlist[i];
// TODO display the info of the current wish list.
%>
of course it is just a part of my code, if you need to see something more tell me.
When you are doing setAttribute(), its scope is limited to the request when the main page is loading and hence will not be available on the next page as it will be a new request.
<%Object product=ptp;
request.setAttribute("purchase", ptp.getId());
%>
What you can do is, submit this value in URL param as GET or in a form (get/ post) to fetch it on next JSP using request.getParameter().
Or you can use session scope by session.setAttribute()
Hope it helps
Related
I am trying to get a one-page input value to another page while clicking the order button.
while I will take a number of item value and click the order button, it will carry the value the order page. The page code is here,
<td><%=rs.getString("product_price")%></td>
<td> <input type="number" name="no_item" value="1" /></td>
<td class="text-center" width="250">
Order
Edit
Delete
</td>
The order page code is here,
<%
statement = connection.createStatement();
String u=request.getParameter("u");
String item_num =request.getParameter("no_item");
int num=Integer.parseInt(u);
String Data = "select * from products_tbl where id='"+num+"'";
rs = statement.executeQuery(Data);
String product_price;
while (rs.next()) {
%>
<input type="hidden" name="id" value='<%=rs.getString("id")%>'/>
<div class="form-group">
<h4 style="float:left; padding-right:8px;">Product Name:</h4> <h4> <%=rs.getString("product_name")%> </h4>
</div>
<div class="form-group">
<%
product_price = rs.getString("product_price"); int num1 = Integer.parseInt(product_price); %>
</div>
<%= item_num %>
<%= num1 %>
<%
}
%>
Onclick of order button add the input value in url query string. you have need to use java script in jsp page.
<script>
function order(page, id){
input_value = document.getElementById('no_item').value;
location.href= page+"?u="+id+"&no_item="+input_value;
}
</script>
Add the onclick function within order button.
<a onclick="order('order.jsp', '<%=rs.getString("id")%>')" class="btn btn-success">Order</a>
In order.jsp page you will get the input value.
request.getParameter("no_item");
I have a dropdown which consist the language names. I am setting the value and displaying name of the dropdown by using a hashmap.
<form action="TextTranslation" method="post" class="form" role="form" >
<div class="row">
<div id = "imageView" class="col-lg-8 center-block ">
<div class="btn-group">
<select name="country">
<%
Map<String,String> langCode = x.getCountryList();
for( Object key :langCode.keySet() )
{%>
<option value="<%=(String)key%>"><%=langCode.get(key) %> </option>
<%
System.out.println((String)key);
}
String name = request.getParameter("country");
request.setAttribute("code", name);
%>
</select>
</div>
<input type="submit" class= "btn btn-image" value="Translate">
Search Text
</div>
</div>
</form>
Values are passed correctly to dropbox as it print all the values in console. the set attribute is accessed in the particular servlet. But it gives a null value. Do you have any idea?Thank you in advance
UPDATED
<select name="country">
<%
Map<String,String> langCode = x.getCountryList();
for( Object key :langCode.keySet() )
{%>
<option value="<%=(String)key%>"><%=langCode.get(key) %> /option>
<% System.out.println((String)key);
}
String name = request.getParameter("country");
%>
</select>
<input type="hidden" name="code" value = <%= name%>/> .
In the servlet I used,
request.getParameter("code");
update your jsp likewise,
<form...>
...
<input type="hidden" name="code" value = <%= name%>/>
....
</form>
then get it from your servlet likewise,
request.getParameter("code"); // will return value of code
NOTE :
Remove from your jsp-code if above solution you gonna implement then,
request.setAttribute("code", name);
I am making a web application in which I pass a Int value from a servlet to next jsp page like this :
request.setAttribute("n",n);
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/sharingfilesselection.jsp");
dispatcher.forward(request, response);
Now on the next page i.e sharingfilesselection.jsp I want that n textboxes are created dynamically each with a different id as i need to store the values of these textboxes in my database.
The N is obtained on next jsp page by this :
Object N=request.getAttribute("n");
How this can be done using javascript ?Please help
You can do this using JSTL:
<c:forEach var="i" begin="1" end="${n}">
Input ${i}: <input type="text" name="txtDynamic_${i}" id="txtDynamic_${i}" />
<br />
</c:forEach>
Try this in your
.javascript
<%String n=(String)request.getAttribute("n");%>
var n=<%=n%>;
for(var i=0;i<n;i++{
$(".exac").append("<input type="text" id='textbox"+i+"'></input>");
}
}
.html
<div class="exac">
</div>
Working sample here
html
<div id="container"><input type="button" onclick="createTextBox(5)" value="create textbox">
JS
function createTextBox(n) {
for (var i = 0; i < n; i++) {
var textBox = document.createElement("input");
textBox.setAttribute("id", "text_" + i);
document.getElementById("container").appendChild(textBox);
}}
I have two jsp pages: search.jsp and update.jsp.
When I run search.jsp then one value fetches from database and I store that value in a variable called scard. Now, what I want is to use that variable's value in another jsp page. I do not want to use request.getparameter().
Here is my code:
<%
String scard = "";
String id = request.getParameter("id");
try {
String selectStoredProc = "SELECT * FROM Councel WHERE CouncelRegNo ='"+id+"'";
PreparedStatement ps = cn.prepareStatement(selectStoredProc);
ResultSet rs = ps.executeQuery();
while(rs.next()) {
scard = rs.getString(23);
}
rs.close();
rs = null;
} catch (Exception e) {
out.println(e.getLocalizedMessage());
} finally {
}
%>
How can I achieve this?
Using Query parameter
<a href="edit.jsp?userId=${user.id}" />
Using Hidden variable .
<form method="post" action="update.jsp">
...
<input type="hidden" name="userId" value="${user.id}">
you can send Using Session object.
session.setAttribute("userId", userid);
These values will now be available from any jsp as long as your session is still active.
int userid = session.getAttribute("userId");
Use sessions
On your search.jsp
Put your scard in sessions using session.setAttribute("scard","scard")
//the 1st variable is the string name that you will retrieve in ur next page,and the 2nd variable is the its value,i.e the scard value.
And in your next page you retrieve it using session.getAttribute("scard")
UPDATE
<input type="text" value="<%=session.getAttribute("scard")%>"/>
Use below code for passing string from one jsp to another jsp
A.jsp
<% String userid="Banda";%>
<form action="B.jsp" method="post">
<%
session.setAttribute("userId", userid);
%>
<input type="submit"
value="Login">
</form>
B.jsp
<%String userid = session.getAttribute("userId").toString(); %>
Hello<%=userid%>
How can I send data from one JSP page to another JSP page?
One of the best answer which I filtered out from above discussion.
Can be done in three ways:
using request attributes:
Set the value to send in request attribute with a name of your choice as request.setAttribute("send", "valueToSend") and retrieve it on another jsp using request.getAttribute("send");
using session attributes
Similar to above but using session object instead of request.
using application attributes
Same as 1 and 2 above but using application object in place of request and session.
Suppose we want to pass three values(u1,u2,u3) from say 'show.jsp' to another page say 'display.jsp'
Make three hidden text boxes and a button that is click automatically(using javascript).
//Code to written in 'show.jsp'
<body>
<form action="display.jsp" method="post">
<input type="hidden" name="u1" value="<%=u1%>"/>
<input type="hidden" name="u2" value="<%=u2%>" />
<input type="hidden" name="u3" value="<%=u3%>" />
<button type="hidden" id="qq" value="Login" style="display: none;"></button>
</form>
<script type="text/javascript">
document.getElementById("qq").click();
</script>
</body>
// Code to be written in 'display.jsp'
<% String u1 = request.getParameter("u1").toString();
String u2 = request.getParameter("u2").toString();
String u3 = request.getParameter("u3").toString();
%>
If you want to use these variables of servlets in javascript then simply write
<script type="text/javascript">
var a=<%=u1%>;
</script>
Hope it helps :)
I have created dynamic row on click of button in table using following code :
<script type="text/javascript">
var counter = 1;
function displayResult()
{
counter++;
document.getElementById("myTable").insertRow(-1).innerHTML = '<td><select name="list_dispatch_state" id="list_dispatch_state"><option value="01">01</option><option value="02">02</option><option value="03">03</option></select></td><td><input type="text" name="txt_email'+ counter +'" id="txt_email'+ counter +'" value='+ counter +'></td>';
}
</script>
<body>
<form action="Dogetdat" method="post">
<table id="myTable" border="1">
<tr>
<th>Select</th>
<th>Value</th>
</tr>
</table>
<br />
<button type="button" onclick="displayResult()">Insert new row</button>
<input type="submit">
</form>
</body>
my question is that, on click of button new row and control inside it created but when I click on submit then form submitted to servlet page.
Then how servlet will know that how many data are received ?
Because in servlet I ll get data using
String str1= request.gerParameter("txt_email");
how servlet will know that how many variable it have to create and what will be the name of that ? what will I have to pass in request.gerParameter(""); ?
You have to use the following request.getParameterValues and get the result as an array
String emails[] = request.getParameterValues("txt_email");