从servlet显示数据到下拉菜单jsp [英] display data from servlet to drop down menu jsp
问题描述
我试图避免jsp中的java代码用于下拉菜单。我不知道我的代码是否正确(我打赌还有其他错误),但我真正的问题是,我不知道如何让JSP下拉菜单从servlet接收数据。基本上我希望用户从下拉菜单中选择任何讲师姓名,然后它会显示选定的讲师信息以更新(好吧,那是另一个故事)。我希望有人能帮帮忙。
这是我的jsp代码:
< form action =updateLecturerclass =sky-form>
< header>更新讲师信息< / header>
< center>
< fieldset>
< section>
< label class =select>
< select name =selectLecturerid =lecturerFullname>
< option value =0selected disabled> Lecturers Name< / option>
**< option name =lecturerFullname>我在这里遇到了问题!!!< / option> **
< / select>
< / label>
< / section>
< / center>
< footer>
< center><按钮类型=submitclass =button>更新< /按钮>< / center>
< / footer>
< / form>
这是我的servlet(不知道它是否正确):
public void doGet(HttpServletRequest请求,HttpServletResponse响应)
throws ServletException,java.io.IOException {
试试{
LecturerBean lecturer = new LecturerBean();
lecturer.setLecturerFullname(request.getParameter(lecturerFullname));
response.sendRedirect(updateLecturer.jsp);
$ b catch(Throwable theException){
System.out.println(theException);
$ / code $ / pre
这是我的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){
//准备一些连接对象
System.out.println(JIJIJI);
Statement stmt = null;
字符串lecturerID = Lbean.getLecturerID();
字符串lecturerFullname = Lbean.getLecturerFullname();
String lecturerPassword = Lbean.getLecturerPassword();
System.out.println(j444444);
字符串searchQuery =从讲师选择lecturerfullname;
System.out.println(你的讲师是+ lecturerFullname);
System.out.println(Query:+ searchQuery);
尝试{
//连接到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);
//一些异常处理
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;
$ / code $ / pre
解决方案传入DAO并从中返回 - 都是一样的。
此代码也没有任何意义:
while(rs.next()){
rs.getString(lecturerFullname);
您需要在<$ c $中创建一个DAO对象的实例c> doGet()
方法,然后填充一些列表并设置查询属性:
req。 setAttribute(lecturerList,lecturerList);
RequestDispatcher rd = req.getRequestDispatcher(updateLecturer.jsp);
rd.forward(req,res);
然后使用JSTL填充JSP页面中的菜单:
< select>
< c:forEach var =lectureritems =$ {lecturerList}>
< option value =$ {lecturer.id}> $ {lecturer.fullName}< / option>
< / c:forEach>
< / select>
I am trying to avoid java codes in jsp for drop down menu. i dont know if my codes are correct in the first place (i bet there are other errors) BUT my real problem is, i dont know how to allow drop down menu in JSP to receive data from servlet. Basically I want the user to select any lecturer name from drop down menu, then it will display the chosen lecturer's information to update (well, thats another story). I hope someone can help. Thank you in advance!
this is my jsp code:
<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>
This is my servlet (not sure if its right anyway):
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);
}
}
This is my web.xml file :
<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>
This is my 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;
}
解决方案 What you pass into the DAO and what returns from it - are one and the same.
This code also doesn't make any sense:
while(rs.next()) {
rs.getString(lecturerFullname);
}
You need to create an instance of the DAO object in the doGet()
method, then populate some list and set the query attribute:
req.setAttribute("lecturerList", lecturerList);
RequestDispatcher rd = req.getRequestDispatcher("updateLecturer.jsp");
rd.forward(req, res);
Then populate the menu in the JSP page by using JSTL:
<select>
<c:forEach var="lecturer" items="${lecturerList}">
<option value="${lecturer.id}">${lecturer.fullName}</option>
</c:forEach>
</select>
这篇关于从servlet显示数据到下拉菜单jsp的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!