我正在尝试使用getAttribute()将值从数据库传递到下拉菜单。但是,它返回null。
这是我的jsp(updateLecturer.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"><%=((LecturerBean)request.getAttribute("LecturerFullname"))%></option>**
</select>
</label>
</section>
</center>
<footer>
<center><button type="submit" class="button">Update</button></center>
</footer>
</form>
这是我的servlet UpdateLecturerServlet.java):
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, java.io.IOException {
String lecturerFullname = request.getParameter("LecturerFullame");
LecturerBean lecturer = new LecturerBean();
lecturer.setLecturerFullname(lecturerFullname);
request.setAttribute("LecturerFullname",lecturerFullname);
RequestDispatcher view = getServletContext().getRequestDispatcher("/updateLecturer.jsp");
view.forward(request,response);
}
这是我的UpdateLecturerDAO:
static Connection currentCon = null;
static ResultSet rs = null;
public static LecturerBean selectlecturer(LecturerBean Lbean) {
// preparing some objects for connection
System.out.println("JIJIJI");
Statement stmt = null;
String lecturerFullname = Lbean.getLecturerFullname();
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())
{
LecturerBean lecturer = new LecturerBean();
lecturer.setLecturerFullname(rs.getString("LecturerFullname"));
lecturer.add(lecturer);
}
}
catch (Exception ex) {
System.out.println("Select failed: An Exception has occurred! " + ex);
}
答案 0 :(得分:0)
我以前从未使用过任何此类代码,因此我不确定属性系统是如何工作的,但我发现代码中存在异常:
您在哪里设置:
request.setAttribute("LecturerFullname",lecturerFullname);
你得到的地方:
lecturer.setLecturerFullname(request.getAttribute("lecturerFullname",lecturer));
注意到了吗?你设置它的地方,你把一个大写“L”,我相信它是区分大小写的。尝试没有伤害。
答案 1 :(得分:0)
如果你在这样的请求中设置一些属性
request.setAttribute("key",obj);
你可以通过像这样的文章在jsp中显示它
<%=request.getAttribute("key")%>
在您的情况下,请检查以下几点
检查以下代码是否为空。
String lecturerFullname = request.getParameter("LecturerFullame");
如果这给你null,那么检查你在url中传递参数 LecturerFullame 。
在jsp中请将以下内容转换为正确的对象
<option name="lecturerFullname"><%=((String)request.getAttribute("LecturerFullname"))%></option>
让我知道。
答案 2 :(得分:0)
在return $section/text/d:table
。
UpdateLecturerServlet.java
现在 //calling selectlecturer() to retrieve list full names and store it in session
request.setAttribute("LecturerFullname",selectlecturer());
RequestDispatcher view = getServletContext().getRequestDispatcher("/updateLecturer.jsp");
view.forward(request,response);
方法。
selectlecturer()
在public static List<LecturerBean> selectlecturer() {
//DB query to retrieve all lecturer fullnames
List<LecturerBean> lecturers = new ArrayList<LecturerBean>();
while(rs.next())
{
LecturerBean lecturer = new LecturerBean();
lecturer.setLecturerFullname(rs.getString("LecturerFullname"));
lecturers.add(lecturer);
}
return lecturers;//return the list lecturer fullnames
}
。
updateLecturer.jsp
But you should avoid Java code in JSP。仅供参考,我提供此代码。