I am trying to implement a simple dynamic dropdown menu in JSP.I am using MONGODB.Here is my code.But I am getting
<select name="village" id="village">
<option value="0">Select Village</option>
<%
BasicDBObject adminQuery = new BasicDBObject();
DBCursor cursor = villages.find(adminQuery);
while(cursor.hasNext()){
%>
<option value="<%= cursor.next().get("Village").toString()%>">
<%= cursor.next().get("Village").toString()%>
</option>
<% } %>
</select>
But I am getting following exception
org.apache.jasper.JasperException: An exception occurred processing JSP page /home.jsp at line 116
113:
114: %>
115: <option value="<%= cursor.next().get("Village").toString()%>">
116: <%= cursor.next().get("Village").toString()%>
117: </option>
118: <% } %>
119: </select>
I don't know where is the problem.Please Help me
Use single quotes instead of double quotes.
Try this
<option value="<%= cursor.next().get('Village').toString() %>" ><%= cursor.next().get('Village').toString() %></option>
Related
I have this code in one jsp and i want to sent to the another and display selected value and correct answer. In the first page i have this code:
<form method="post" action="result.jsp" >
<p>choose answer</p>
<select name="fill">
<option value="0">Fill</option>
<option value="1">England</option>
<option value="2">China</option>
<option value="3">France</option>
</select>
<input type="submit" name="sent" />
</form>
and then the second jsp:
<body>
<jsp:declaration>
String s = "";
</jsp:declaration>
<jsp:scriptlet>
s = request.getParameter("fill");
</jsp:scriptlet>
<h1>Your answer is <jsp:expression>s </jsp:expression>and correct is England</h1>
</body>
And it returns null, why?
I need to have multiple values selected when I call that LoadDropDown function. But its not working.... Please suggest how to get multiple options selected.
<td>Select Profession:</td>
<td>
<select name="selectcontent_profession" id="selectcontent_profession" multiple>
<option value="0">None</option>
<option value="10">Student</option>
<option value="201">Lecturer</option>
<option value="333">Head Of Department</option>
<option value="421">Principal</option>
<option value="523">Chairman</option>
<option value="667">Management</option>
<option value="784">Placement Officer</option>
</select>
</td>
<%
String s1= "10,333,421,523";
String[] array = s1.split(",");
%>
<script >
function LoadDropDown()
{
<%for(int i=0;i<array.length;i++){%>
document.getElementById("selectcontent_profession").value ="<%= array[i]%>";
<%}%>
}
</script>
First you have to declare the select's multiple selection option as follows:
<select name="selectcontent_profession" id="selectcontent_profession" multiple="multiple">
And your function should be like this:
var fld = document.getElementById('selectcontent_profession');
for(var i=0;i<fld.options.length;i++){
for(var j=0;j<array1.length;j++){
if(fld.options[i].value==array1[j])fld.options[i].selected=true;
}
}
You are trying to get the value of the dropdown whereas you have to set it to selected.
You have to add the options like
var option = document.createElement("option");
option.text = "<%= array[i]%>";
option.value = "<%= array[i]%>";
document.getElementById("selectcontent_profession").options
.add(option);
document.getElementById("selectcontent_profession").options[<%=array[i]%>].defaultSelected =true;
Use this statement in the loop.
I m working on school project and I need to display academic year,section and medium dynamically from database in jsp file in drop down format. I am fetching the database values from java class and trying call tat java method in jsp to display those values but I am not getting nething der and i dnt want write query in jsp file.. please help me guyz m trying from last 3 days...
my java class
public class EmpBean {
public java.util.List dataList(){
ArrayList list=new ArrayList();
try {
Class.forName("driver");
Connection con = DriverManager.getConnection("url", "user", "pwd");
Statement st = con.createStatement();
System.out.println("hiiiii");
ResultSet rs = st.executeQuery("select * from employee");
while(rs.next()){
list.add(rs.getString("name"));
list.add(rs.getString("address"));
list.add(rs.getString("contactNo"));
list.add(rs.getString("email"));
}
System.out.println(rs.getString("contactNo"));
}
catch(Exception e){}
return list;
}
}
and my jsp file:
<%#page language="java" import="java.util.*" %>
<html>
<body>
<table border="1" width="303">
<tr>
<td width="119"><b>Name</b></td>
</tr>
<%Iterator itr;%>
<%
EmpBean p = new EmpBean();
List list= (List) p.dataList();
%>
for (itr=list.iterator(); itr.hasNext(); ) {
%>
<tr>
<select name="" id="" style="width: 150px;"">
<option value="-1"><%=itr.next()%></option>
</select>
</tr>
<%
}
%>
</table>
</body>
</html>
Edit 1:
Error message:
> The server encountered an internal error () that prevented it from
> fulfilling this request. exception org.apache.jasper.JasperException:
> An exception occurred processing JSP page
> /Administrative/collectFees.jsp at line 93 90: <td colspan="4"><div
> id="fndiv"> 91: <%Iterator itr;%> 92: <% List data=
> (List)request.getAttribute("data"); 93: for (itr=data.iterator();
> itr.hasNext(); ){ 94: %> 95: <select name="year1" id="yr1"
> style="width: 150px;" onclick="showDetails()"> 96: <option
> value="-1">><%=itr.next()%></option> root cause
> java.lang.NullPointerException
Try
<%
java.util.List list = new EmpBean().dataList();
%>
To Itterate you can use
<select>
<%for(String txt : new EmpBean().dataList()){%>
<option><%=txt%></option>
<%}%>
</select>
It's good to stick to good coding conventions.
Suming up the below thread: "The use of scriptlets (those <% %> things) in JSP is indeed highly discouraged"
How to avoid Java code in JSP files?
<tr>
<td><select name="" id="" style="width: 150px;"">
<option value="-1"><%=itr.next()%></option>
</select></td>
</tr>
you forgot the td tag
I have an arraylist for Strings eqArray.
I need to show it in a drop-down list for which I'm using the following in my JSP:
<%
for(int count=0;count<eqArray.size();count++){ %>
<option value="<%=eqArray.get(count)%>"><%=eqArray.get(count)%></option>
<%}
%>
I have an eqName String which is part of eqArray and should be the selected value by default.
How do I go about it without having to check and set the first option as eqName always?
<% for(int count=0; count<eqArray.size(); count++){ %>
<option value="<%= eqArray.get(count) %>" <%= (eqArray.get(count).equals("eqName"))?"selected":"" %> ><%= eqArray.get(count) %></option>
<%} %>
Change the index of eqName element to 0 in the array, or use a conditional Statement.
<%
for(int count=0; count < eqArray.size(); count++){ %>
<%if(eqArray.equals("eqName"){ %>
<option selected="selected" value="<%=eqArray.get(count)%>"><%=eqArray.get(count)%></option>
<%} %>
<option value="<%=eqArray.get(count)%>"><%=eqArray.get(count)%></option>
<%} %>
but use JSTL taglibs instead of using scriptlets.
You can do it via JQuery, which IMHO more clean:
<select data-selected="${eqName}">
<%
for(int count=0;count<eqArray.size();count++){ %>
<option value="<%=eqArray.get(count)%>"><%=eqArray.get(count)%></option>
<%}
%>
</select>
At the end of the page:
<script type="text/javascript">
$("select[data-selected]").each(function() {
var selected = $(this).data("selected");
$("select[data-selected='" + selected + "'] option[value='" + selected + "']").attr("selected", "selected");
})
</script>
By this way, you just need include the js at each of your page.
BTW, I recommend you to use JSTL and EL, which is more readable:
<%# page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%#taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<select data-selected="${eqName}">
<c:forEach items="${eqArray}" var="model">
<option value="${model}">${model}</option>
</c:forEach>
</select>
You can implement these in two ways
By using jsp
" selected="selected" >
By Using Javascript at the end of the select box code
document.getElementById('selectBoxID').value=""
instead of selectBoxID id use the select box id
I have the following code:
<div>
<%
TaxonomicTypeFeed ttf = new TaxonomicTypeFeed();
ArrayList<String> tmp = ttf.getTypes();
System.out.println("Going to print");
for (int i=0; i < tmp.size(); i++)
{
System.out.println(tmp.get(i));
}
%>
<form>
<select>
<%
Iterator<String> i = tmp.iterator();
while (i.hasNext())
{
String str = i.next(); %>
<option value="<%str.toString();%>"><%str.toString();%>
</option>
<%}%>
</select>
</form>
</div>
It creates a dropdown list fine, however there is no text.
This is the first time I have ever used any of these options before, so I have no idea if I am even going about it in the right manner.
You need to print the values by <%= %>. The <% %> won't print anything.
<option value="<%=str%>"><%=str%></option>
Unrelated to the problem: this is not the best practice. Consider using taglibs/EL. You end up with better readable/maintainable code.
<%#taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<jsp:useBean id="taxonomicTypeFeed" class="com.example.TaxonomicTypeFeed" />
...
<select>
<c:forEach items="${taxonomicTypeFeed.types}" var="type">
<option value="${type}">${type}</option>
</c:forEach>
</select>
Instead of <jsp:useBean> you can also use a preprocessing servlet.