我有一個jsp頁面和一個servlet,在JSP頁面中我有兩種形式,每種選擇輸入類型,即選擇類和選擇學生。當我從選擇下拉列表中選擇類時,通過onchange事件進入abcServlet,從servlet中檢索類,並將其轉發到jsp頁面,轉到jsp頁面中的下一個表單,即選擇student,然後再次提交給同一個servlet,從他們的servlet轉發到相同的jsp頁面,並從servlet中檢索學生,並根據選擇的類正確地在選擇學生下拉列表中顯示學生,但是我的問題是當我在選擇學生下拉列表中選擇學生時,Class值更改爲null (我認爲當第一次servlet轉發到jsp頁面時,類是從abcServlet中正確檢索的,當我從下一個下拉菜單中選擇學生時,第二次整個jsp被轉發,所以class值第二次變爲null)我想要我選擇課後從學生下拉菜單中選擇學生時,我的課程保持不變,我該如何實現這一目標?Jsp和Servlet處理
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>
代碼片段請 – 2013-04-26 17:11:03
不要提交整個頁面;使用Ajax。 – 2013-04-26 17:27:26
其實我並不瞭解Ajax,因爲我是JSP和servlet的新手。有沒有其他方式可能沒有ajax – user2147921 2013-04-26 17:34:26