我正在尝试避免jsp中的java代码用于下拉菜单。我不知道我的代码是否正确(我敢打赌还有其他错误)但是我真正的问题是,我不知道如何允许JSP中的下拉菜单从servlet接收数据。基本上我希望用户从下拉菜单中选择任何讲师名称,然后它将显示所选讲师的信息以进行更新(好吧,这是另一个故事)。我希望有人能帮帮忙。提前谢谢!
这是我的jsp代码:
<form action="updateLecturer" class="sky-form">
<header>Update Lecturer Information</header>
<center>
<fieldset>
<section>
<label class="select">
<select name="selectLecturer" id="lecturerFullname">
<option value="0" selected disabled>Lecturers Name</option>
**<option name="lecturerFullname">I HAVE PROBLEM IN HERE!!!</option>**
</select>
</label>
</section>
</center>
<footer>
<center><button type="submit" class="button">Update</button></center>
</footer>
</form>
这是我的servlet(不知道它是否正确):
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, java.io.IOException {
try {
LecturerBean lecturer = new LecturerBean();
lecturer.setLecturerFullname(request.getParameter("lecturerFullname"));
response.sendRedirect("updateLecturer.jsp");
}
catch (Throwable theException) {
System.out.println(theException);
}
}
这是我的web.xml文件:
<servlet>
<servlet-name>UpdateLecturerServlet</servlet-name>
<servlet-class>CSP600.UpdateLecturerServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>UpdateLecturerServlet</servlet-name>
<url-pattern>/updateLecturer</url-pattern>
</servlet-mapping>
这是我的DAO:
static Connection currentCon = null;
static ResultSet rs = null;
public static LecturerBean lecturerlogin(LecturerBean Lbean) {
// preparing some objects for connection
System.out.println("JIJIJI");
Statement stmt = null;
String lecturerID = Lbean.getLecturerID();
String lecturerFullname = Lbean.getLecturerFullname();
String lecturerPassword = Lbean.getLecturerPassword();
System.out.println("j444444");
String searchQuery = "select lecturerfullname from lecturer";
System.out.println("Your lecturer is " + lecturerFullname);
System.out.println("Query: " + searchQuery);
try {
// connect to DB
currentCon = JavaConnectionDB.getConnection();
stmt = currentCon.createStatement();
rs = stmt.executeQuery(searchQuery);
// boolean more = rs.next();
while(rs.next())
{
rs.getString(lecturerFullname);
}
}
catch (Exception ex) {
System.out.println("Log In failed: An Exception has occurred! " + ex);
}
// some exception handling
finally {
if (rs != null) {
try {
rs.close();
} catch (Exception e) {
}
rs = null;
}
if (stmt != null) {
try {
stmt.close();
} catch (Exception e) {
}
stmt = null;
}
if (currentCon != null) {
try {
currentCon.close();
} catch (Exception e) {
}
currentCon = null;
}
}
return Lbean;
}
答案 0 :(得分:0)
你传递给DAO的东西以及从它返回的东西 - 是同一个。 这段代码也没有任何意义:
while(rs.next()) {
rs.getString(lecturerFullname);
}
您需要在doGet()
方法中创建DAO对象的实例,然后填充一些列表并设置查询属性:
req.setAttribute("lecturerList", lecturerList);
RequestDispatcher rd = req.getRequestDispatcher("updateLecturer.jsp");
rd.forward(req, res);
然后使用JSTL填充JSP页面中的菜单:
<select>
<c:forEach var="lecturer" items="${lecturerList}">
<option value="${lecturer.id}">${lecturer.fullName}</option>
</c:forEach>
</select>