Sending multiple parameter values from a JSP to a servlet - html

The answer to my question is partially here but, not working for me
Sending multiple parameters to servlets from either JSP or HTML
My JSP is the following
<table cellpadding = "10" border = "1">
<tr valign = "bottom">
<th align = "left" > Catalog No </th>
<th align = "left" > Chemical Name </th>
<th align = "left" > Unit </th>
<th align = "left" > Price </th>
<th align = "left" > Vendor </th>
<th align = "left" > Category </th>
<th align = "left" > Account No</th>
</tr>
<%
try
{
ArrayList<CartProduct> cp3 = (ArrayList<CartProduct>) session.getAttribute("cp");
for(CartProduct pp: cp3)
{
%>
<tr>
<td><%=pp.getCatNo()%></td>
<td><%=pp.getChemName()%></td>
<td><%=pp.getChemUnit()%></td>
<td><%=pp.getPrice()%></td>
<td><%=pp.getVendor()%></td>
<td><%=pp.getCategory()%></td>
<td><select name = "accno"><option value="--Select--">--Select--</option>
<%ArrayList<String> str=pp.getAccList();
for(String s:str)
{
%> <option value="<%=s%>"><%=s%></option>
<%
}
%>
</select></td><td>
<a href="DisplayCartServlet?catNo=<%=pp.getCatNo()%>&;accountNo=accno">Add To Cart
</a></td>
</tr>
<%
}
}
finally{
}
%>
</table>
How do I send the value of the list box to the servlet? Currently only catNo is being passed, but accountNo is null at the servlet.

The easiest way to accomplish what you are attempting is to post using a form.
<form method="post" action="DisplayCartServlet">
<input type="hidden" name="catNo" value="<%=pp.getCatNo()%>">
<select name = "accno"><option value="--Select--">--Select--</option>
<%ArrayList<String> str=pp.getAccList();
for(String s:str)
{
%> <option value="<%=s%>"><%=s%></option>
<%
}
%>
</select>
<input type="submit" value="Add To Cart">
</form>

it should be accno , it seems you are trying to retrieve param by accountNo
Also See
how-to-avoid-java-code-in-jsp-files

Related

Send row info to angularjs controller when checkbox in row is clicked

I have some html code which uses angularjs to show a table. On this table, there is a checkbox on each row.
The table looks like this;
Here is the html code.
<div ng-app ng-controller="CheckCtrl">
<table class="table table-hover data-table sort display" style="width:100%">
<thead>
<tr>
<th class="Serial_">
Serial
</th>
<th class="Name_">
Name
</th>
<th class="ID_">
ID
</th>
<th class="On_off_">
On/off
</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="item in check_items">
<td>{{item.SERIAL}}</td>
<td>{{item.NAME}}</td>
<td>{{item.ID}}</td>
<td> <input type="checkbox" ng-checked="item.ON_OFF == '1'"></td>
</tbody>
</table>
</div>
Here is my angularjs controller code.
controller('CheckCtrl', ['$scope', '$http', 'configuration',
function ($scope, $http, $configuration) {
var url_api = $configuration.host + "cloe/webroot/cloe-cloud/app/API.json";
$http.get(url_api).success(function(data)
{
$scope.check_items = data;
});
This is what I want. When a user clicks on a checkbox, information regarding all the items on that particular row belonging to the clicked checkbox is sent to a angualrjs controller function for processing. This information should include the Serial, Name, ID and new state of On/Off after clicking on the checkbox.
I am using angularjs v1 and twitter bootstrap.
EDIT: Edited the original question details to indicate that the row information should be sent whenever the checkbox is clicked and not only when the checkbox is enabled.
We can use ng-change to send row data to controller as object.
After that, you can retrieve row data as following:
var serial = row.SERIAL;
var name = row.NAME;
var id = row.ID;
var onOff = row.ON_OFF;
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app" ng-controller="CheckCtrl">
<table class="table table-hover data-table sort display" style="width:100%">
<thead>
<tr>
<th class="Serial_">
Serial
</th>
<th class="Name_">
Name
</th>
<th class="ID_">
ID
</th>
<th class="On_off_">
On/off
</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="item in check_items">
<td>{{item.SERIAL}}</td>
<td>{{item.NAME}}</td>
<td>{{item.ID}}</td>
<td> <input type="checkbox" ng-checked="item.ON_OFF == '1'" ng-click="rowSelected(item)"></td>
</tbody>
</table>
</div>
<script>
var app = angular.module('app',[]);
app.controller('CheckCtrl', ['$scope', '$http', function ($scope, $http) {
$scope.check_items =
[
{
SERIAL:'Test Serial',
NAME:'Test Name',
ID : 10,
ON_OFF : '1'
}
];
$scope.rowSelected = function(row)
{
console.log(row);
};
}]);
</script>
You can maybe use something like this,
HTML:
<input type="checkbox" ng-checked="item.ON_OFF == '1'" ng-change="checkSubmit(item.serial,item.name,item.id,item.on_off)">
JS:
function checkSubmit(serial,name,id,on_off){
...
/*Processing can happen now with the parameters obtained*/
...}
NOTE: The case of the variables may be wrong
Some modification to the answer provided by Natiq.
Modify this line
<td> <input type="checkbox" ng-checked="item.ON_OFF == '1'" ng-model="item.selected" ng-change="rowSelected(item)"></td>
to
<td> <input type="checkbox" ng-checked="item.ON_OFF == '1'" ng-model="item.selected" ng-click="rowSelected(item)"></td>
The modification is from ng-change to ng-click. This will solve your problem where the first click does not work but only subsequent clicks work based on the comments posted. Using ng-click ensures that every click will be detected.

Best way to check jQuery POST method parameters for reducing jsp prepared statement query extra function

How can I make Submit TRUE result I mean from 2 INPUT field how to POST only one field of value to same JSP page like
<% string param=req.......
The most important thing is my PreparedStatement of jdbc have to be written twice for reason of two fields ... for checking Oracle DB of EMPLOYEE NO || NAME
By using Name field it's working fine and after changing of code by using Number it's working fine but I want you to help me is there any good way to code this stuff
this is HTML Form
<form id="contact" method="get">
<b>Employee No: </b> <input type="text" id="contact_number" size="15" name="number"/>
<b>Employee Name:</b> <input type="text" id="contact_name" size="25" name="name"/>
<input type="submit"/>
</form>
This is validation jQuery
$(document).ready(function() {
$("#contact").submit(function() {
if (($('#contact_number').val().length !== 0) && ($('#contact_name').val().length !== 0)) {
return false;
} else if (($('#contact_number').val().length === 0) && ($('#contact_name').val().length === 0)) {
return false;
} else {
return true;
}
});
});
This is complete JSP & String sql="Oracle query" & JDBC
<%String number = request.getParameter("number");
String name = request.getParameter("name");
if (name != null) { %>
<h3 id="myDiv"> Search results for <i> <%= name %> </i> </h3>
<% }
else { %>
<h4> Enter any Information on above field ... </h4>
<% } %>
<br>
<%#page import="java.sql.*" %>
<%Class.forName("oracle.jdbc.driver.OracleDriver");
String sql="SELECT * FROM RWEMP WHERE ENAME= ?";
Connection con=DriverManager.getConnection("jdbc:oracle:thin:#localhost:1521:XE","hari","root");
PreparedStatement stat=con.prepareStatement(sql);
stat.setString(1,name);
ResultSet rs=stat.executeQuery();
try {
if(rs!=null) {
%>
<table class="gridtable" border=1 cellspan=60 cellpadding=16>
<tr>
<th> Emp ID </th>
<th> Emp Name </th>
<th> Emp Dept </th>
<th> Emp D.o.B </th>
<th> Show Record </th>
<th> Record Update </th>
<th> Compassionate </th>
</tr>
<%
while(rs.next()) {
%>
<tr>
<td><%= rs.getString("EID")%> </td>
<td><%= rs.getString("ENAME") %> </td>
<td><%= rs.getString("EDEPT")%> </td>
<td><%= rs.getString("EDOB")%> </td>
<td><input class="ui-button" type="button" onSubmit="result.jsp" target="destination" value="Show"></td>
<td><input class="ui-button" type="button" onclick="update.jsp" target="destination" value="update"></td>
<td><input class="ui-button" type="button" onclick="compassionate.jsp" target="destination" value="Compassionate"></td>
</tr>
<%
}
}
}
catch(SQLException e) {
e.printStackTrace();
}
con.close();
%>
</table>
For more Viewing Look at this Fiddle's HTML output only
How can i make Submit TRUE result ... i mean from 2 INPUT field .. how
to POST only one Field of Value to same JSP page ... like :- <% string
param=req.......
To do that put a dropdown which say select the criteria of search in your case is number and name and then provide him with a input field so when you submit form you have both columname and data to the next page and you dont have write 2 queries.Form should be like this
If possible help me that if user Click on Particular userS table row
... how to SEND that user row details to others jsp page ... for
complete record viewing or record editing purpose
Solution :Pass id of that row to other page
Create a column on each row name say Detail and do
<a href="/detailed.jsp?user_id="<%=user_id%>>Detail</a>
On detailed.jsp get value of user_id from get parameter and the apply the query with user_id to get all the details of that row

Retrieve values from jsp table

I'm new to jsp and I'm trying to present the results of my database to a table in my jsp page. This looks like it works well as it presents all the elements correctly ! The problem is that I also want to to have a separate button and when I'm clicking it , I want the satatus of the line the button belongs to , to be converted from "unconfirmed" to "confirmed", but when I'm pressing it all the records turned to "confirmed" and not only the one the button belongs to !
My jsp code:
<table border="1">
<tr>
<th>Username</th>
<th>Role</th>
<th>Firstname</th>
<th>Lastname</th>
<th>Email</th>
<th>Phone</th>
<th>Status</th>
</tr>
<%
while(rs.next()){%>
<td> <%=rs.getString("username") %></td>
<td> <%=rs.getString("role") %></td>
<td> <%=rs.getString("firstname") %></td>
<td> <%=rs.getString("lastname") %></td>
<td> <%=rs.getString("email") %></td>
<td> <%=rs.getString("phone") %></td>
<td> <%=rs.getString("status") %></td>
<% if(rs.getString("status").equals("unconfirmed")){%>
<td><input type="button" name="users" onclick="<%Bean.changeStatus(rs.getString("username"));%>"</td>
<% }
else{ %>
<td>Check</td>
<% } %>
</tr>
<% } %>
</table>
And .java code:
public void changeStatus(String us){
ResultSet rs = null;
Statement state = null;
String query = null;
try{
state=this.getConn().createStatement();
query="update users set status='confirmed' where username='"+us+"'";
int rowsEffected = state.executeUpdate(query);
}catch(SQLException e){
}
}
Put a single listener on the table. When it gets a click from an input with a button that has a name of "unconfirmed" and value "unconfirmed", change its value to "confirmed". Get rid of the input's id (they aren't used for anything here), or make them all unique.
<script type="text/javascript">
function handleClick(evt) {
var node = evt.target || evt.srcElement;
if (node.name == 'unconfirmed') {
node.value = "confirmed";
}
}
</script>
<table id="table1" border="1" onclick="handleClick(event);">
<thead>
<tr>
<th>Select
</thead>
<tbody>
<tr>
<td>
<form name="f1" action="#" >
<input id="UnConfirmed1" type="submit" name="unconfirmed" value="UnConfirmed">
</form>
<tr>
<td>
<form name="f2" action="#" >
<input id="UnConfirmed2" type="submit" name="unconfirmed" value="UnConfirmed">
</form>
<tr>
<td>
<form name="f3" action="#" >
<input id="UnConfirmed3" type="submit" name="unconfirmed" value="onfirmed">
</form>
</tbody>
</table>

How to identify buttons created using a loop. Please assist

Following is the code, where the button Generate Key is getting created dynamically. For each record I get from the table, I create a button against it on the page. I want to capture which button was clicked and then use the values in that row to manipulate something.
<table border="1" width="100%" cellpadding="0" cellspacing="0">
<tr>
<th>File ID</th>
<th>Generate Key</th>
</tr>
<%
Connection con = DbConnector.getConnection();
PreparedStatement pstm = null;
String sql = "select u.uniquserid, t.filename, t.status, t.cloud, t.date_, t.report, t.FileID from transaction t join user u on u.userid = t.user order by t.date_ desc;";
pstm = con.prepareStatement(sql);
ResultSet rs = pstm.executeQuery();
while (rs.next()) { %>
<tr>
<td><%=rs.getString(7)%></td>
<% if (rs.getString(3).contains("s")) {%>
<%request.getSession().setAttribute("PassFID", rs.getString(7));%>
<td><input type="button" value='Generate Key' onclick=""></input></td>
<%} else {%>
<td></td>
<%}%>
</tr>
<%}%>
</table>
Use jquery to do your task.
Change your html code to these lines of code.
<form method="post" action="#" name="Form" id="Form" >
<input type="button" value="one" id="One"/>
<input type="button" value="two" id="Two"/>
</form>
And add these lines in your script
$('input:button').click(function() {
alert($(this).val());
var value=$(this).val();
var url='hello?good=';
url+=value;
$("#Form").attr("action",url);
$("#Form").submit();
});
You can use jquery 1.7.1 and above. Hope this helps you. Happy Coding :)

Retreival of multiple row from oracle 10g

I have created a table in oracle which consist of college_names, student_name,regno and result belong to particular college and college_ids of each and every college and date on which the data has been stored...
Now I want to give college_id and date and i want all the student name with their result and regno in a table like structure in a browser I have written one html page for giving college_id and date and one jsp page for retrieving different fields from database but its not working..here is my code for both the pages..
this is my html page
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org /TR/html4/loose.dtd">
<html>
<head>
<script>
function check()
{
var a=result.college_id.value;
var b=result.date.value;
if(a==""|| b=="")
{
alert("fill the fields")
}
else
{
result.action="result.jsp";
result.method="post";
result.submit();
}
}
</script>
</head>
<body bgcolor="#cc99ff">
<form name="result">
<center>
<table>
<tr>
<td>college_id:</td>
<td><input type="text" name="college_id"></td>
</tr>
<tr>
<td>date:</td>
<td><input type="text" name="date"></td>
</tr>
<tr>
<td>
<input type="Button" value="Submit" onClick="check()">
</td>
</tr>
</table>
</center>
</body>
</html>
Now here is my jsp page for retrieving multiple rows...
<html>
<body background="main_BG.jpg">
<%#page language="java"%>
<%#page import="java.sql.*,java.util.*"%>
<%!
Connection con;
PreparedStatement ps;
ResultSet rs;
String college_id;
String college_name;
String regno;
String student_name;
String result;
String date;
%>
<%
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
con=DriverManager.getConnection("jdbc:oracle:thin:#localhost:1521:orcl","pro","pro
");
college_id=request.getParameter("college_id");
date=request.getParameter("date");
ps=con.prepareStatement("select * from add_result where college_id=? and date=?");
ps.setString(1,college_id);
ps.setString(6,date);
rs=ps.executeQuery();
while(rs.next())
{
college_name=rs.getString(2);
regno=rs.getString(3);
student_name=rs.getString(4);
result=rs.getString(5);
}
else
{
out.println("no data found");
}
}
catch(Exception e)
{
out.println("<center><b><font color=lightblue>some error occured...please try again</font></b></center>");
out.println("<br><br><a href='result.html'><center><b><font color=lightblue>click here to return..</font></b></center>");
}
%>
<center>
<font color=lightblue>
<b>
Information of student with college_id [<%=college_id%>]:
</b>
</font>
</center>
<p style="position:absolute;left:100;top:100">
<table border="2" width="100%">
<th style="color:yellow">
college_name
</th>
<th style="color:yellow">
regno
</th>
<th style="color:yellow">
student_name
</th>
<th style="color:yellow">
result
</th>
<tr>
<td style="color:lightgreen" align="center"><%=college_name%></td>
<td style="color:lightgreen" align="center"><%=regno%></td>
<td style="color:lightgreen" align="center"><%=student_name%></td>
<td style="color:lightgreen" align="center"><%=result%></td>
</tr>
</table>
</p>
</body>
</html>
<%
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
con=DriverManager.getConnection("jdbc:oracle:thin:#localhost:1521:orcl","pro","pro ");
college_id=request.getParameter("college_id");
date=request.getParameter("date");
String sql = "select * from add_result where college_id=? and date=?";
ps=con.prepareStatement(sql);
ps.setString(1,college_id);
//ps.setString(6,date);
ps.setString(2,date);
System.out.println("Sql: " + sql);
rs=ps.executeQuery();
// process resultset below
}
catch(Exception e)
{
out.println("<center><b><font color=lightblue>some error occured...please try again</font></b></center>");
out.println("<br><br><a href='result.html'><center><b><font color=lightblue>click here to return..</font></b></center>");
}
%>
<center>
<font color=lightblue>
<b>
Information of student with college_id [<%=college_id%>]:
</b>
</font>
</center>
<p style="position:absolute;left:100;top:100">
<table border="2" width="100%">
<th style="color:yellow">
college_name
</th>
<th style="color:yellow">
regno
</th>
<th style="color:yellow">
student_name
</th>
<th style="color:yellow">
result
</th>
<%
if(rs!=null){
while(rs.next())
{
college_name=rs.getString(2);
regno=rs.getString(3);
student_name=rs.getString(4);
result=rs.getString(5);
%>
<tr>
<td style="color:lightgreen" align="center"><%=college_name%></td>
<td style="color:lightgreen" align="center"><%=regno%></td>
<td style="color:lightgreen" align="center"><%=student_name%></td>
<td style="color:lightgreen" align="center"><%=result%></td>
</tr>
<%
} // result set loop ends here
}else{
%>
<tr>
<td colspan="4" style="color:red" align="center">No Data Found</td>
</tr>
<%
} //if condition ends here
%>