searchStudent.jsp
资源名称:某公司的java培训教材 [点击查看]
上传用户:dinglihq
上传日期:2013-02-04
资源大小:99958k
文件大小:3k
源码类别:
Java编程
开发平台:
Java
- <%@ page contentType="text/html; charset=GBK" %>
- <%@ page language="java" %>
- <%@ page import="java.util.ArrayList" %>
- <%@ page import="java.util.Collection" %>
- <%@ page import="javax.ejb.ObjectNotFoundException" %>
- <%@ page import="javax.naming.InitialContext" %>
- <%@ page import="cmpsample.*" %>
- <html>
- <head><title>Chapter 11 CMP 2.0 Sample Example </title></head>
- <body bgcolor="white">
- <center>
- <h2>Chapter 11 CMP 2.0 StudentEJB - Search for a student entity and list courses and addresses</h2>
- Search for a Student:
- <p>
- <form method="get" action="/WebApp/searchStudent.jsp">
- Search by
- <select name="searchCriteria">
- <option value="studentID" selected>Student ID
- <option value="lastName">Last Name
- <option value="firstName">First Name
- </select>
- <input type="text" name="searchText" size="25">
- <p>
- <input type="submit" value="Search">
- </form>
- <%
- String text = request.getParameter("searchText");
- String criteria = request.getParameter("searchCriteria");
- if (text != null && !"".equals(text)) {
- try {
- InitialContext ic = new InitialContext();
- Object obj = ic.lookup("Student");
- StudentHome home = (StudentHome) obj;
- Collection Students = new ArrayList();
- if ("studentID".equals(criteria)) {
- try {
- Student student = home.findByPrimaryKey(text);
- Students.add(student);
- } catch (ObjectNotFoundException ex) {}
- }
- else if ("lastName".equals(criteria)) {
- Students = home.findByLastName(text);
- }
- else if ("firstName".equals(criteria)) {
- Students = home.findByFirstName(text);
- }
- else
- { }
- %>
- <hr>
- <p>
- Results of the search: <p>
- <%
- for (int i = 0; i < Students.size(); i++) {
- Student stud = (Student)((ArrayList)Students).get(i);
- String sid = (String) stud.getPrimaryKey();
- ArrayList rosterList = stud.getRosterList();
- %>
- <b> <%=stud.getFirstname()%> <%=stud.getLastname()%> </b> is registered in
- <%=stud.getRosterList().size()%> classes listed below: <p>
- <table border=2>
- <tr><th>Roster ID</th> <th>Schedule ID</th></tr>
- <%
- for (int j=0; j < rosterList.size(); j++)
- {
- Roster rost = (Roster) rosterList.get(j);
- %>
- <tr>
- <td> <%=rost.fetchRosterID()%> </td>
- <td> <%=rost.fetchScheduleID()%> </td>
- </tr>
- <%
- }
- %>
- </table>
- <p>
- and has
- <%=stud.getAddressList().size()%> addresses<p>
- <table border=3>
- <tr><th>Strret</th><th>city</th><th>State</th></tr>
- <% ArrayList list = stud.getAddressList();
- for (int k=0; k< list.size(); k++)
- {
- Address addr = (Address)list.get(k);
- %>
- <tr>
- <td><%=addr.getStreet()%></td>
- <td> <%=addr.getCity()%></td>
- <td> <%=addr.getState()%></td>
- </tr>
- <%
- }
- %>
- </table>
- <%
- }
- } catch(Exception e) {
- e.printStackTrace();
- out.println(e.toString());
- }
- }
- %>
- <hr>
- [<a href="/WebApp/index.html">HOME</a>]
- </center>
- </body>
- </html>