This is my Code that retrieves information from database and displays it in Table with an addition button that will used to submit(add to fav) to another new table with that row values and id of the user Below code displaying all records correctly with in table and every row is wrapped inside a form with button so when i click on a Particular row button then Values of that row only should be passed to target page,But for each and every row only the first record values are passing...
<%pst = con.prepareStatement("select * from reg_faculty");
res = pst.executeQuery();
while(res.next()) {
String uname = res.getString(1);
String id = res.getString(2);
String branch = res.getString(5);
String subject = res.getString(4);
%>
<tr><td align="center"><b><%=uname%></b></td><td align="center"><b><%=branch%></b></td>
<td align="center"><b><%=subject%></b></td>
<input type="hidden" name="fac_id" value="<%=id%>">
<input type="hidden" name="std_id" value="<%=ht%>">
<input type="hidden" name="fac_name" value="<%=uname%>">
<td align="center"><img src="viewimagef.jsp?id=<%=id%>&type=<%="faculty"%> " alt="" width="100" height="60" /></td>
<td align="center"><input type="submit" value="Add"/></td>
</tr>
<%
}
%>
Target page where the selected row values should pass to database but for any row selected the first record values are passing
String std_id1=request.getParameter("std_id");
String fac_id2=request.getParameter("fac_id");
String name=request.getParameter("fac_name");
try{
pst=con.prepareStatement("select * from fav_faculty where std_id=? AND fac_id=?");
pst.setString(1, std_id1);
pst.setString(2, fac_id2);
res=pst.executeQuery();
boolean exists = false;
if(res.next()){
out.println("Faculty Already exist in your List");
out.println("Faculty: "+fac_id2+" Student Id: "+std_id1+" Name: "+name);
}else{
pst=con.prepareStatement("insert into fav_faculty values(?,?)");
pst.setString(1, std_id1);
pst.setString(2,fac_id2);
int j=pst.executeUpdate();
if(j>0){
out.println("You successfully added "+name+" to your Faculty List");
}
}
}catch (Exception e) {
out.println("Unable To Add");
}
the problem is in the submit buttons you are using, a submit button belongs to the whole form, a working scenario would be changing the value of the submit button to the faculty id but keeping the same name for all the submit buttons.
another and probably a better solution would be adding a checkbox instead of a submit buttons.
<tr><td align="center"><b><%=uname%></b></td><td align="center"><b><%=branch%></b></td>
<td align="center"><b><%=subject%></b></td>
<input type="hidden" name="fac_id" value="<%=id%>">
<input type="hidden" name="std_id" value="<%=ht%>">
<input type="hidden" name="fac_name" value="<%=uname%>">
<td align="center"><img src="viewimagef.jsp?id=<%=id%>&type=<%="faculty"%> " alt="" width="100" height="60" /></td>
<td align="center"><input type="ckeckbox" name="fac_id" value="<%=id%>">
</tr>
%><%-- end of the while loop --%>
<input type="submit" value="add all">
and in the target page
String allSelectedFacultyIds[]= request.getParameterValues("fac_id");
for (int i=0;i<allSelectedFacultyIds.length;i++)
{
pst=con.prepareStatement("select * from reg_faculty where id="+allSelectedFacultyIds[i]);
//now you have the name, id, ... so the rest of your code should apply with minor changes
}
Related
I want to populate 3 selects (Zone, Station, Department) in struts2.
Each dropdown displays various options depending on value(s) selected in previous dropdown:
First field is Zone. User will select single Zone from zone select.
Based on this selected zone, station select will get populated from database.
User may select multiple stations here.
Based on these selected station(s), departments select tag will be populated from database.
Users may select multiple departments here.
I am able to populate zone select(dropdown) on page load and display it to jsp page.
However, station select is not displayed on my page, even though stationList attribute gets populated from database.
Setting it manually also does not help.
JSP Code here :
<tr>
<td style="background-color:#FFFFFF;" class="headingLabelStyle1" align="right">
<s:label value="Zone" ></s:label></td>
<td width="4%" align="center" class="headingLabelStyle1" style="background-color:#FFFFFF;">
<s:label value=":" /> </td>
<td style="background-color:#FFFFFF;">
<s:select name="pmrkpr8800Bean.zone" list="zoneList" id="zone"
headerValue="-- Please Select --" headerKey="-1"
tabindex="1" cssClass="h1"></s:select>
</td>
</tr>
<tr>
<td style="background-color:#FFFFFF;" class="headingLabelStyle1" align="right">
<s:label name="stncd" value="Station Code" ></s:label></td>
<td width="4%" align="center" class="headingLabelStyle1" style="background-color:#FFFFFF;">
<s:label value=":" /> </td>
<td style="background-color:#FFFFFF;">
<s:select name="pmrkpr8800Bean.stnCde" list="stationList" id="stnCde" headerValue="-- Please Select --" headerKey="-2" tabindex="2" cssClass="h1" size="3" multiple="true"></s:select>
</td>
</tr>
<tr>
<td style="background-color:#FFFFFF;" class="headingLabelStyle1" align="right">
<s:label value="Department" ></s:label></td>
<td width="4%" align="center" class="headingLabelStyle1" style="background-color:#FFFFFF;">
<s:label value=":" /> </td>
<td style="background-color:#FFFFFF;">
<s:select name="pmrkpr8800Bean.dept" list="deptList" id="dept" headerValue="-- Please Select --" headerKey="-1" tabindex="3" cssClass="h1" size="3" multiple="true"></s:select>
</td>
</tr>
public String populateZoneSelect() {
String result = SUCCESS;
try {
log.info("Inside populateZoneSelect");
zoneList = ipmrkpr8800.getZoneList();
} catch (Exception e) {
result = ERROR;
e.printStackTrace();
}
return result;
}
public String populateStationSelect() {
String result = SUCCESS;
String selZone = request.getParameter("selZone");
System.out.println("Selected zone: " + selZone);
try {
stationList.put("CSMT", "CSMT-MUMBAI");
stationList.put("NDL", "NDL-NEW DELHI");
stationList.put("PNVL", "PNVL-PANVEL");
} catch (IOException e) {
e.printStackTrace();
}
/*try {
log.info("Inside populateStationSelect");
stationList = ipmrkpr8800.getStationList(selZone);
System.out.println("StationList: " + stationList.size());
} catch (Exception e) {
result = ERROR;
e.printStackTrace();
}*/
return result;
}
Last two code blocks are from Action class. populateZoneList works fine and I am able to display different zones from database. Selected value is captured and sent to populateStationSelect(). if the commented part in the function is uncommented, stationList Map variable contains list of station codes and names.
But after that in jsp, in Station select tag only "Please Select" header value is displayed. The station codes and names retrireved from database are not displayed.
I have written getters and setters for these map variables also in action.
I'm trying to get ID to transfer when the button is clicked both ID and button is within the loop(so that they appear in a single row) but when I click the button it only transfers the last ID in the table...
<%
int ID = 0;
int userID =0;
String deleteRow = "delete_Row.jsp";
String myPage = "profile.jsp";
while (rs.next()) {
ID ++;
%>
<tbody>
<tr class="success">
<td><%=ID%></td>
<td><%=rs.getString(5)%></td>
<td><%=rs.getString(2)%></td>
<td><%=rs.getString(4)%></td>
<td><%=rs.getString(3)%></td>
<td><%=rs.getString(6)%></td>
<td><%=rs.getString(7)%></td>
<td><button type="button" class="btn btn-primary" onClick="window.location='<%= myPage %>';">Check Details</button></td>
<td><button href="delete_Row.jsp?userID=<%ID%>"type="button" class="btn btn-danger" onClick="window.location='<%=deleteRow%>';">Remove</button></td>
</tr>
</tbody>
<% } %> `
I spot two errors in your program:
The most important: <button> has no href attribute. I suggest you change it by:
Remove
The less important: The <tbody> should be defined outside the loop.
In my project I need to generate bill number automatically.
My database is mysql.
I am getting the default value 0 from the column 'bill_no' and increment that value. It's working fine, but my problem is it is incremented up to 10. After that it is not incremented.
What is the reason? please let me know,thanks
My code is:
<%
connection=DBCreation.getConnection();
Statement statement=connection.createStatement();
resultSet= statement.executeQuery("select max(diag_pt_bill_no_v) from diagnostics_details");
if(resultSet.next())
{
System.out.println(resultSet.getInt(1));
i=resultSet.getInt(1)+1;
}}catch(Exception e){
e.printStackTrace();
%>
<form action="diagnosticsinsert" method="post" name="form">
<table><tr>
<td><label class="red">Bill No </label><input type=text name="bno" placeholder="Bill No" size="17px" value="<%=i++%>"
</td></tr>
</table>
<td><button name="save" id="save" style="width: 80px; height: 30px;font-size: 15px;" value="Save" accesskey="S" onclick="getTotalTests()">Save</button>(Alt+s) </td>
</form>
check if any max value set to your text box....
This is my jsp page that retrieve the list of items from database using for loop
<%
itemManager mgr = new itemManager();
Item[] items = mgr.getAllItems();
for(int i = 0; i < items.length; i++){
%>
<tr>
<td> <img border="0" src="<%=items[i].getItemImage() %>" width="100" height="100">
</td>
<td>
<%=items[i].getItemName()%>
<input type="text" name="itemID" value="<%=items[i].getItemID()%>">
<br/>
<%=items[i].getItemDesc()%>
<br/>
Start Bid : <%=items[i].getStartBid()%>
<br/>
Buy it now : <%=items[i].getEndBid()%>
<br/>
Bidding close on : <%=items[i].getDuration()%>
<br/>
<input type="submit" value="View">
<%
}
%></table>
This is the jsp page that link to the item you selected previously
<table border="1" align="center">
<%
itemManager mgr = new itemManager();
Item items = mgr.getItem((Integer)session.getAttribute("ITEM_DATA"));
%>
<tr>
<td> <b> <%=items.getItemName() %></b> </td>
</tr>
</table>
This is the servlet to store the session of the selected item id and forward to the correct item jsp page.
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
HttpSession session = request.getSession(true);
int id = Integer.parseInt(request.getParameter("itemID"));
session.setAttribute("ITEM_DATA",id);
RequestDispatcher rd = request.getRequestDispatcher("viewItem.jsp");
rd.forward(request, response);
}
However, after I clicked the view button. It keeps linking to the itemID = 1.
The URL dispalys "/ItemServlet?itemID=1&itemID=2" .
In fact, if I click on itemID=2 the URL should display like this:
"/ItemServlet?itemID=2"
As a result, how can I achieve this? Thanks in advance!
The problem in your code is you are using a single form and dynamically creating Input box inside the form. So all the input box will be having same name. That's why when you submit the form all the input box values are sent as request parameters. I just reduced some part in your code for better under standing. Take this as your code
<form action="item" method="get">
<table>
<%
ItemManager mgr = new ItemManager();
Item[] items = mgr.getAllItems();
for(int i = 0; i < items.length; i++){
%>
<tr>
<td>
<%=items[i].getItemName()%>
<input type="text" name="itemID" value="<%=items[i].getItemId()%>">
<input type="submit" value="View"> </td></tr>
<%
}
%></table>
</form>
When you run this code and if you check the rendered HTML code it will be look
<form action="item" method="get">
<table>
<tr><td>
aaa
<input type="text" name="itemID" value="1">
<input type="submit" value="View"> </td></tr>
<tr>
<td>
bbb
<input type="text" name="itemID" value="2">
<input type="submit" value="View"> </td></tr>
<tr><td>
ccc
<input type="text" name="itemID" value="3">
<input type="submit" value="View"> </td></tr>
</table>
</form>
Here All the Input box having same name as "itemID". As a solution you can create the form inside the for loop, so that while submitting only one Input box value will be sent as request.
Create form inside your for loop like below code.
<table>
<%
ItemManager mgr = new ItemManager();
Item[] items = mgr.getAllItems();
for(int i = 0; i < items.length; i++){
%>
<form action="item" method="get">
<tr>
<td>
<%=items[i].getItemName()%>
<input type="text" name="itemID" value="<%=items[i].getItemId()%>">
<input type="submit" value="View"> </td></tr>
</form>
<%
}
%></table>
Hope This will help you.
change the name of text field dynamically.
And use getQueryString() in servlet to find the itemId name and value. by using EL.I hope this will help you
I'm tying to bind the value to dropdownlist i.e named as subtype which is depending on another dropdownlist i.e named as type. Please tell me how I can do that.. I have given I following code which implement for binding the dropdownlist depending on another dropdownlist.
`<table>
<tr>
<td>
<label id="type">Chosen category : </label>
</td>
<td>
<% String getType=request.getParameter("id");
out.println("<h4>"+getType+" <a href='post free ads.jsp'>Change the
category</a> </h4>");
%>
</td>
</tr>
<tr>
<td>
<label id="typeselect">Select type : </label>
</td>
<td>
<select name="type">
<option value="none">Select</option>
<%
Connection conn = NewClass.getOracleConnection();
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("Select distinct company from admin.all_stuff
where stuff_type='"+getType+"' ");
while(rs.next()){
%>
<option value="<%=rs.getString(1)%>"><%=rs.getString(1)%></option>
<%
}
%>
</select>
</td>
</tr>
<tr>
<td> <label id="subtypeselect">Select Subtype : </label></td>
<td>
<select name="subtype">
<option value="none">Select</option>
<%
try
{ %>
<% String typeselected = request.getParameter("type");
Statement stmtmt = conn.createStatement();
ResultSet rse = stmtmt.executeQuery("Select model_no from admin.all_stuff
where stuff_type='"+getType+"' and company='"+typeselected+"' ");
while(rse.next()){
%>
<option value="<%=rse.getString(1)%>"><%=rse.getString(1)%></option>
<%
}
%>
<% }
catch(Exception e)
{
out.println(e.getMessage());
}
%>
}
</select>
</td>
</tr>
</table>`
You need AJAX for this to get it work.
Your jsp is compiled into a servlet and then delivered to your client. After that no more changes are possible.
You have to handle an onChange event on the first drop down box in HTML and then post an AJAX request to your server to send back the content for the second drop down box.
Easiest way to start with AJAX is to get some library for example jQuery
$.get('ajax/test.html', function(data) {
alert(data);
});
This code allow us to download from HTML page some content from URL 'ajax/test.html'.
Second argument is callback function. Variable data has content of page 'ajax/test.html'
More: http://api.jquery.com/jQuery.get/