本文介绍了如何使用servlet在jsp中填充下拉列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是jsp和servlets的初学者。我想留在同一页面并填充下拉列表,但我不能这样做。我是用scriplet做的,它发生了但是很久以前就弃用了scriplets。我只是想了解更多关于web.xml中的servlet映射。我猜这个映射有问题。请尽快帮助。
我的尝试:
.jsp
< form>
< select name =sourcestyle =width:180 px>< / select>
< c:forEach items =$ {slist}var =ls>
< option> < c:out value =$ {ls}/>< / option>
< / c:forEach>
< / form>
在Servlet中
protected void doGet(HttpServletRequest request,HttpServletResponse response)抛出ServletException,IOException {
try {
Class.forName(com.mysql.cj.jdbc.Driver);
Connection con = DriverManager.getConnection(
jdbc:mysql:// localhost:3306 / dbname,root,password);
语句stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(select * from City);
while(rs.next())
{
i = rs.getInt(1);
val = rs.getString(2);
ar.add(val);
}
request.setAttribute(slist,ar);
request.getRequestDispatcher(/ WebContent / Home.jsp)。forward(request,response);
//rd.forward(req,res);
//System.out.println(rs.getInt(1)++ rs.getString(2)+);
con.close();
} catch(例外e){System.out.println(e);}
最后
{
}
}
解决方案
I am a beginner in jsp and servlets. I want to stay in the same page and populate the dropdownlist but iam not able to so. I did it using scriplets and it happened but scriplets are deprecated long back. I just want to know more about servlet mapping in web.xml. I guess something is wrong with the mapping. Please somebody help asap.
What I have tried:
Home.jsp
<form> <select name="source" style="width:180 px"></select> <c:forEach items="${slist}" var="ls"> <option> <c:out value="${ls}"/></option> </c:forEach> </form>
In Servlet
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try{ Class.forName("com.mysql.cj.jdbc.Driver"); Connection con=DriverManager.getConnection( "jdbc:mysql://localhost:3306/dbname","root","password"); Statement stmt=con.createStatement(); ResultSet rs=stmt.executeQuery("select * from City"); while(rs.next()) { i=rs.getInt(1); val=rs.getString(2); ar.add(val); } request.setAttribute("slist",ar); request.getRequestDispatcher("/WebContent/Home.jsp").forward(request, response); //rd.forward(req,res); //System.out.println(rs.getInt(1)+" "+rs.getString(2)+" "); con.close(); }catch(Exception e){ System.out.println(e);} finally { } }
解决方案
这篇关于如何使用servlet在jsp中填充下拉列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!