I am trying to pass value from database to drop down menu using getAttribute(). However, it returns null.
This is my jsp (updateLecturer.jsp) file:
<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>
This is my 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);
}
This is my 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);
}
I've never worked with any of this type of code before, so I'm not sure how the attribute system works, but I noticed an anomaly in your code:
Where you are setting: request.setAttribute("LecturerFullname",lecturerFullname);
Where you are getting: lecturer.setLecturerFullname(request.getAttribute("lecturerFullname",lecturer));
Notice it yet? Where you are setting it, you put a capital "L" and I believe it's case sensitive. Doesn't hurt to try.
If you set some attribute in request like this
request.setAttribute("key",obj);
You can get it display in jsp by a scriplet like this
<%=request.getAttribute("key")%>
In your case please check the following points
Check whether the following code is giving you a null.
String lecturerFullname = request.getParameter("LecturerFullame");
If this gives you null then check whether your passing parameter LecturerFullame in url.
In the jsp please cast the following to correct object
<option name="lecturerFullname"><%=((String)request.getAttribute("LecturerFullname"))%></option>
And let me know.
In 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);
Now selectlecturer()
method.
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
}
In updateLecturer.jsp
.
<%
java.util.List<LecturerBean> lists = (java.util.List<LecturerBean>)request.getAttribute("LecturerFullname"));
for(LecturerBean bean:lists)
{
%>
<option name="lecturerFullname"><%=bean.getLecturerFullname()%></option>
<%
}
%>
But you should avoid Java code in JSP . Just for the reference here I'm giving this code.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.