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.
In
UpdateLecturerServlet.java
.Now
selectlecturer()
method.In
updateLecturer.jsp
.But you should avoid Java code in JSP. Just for the reference here I'm giving this code.
If you set some attribute in request like this
You can get it display in jsp by a scriplet like this
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.