I have a database driven textbox that needs to be repopulated when the user hits the back button (a back button that I have created on the form)
Currently, I am able to repopulate non database driven fields on the form using http sessions just fine. I cannot seem to apply to same logic to database driven fields.
The code on my jsp looks as follows:
<td><select name = "actionType" tabindex = "1" value="<%if(session.getAttribute("actionType")== null) out.print(""); else out.print(session.getAttribute("actionType"));%>">
<option>--</option>
<% for(int i=0; i<actTypeDDL.size()-1; i++){
String actType = actTypeDDL.get(i).toString();
i++;
String actTypeVal = actTypeDDL.get(i).toString();%>
<option value=<%=actTypeVal%>>
<%=actType%>
</option>
<%
} %>
</select></td>
Any ideas?
The issue doesn't have anything to do with the fact that the value comes from the database, the problem is that specifying a value on a select tag won't preselect the option. You need to add a "selected" flag to the option itself.
The following should work:
<td><select name = "actionType" tabindex = "1" >
<option>--</option>
<% for(int i=0; i<actTypeDDL.size()-1; i++){
String actType = actTypeDDL.get(i).toString();
i++;
String actTypeVal = actTypeDDL.get(i).toString();%>
<option value=<%=actTypeVal%>
<% if (session.getAttribute("actionType") == actTypeVal) {
System.out.println("selected = 'true'");
} %>
>
<%=actType%>
</option>
<%
} %>
</select></td>
Related
I have one jsp and want to disable dropdown list using scriplet without any onclick method or event in dropdown menu
<%
boolean something= true;
if(something){
// here I want to get plc1 using id and want to disable it if it is true
}else{
//do some thing
}
%>
my dropdown html code is here
<td bgcolor='' align="center" ><select id = "plc1" name="place1" onclick="this.parentNode.style.backgroundColor = this.value">
<option value='red'>Park here</option>
<option value='green'>Cancel</option>
</select></td>
how to do it? any hint please?
just use disabled attribute: <select id="plc1" disabled>
<%
String state = "";
if(something){
state = "disabled";
}
%>
<select id="plc1" <%= state %>>
<%
boolean something=false;
String state = "";
if(something){
state = "disabled";
// here I want to get plc1 using id and want to disable it if it is ture
}else{
state = "enable";
}
%>
Html
<select <%= state %> id = "plc1" >
Now its working Thanks
I am tying to alert the value of namesEmp variable and i get this Ljava.lang.String;#3433205b as result.
my javacsript:
function getNames(names) {
var namesEmp = "";
var namesEmpText = "";
for(i=0;i<names.length;i++) {
if(names.options[i].selected) {
namesEmp = names.options[i].value;
namesEmpText = names.options[i].text;
alert(namesEmp);
alert(namesEmpText);
}
}
}
and here is my HTML:
<tr style="position:relative;left:19;top:1;display:none;">
<td style="position:relative;top:1;text-align:right;">Country:</td>
<td>
<select id="namePay" name="namesPay" onChange="setTimeout('getNames(f.name)',1);">
<%
String namesDesc = "";
if(namesList.size()>0){
for(int i=0;i<namesList.size();i++){
String[] namesValues = ((String)namesList.get(i)).split("~");
NamesText = namesValues[0];
%>
<option value="<%=namesValues%>"><%=NamesText%>
<%}
}%>
</select>
</td>
</tr>
When I alert namesEmpText, i get the the correct result. Any help would be appreciated.
The issue is here:
<option value="<%=namesValues%>"><%=NamesText%>
^
You are trying to render the Java object namesValues (a string array), as a select option value. The JVM doesn't know how to automatically convert an array to a string, so it renders the type name for it instead. Did you mean to include an indexer to that array? For example:
<option value="<%=namesValues[1]%>"><%=namesValues[0]%>
I am new to JSP and don't have much idea. So just let me clarify my requirement.
Say I have a program as below, where there's a First.jsp with an array Match_List[5].
String[] Match_List;
Match_List[] = {a, b, c, d, e};
<form name="Team_Playerdetails" method="post" action="db_Match_Edit.jsp">
<TABLE>
<% for (int j = 0; j < 10; j++) { %>
<TR>
<TD>
<SELECT name="Stat_Match_name">
<% for (int i = 0; i < 5; i++) { %>
<option>
<% out.println(Match_List[i][1]); %>
</option>
<% } %>
</SELECT>
</TD>
</TR>
<% } %>
</TABLE>
<input type="submit" name="submit" value="Add All" tabindex="10" class="button" />
</form>
Now this form above will display a same drop down lists 10 times. I want to select different options in these ten lists say like below:
List_1 Option: a
List_2 Option: d
List_3 Option: e
List_4 Option: b
List_5 Option: c
.....
List_10 Option: d
Now I need that once user clicks the submit button then the variable should be moved to db_Match_Edit.jsp. Here I will have the code to catch the value as
U_Stat_Match_name = request.getParameter("Stat_Match_name");
But since all the 10 drop down options are getting caught by the same select variable name Stat_Match_name. So on db_Match_Edit.jsp I am only getting the last selected option in U_Stat_Match_name.
But I need all the 10 selected options in the db_Match_Edit.jsp jsp in an array.
Either just use HttpServletRequest#getParameterValues():
String[] statMatchNames = request.getParameterValues("Stat_Match_name");
// ...
Or just give them each an unique value based on the iteration index:
<select name="Stat_Match_name_<%=j%>">
and grab them individually as follows:
for (int j = 0; j < 10; j++) {
String statMatchName = request.getParameter("Stat_Match_name_" + j);
// ...
}
Unrelated to the concrete problem, this oldschool JSP code style suggests that you're learning JSP by a heavily outdated resource of possibly more than 10 years old. I recommend to concentrate on more recent and sane learning resources. Start at How to avoid Java code in JSP files? and then read our JSP wiki page.
I have two html drop down list, their value are retrieved from the database by using jsp.
<%
String query =" SELECT question_text,question_id FROM questions WHERE id = ?";
PreparedStatement stmt = conn.prepareStatement(query);
stmt.setString(1,request.getParameter("QID"));
ResultSet rs = stmt.executeQuery();
if(!rs.next()) {} else {%>
<form action="#" method="post">
<p> First Question </p>
<select name="currentQuestion">
<%do{%>
<option value="<%=rs.getString("question_id")%>"><%=rs.getString("question_text")%> </option>
<%}while(rs.next());}%>
</select>
<%
String query =" SELECT question_text,question_id FROM questions WHERE id = ? AND question id != ? ";
PreparedStatement stmt = conn.prepareStatement(query);
stmt.setString(1,request.getParameter("QID"));
stmt.setString(2,CHOOSEN QUESTION);
ResultSet rs = stmt.executeQuery();
if(!rs.next()) {} else {%>
<p> Next Question </p>
<select name="currentQuestion">
<%do{%>
<option value="<%=rs.getString("question_id")%>"><%=rs.getString("question_text")%></option>
<%}while(rs.next());}%>
</select>
</form>
Now, I what when the user choose a specific question from the first drop down list, the value of the second drop down list does not include that question ?
is anyone know how to do that ?
You don't set CHOOSEN QUESTION anywhere in the code.
You forgot he id and name tags in the option element.
CHOOSEN QUESTION is an illegal name for a variable since it contains a space.
You can't do it the way you're trying to do, cause this code runs on the server-side before the user chooses an option. What you probably want to do is load all the options (if there aren't too many of them) and create a JS/jQuery that will refresh the second dropbox on the event onChange of the first dropbox (before the user chooses an option - you'll probably want to have the second dropbox disabled)
Another thing that you probably want to do is create a form which will eventually submit the user's choices to a JSP (server-side).
You can also achieve the same behavior using AJAX, you can find an example of how to do it here.
UPDATE (example code for changing options using JS):
<html>
<head>
<script type="text/javascript" >
<!-- hide
function update(x){
if (x != "null") {
if (x == "1") {
var jumpmenu2 = document.getElementById("jumpmenu2");
var newOption1 = document.createElement('option');
newOption1.text = "a"; //HERE You'll use newOption1.text = "<?php echo $db_option_text;?>";
newOption1.value = "1"; //HERE You'll use newOption1.text = "<?php echo $db_option_value;?>";
var newOption2 = document.createElement('option');
newOption2.text = "b"; // same like above
newOption2.value = "2"; // same like above
var newOption3 = document.createElement('option');
newOption3.text = "c"; // same like above
newOption3.value = "3"; // same like above
jumpmenu2.remove(jumpmenu2.length-1);
jumpmenu2.remove(jumpmenu2.length-1);
jumpmenu2.remove(jumpmenu2.length-1);
try {
// For standard browsers
jumpmenu2.add(newOption1,null);
jumpmenu2.add(newOption2,null);
jumpmenu2.add(newOption3,null);
}
catch (ex) {
// For Microsoft Internet Explorer and other non-standard browsers.
jumpmenu2.add(newOption1);
jumpmenu2.add(newOption2);
jumpmenu2.add(newOption3);
}
}
else if (x == "2"){
var jumpmenu2 = document.getElementById("jumpmenu2");
var newOption1 = document.createElement('option');
newOption1.text = "d";
newOption1.value = "1";
var newOption2 = document.createElement('option');
newOption2.text = "e";
newOption2.value = "2";
var newOption3 = document.createElement('option');
newOption3.text = "f";
newOption3.value = "3";
jumpmenu2.remove(jumpmenu2.length-1);
jumpmenu2.remove(jumpmenu2.length-1);
jumpmenu2.remove(jumpmenu2.length-1);
try {
// For standard browsers
jumpmenu2.add(newOption1,null);
jumpmenu2.add(newOption2,null);
jumpmenu2.add(newOption3,null);
}
catch (ex) {
// For Microsoft Internet Explorer and other non-standard browsers.
jumpmenu2.add(newOption1);
jumpmenu2.add(newOption2);
jumpmenu2.add(newOption3);
}
}
}
}
// end hide -->
</script>
</head>
<body>
<form name="form1" id="form1">
<select name="jumpmenu" name="jumpmenu" onChange="update(document.form1.jumpmenu.options[document.form1.jumpmenu.options.selectedIndex].value)">
<option value=1>1</option>
<option value=2>2</option>
</select>
</form>
<select name="jumpmenu2" id="jumpmenu2">
<option value=a id=1>a</option>
<option value=b id=2>b</option>
<option value=c id=3>c</option>
</select>
</body>
</html>
I'm currently working on an Inventory Management project. I m working with JSP and MySQL on Netbeans Platform. In my project on querying I need to retrieve values from the database and display it in a table. The rows to be displayed should be dynamic in my page. They should be displayed in any number. Suppose When I want to retrieve values based on a particular choice I select, I should be able to display all the data based on the choice and display it in the rows of the table. I am not able to display it in multiple rows of my table because I m using text boxes to display the values.
Here is the code snippet:
<table>
<tr>
<td>
<select name="choice_type">
<option>select</option>
<option value="part_type">part_type</option>
<option value="category">category</option>
<option value="names">names</option>
</select>
</td>
</tr>
<tr>
<th>VAL</th>
<th>VAL DESC</th>
</tr>
<tr>
<td> <input type="text" name="val" id="val" size="15" /></td>
<td> <input type="text" name="val_desc" id="val_desc" size="15" /></td>
</tr>
</table>
<input type="submit" name="Query" value="Query" onClick="getData();"/>
The getData() function is as follows:
function getData(){
xmlHttp=GetXmlHttpObject()
var id=document.getElementById("choice_type").value;
var url="choice_retrieval.jsp";//The code for this file is defined below
url=url+"?choice_type="+id;
xmlHttp.onreadystatechange=stateChanged
xmlHttp.open("GET",url,true)
xmlHttp.send(null);
}
function stateChanged(){
if(xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){
var showdata = xmlHttp.responseText;
var strar = showdata.split(":");
if(strar.length>1){
var strname = strar[1];
document.getElementById("val").value= strar[1];
document.getElementById("val_desc").value= strar[2];
}
}
The Code snippet for choice_retrieval.jsp is as follows:
<%
String ch = request.getParameter("choice_type").toString();
System.out.println(ch);
String data ="";
try{
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://", "", "");
Statement st=con.createStatement();
ResultSet rs=st.executeQuery("select * from master_panel where choice_type='"+ch+"'");
while(rs.next())
{
data = ":" + rs.getString("val") + ": " + rs.getString("val_desc");
}
out.println(data);
System.out.println(data);
}
catch(Exception e) {
System.out.println(e);
}
%>
Database table used here is master_panel(choice_type varchar,val varchar,val_desc varchar). I have not put any constraints as of now. Based on the choice_type value I need to retrieve the corresponding data(val and val_desc) from the database and display it in dynamic rows.
Assuming that the data is being returned (your stateChanged method is being invoked) you need to dynamically create the table rows (and their contents, the text boxes) in your stateChanged method by modifying the DOM.
To modify the DOM to create the table structure the code should read something like this (assuming you've already removed the previously displayed rows):
var table = document.getElementById('tableId');
var data = xmlHttp.responseText.split(":");
for (int i = 0; i < data.length; i + 2)
{
var valueText = document.createElement('input');
valueText.type = 'text';
valueText.name = 'value' + i;
valueText.value = data[i];
var valueCell = document.createElement('td');
valueCell.appendChild(valueText);
var descriptionText = document.createElement('input');
descriptionText.type = 'text';
descriptionText.name = 'value' + i;
descriptionText.value = data[i + 1];
var descriptionCell = document.createElement('td');
descriptionCell.appendChild(descriptionText);
var tableRow = document.createElement('tr');
tableRow.appendChild(valueCell);
tableRow.appendChild(descriptionCell);
table.tBodies[0].appendChild(tableRow);
}
Also, as #TrueDub said, putting SQL in JSPs is bad for a whole host of reasons. What's worse is building SQL queries with string concatenation - it opens your system to SQL injection attacks, especially when the string being concatenated includes a string captured in the browser.
You're doing quite a few things wrong there, especially having database code within a JSP, but to answer your specific question, you should write your resultset into a list and set this list into the request scope with a specific id. You can then iterate over it using the JSTL tag and display the output using the {} notation.