Home > Back-end >  The JSP using c: foreach tag display data
The JSP using c: foreach tag display data

Time:01-26

I have posted the code, first is the method of dao layer
Public List SelectStudent () {
List StudentList=new ArrayList (a);//create the collection
The Connection conn=DataBaseConnection. GetConnection ();
String SQL="select * from student";//SQL query
Try {
PreparedStatement PST=conn. PrepareStatement (SQL);
The ResultSet rs=PST. ExecuteQuery ();
While (rs), next ()) {
Student Student=new Student ();
Student. SetId (rs. Get int (" id "));//get ID
Stuno student. SetStuno (rs. Get string (" "));
Student. SetStuname (rs. Get string (" stuname "));
Stusex student. SetStusex (rs. Get string (" "));
Stubirth student. SetStubirth (rs. Get string (" "));
Stuclass student. SetStuclass (rs. Get string (" "));
Stumajor student. SetStumajor (rs. Get string (" "));
Studept student. SetStudept (rs. Get string (" "));
Stuphone student. SetStuphone (rs. Get string (" "));
StudentList. Add (student);
}
Rs. The close ();//close
PST. The close ();//close
} the catch (SQLException e) {
e.printStackTrace();//throw an exception
}
Return the studentList;//return a collection
}
And then the servlet
Public class selectStudentServlet extends the HttpServlet {

private static final long serialVersionUID=1L;

@ Override
Protected void doGet (it request, HttpServletResponse response) throws ServletException, IOException {
Enclosing the doPost (request, response);
}

@ Override
Protected void doPost (it request, HttpServletResponse response) throws ServletException, IOException {
StudentDao dao=new StudentDao ();
List StudentList=dao. SelectStudent ();
The HttpSession session=request. GetSession ();
The session. The setAttribute (" studentList ", studentList);
Request. GetRequestDispatcher (" student/student_all. JSP "). The forward (request, response);
}
}
The last is the code for the JSP page
<% @ page contentType="text/HTML. Charset=utf-8 "language=" Java "isELIgnored=" false "% & gt;
<% @ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" % & gt;
<%
String path=request. GetContextPath ();
String=basePath request. GetScheme () + "://" + request. GetServerName () + ":" + request. GetServerPort () + + "/" path;
% & gt;


Look at all the students & lt;/title> <br/></head> <br/><body> <br/><Table> <br/><Thead> <br/><tr> <br/><Td align="center" & gt; Student id & lt;/td> <br/><Td align="center" & gt; Name & lt;/td> <br/><Td align="center" & gt; Gender & lt;/td> <br/><Td align="center" & gt; Date of birth & lt;/td> <br/><Td align="center" & gt; Class & lt;/td> <br/><Td align="center" & gt; Professional & lt;/td> <br/><Td align="center" & gt; College & lt;/td> <br/><Td align="center" & gt; Telephone & lt;/td> <br/></tr> <br/></thead> <br/><Tbody> <br/><C: forEach items="${requestScope. StudentList}" var="student" & gt; <br/><tr> <br/><td> The ${student. Stuno} & lt;/td> <br/><td> The ${student. Stuname} & lt;/td> <br/><td> The ${student. Stusex} & lt;/td> <br/><td> The ${student. Stubirth} & lt;/td> <br/><td> The ${student. Stuclass} & lt;/td> <br/><td> The ${student. Stumajor} & lt;/td> <br/><td> The ${student. Studept} & lt;/td> <br/><td> The ${student. Stuphone} & lt;/td> <br/><td> <br/><A href="https://bbs.csdn.net/topics/deleteStudentServlet? Id=${. Student id} "& gt; Delete & lt;/a> <br/><A href="https://bbs.csdn.net/topics/updateStudentServlet? Id=${. Student id} "& gt; Modify & lt;/a> <br/></td> <br/></tr> <br/></c: forEach> <br/><tr> <br/><Td colspan="8" style="text - align: left;"> <A href="https://bbs.csdn.net/topics/student/student_add.jsp" & gt; Add & lt;/a> </td> <br/></tr> <br/></tbody> <br/></table> <br/></body> <br/></html> <br/>C: run after foreach tag data, have a big help <br/>I am using intellij idea<p class="article - content rp"> CodePudding user response: </p>Long time no use JSP, <br/>Under the first confirmed servlet List<Student> StudentList=dao. SelectStudent (); Is there a data <br/>Then the JSP page ${requestScope. StudentList} to ${studentList} try<p class="article - content rp"> CodePudding user response: </p><C: forEach items="${requestScope. StudentList}" var="student" & gt; Change & lt; C: forEach items="${studentList}" var="student" & gt; </div> <div class="th_page th_page_color"></div> <div class="umCopyright"> <p>Page link:<a href="/Backend/99849.html" target="_blank" style="color:#999">https//www.codepudding.com/Backend/99849.html</a></p> </div> <div class="detail-arr"> <div class="detail-arr-left">Prev:<a href='/Backend/99848.html'>Password displayed as * * * * *</a></div> <div class="detail-arr-right">Next:<a href='/Backend/99850.html'>jQuery</a></div> </div> </div> </div> </div> </div> <div class="container th_top"> <div class="row"> <div class="col-md-12"> <div class="hot-tags neitags"> <ul> <li><i class="iconfont icon-x-tags"></i> Tags:  </li> <a href='/e/tags/?tagname=Web+development' target='_blank'>Web development</a> </ul> </div> </div> </div> </div> <div class="container th_top"> <div class="row"> <div class="col-md-12"> <div class="xiangguan"> <ul class="msg msghead"> <li class="tbname">Related</li> </ul> <ul> </ul> </div> </div> </div> </div> <div class="container th_top"> <div class="row"> <div class="col-md-12"> <div class="flinks"> <ul> <li><i class="iconfont icon-x-tags"></i> Links:  </li> <li class="liflinks"><a target="_blank" href="/" title="CodePudding">CodePudding</a></li> </ul> </div> </div> </div> </div> <div class="footer"> <p><span style="font-size:16px;color:#666;font-weight: bold">About Us:</span>  <a href="https://www.codepudding.com/contact.html">Contact Us</a>      <a href="https://www.codepudding.com/service.html">Terms of Service</a>      <a href="https://www.codepudding.com/privacy.html"> Privacy Policy</a></p> <p class="foot_info">Copyright © 2010-2023,Powered By <a href="/" target="_blank">CodePudding</a> </p> </div> <script type="text/javascript" src="/skin/code/tianhu.js"></script> </body></html>