Reputation: 9
I have one jsp page and one servlet, In JSP page I have two forms, each of select input type i.e.,select class and select student. When I select class from the select dropdown,by onchange event it goes to abcServlet,retrieves the class from the servlet and it forwards to jsp page goes to next form in the jsp page i.e., select student,it submits again to the same servlet,from their servlet forwards to same jsp page and students are retrieved from the servlet and Students are displayed in the select student dropdown correctly according to the class selected but my problem is when I select a student in select student dropdown, Class value is changing to null (I think when first time servlet forwards to jsp page, class is retrieved properly from the abcServlet and when I select student from the next drop down,second time whole jsp is forwarded, so class value is becoming null for the second time) I want my class to be remain same when I select student from the student dropdown after selecting class,How can I achieve that?
StudentFee.java
String sname[]={};
ResultSet rs;
int no_stdnts=0,admfee=0;
String clas;
clas=request.getParameter("class");
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection("jdbc:mysql://localhost/XYZ","XYZ","XYZ");
Statement st;
st=(Statement)con.createStatement();
rs=st.executeQuery("select count(sid) from students where class='"+clas+"'");
while(rs.next())
{
no_stdnts=rs.getInt(1);
}
String snames[]=new String[no_stdnts];
String stdnt=request.getParameter("student");
request.setAttribute("snames", snames);
request.setAttribute("no_stdnts", no_stdnts);
request.setAttribute("cls", clas);
RequestDispatcher rd=request.getRequestDispatcher("studentfee.jsp");
rd.forward(request,response);
studentfee.jsp
<form name="sclass" id="sclass" action="StudentFee" method="post">
<br><br>
<label style="font-size: 20px; word-spacing: 4px;"><b>Select Class : </b></label>
<select name="class" id="clas" onchange="Javascript:sclass.submit()">
<option value="Select" style="visibility:hidden;"><% String cls=(String)request.getAttribute("cls");if(cls!=null)out.print(cls);else out.print("Select");%></option>
<option value="Nursery">Nursery</option>
<option value="LKG">LKG</option>
<option value="UKG">UKG</option>
<option value="I">I</option>
<option value="II">II</option>
<option value="III">III</option>
<option value="IV">IV</option>
<option value="V" >V</option>
<option value="VI">VI</option>
<option value="VII">VII</option>
<option value="VIII">VIII</option>
<option value="IX">IX</option>
<option value="X">X</option>
</select>
</form>
</td></tr>
<tr><td>
<form name="sname" id="sname" action="StudentFee" method="post">
<br><br>
<label style="font-size: 20px; word-spacing: 4px;"><b>Select Student : </b></label>
<select name="student" id="clas" onchange="Javascript:sname.submit()">
<%
try
{
response.setContentType("text/html");
int no_stdnts=(Integer)request.getAttribute("no_stdnts");
String snames[]=new String[no_stdnts];
snames=(String[])request.getAttribute("snames");
for(int i=0;i<no_stdnts;i++)
{ %>
<option>
<% out.print(snames[i]);
}
}
catch(Exception e)
{
e.getMessage();
}
%>
</option>
</select>
</form>
Upvotes: 0
Views: 513
Reputation: 10383
The first thing I noticed are same id
values for both select
fields. Why? It's better to use differnt values and use same values for id
and name
for each select
.
The second problem is, that you use two different forms without to handle state. If you submit one form then the POST operation contains only parameters for this form's input fields. E.g if you submit form sname
then the POST request contains only parameter student
and no parameter class
.
You have to maintain the state of previous selections in some kind. There are several alternatives to achieve this.
student
and class
. In that case your servlet always gets the previous selection and you don't have to use server side state with a session.class
within the second form and initialize its value to the previous selection. If the user submits the second form your servlet will get the additional parameter class
as in 2nd scenario.The difference between 2. and 3. is that in 2. the user is able to change both selection at once.
If you got an understanding how to manage such state in a traditional way you can proceed with using AJAX where you don't submit and reload the whole page but only some small parts of it. There are several tutorials out there, just one Google search away.
Upvotes: 0
Reputation: 309008
It sounds like there's a session implicit in all this. You have two operations, and you want the second one to remember the result of the first.
You can maintain related values in session. If you don't find one, that means the query has never been run. If you do, return it and display it.
Upvotes: 0