1 package edu.fjnu.controller;
2
3 import java.io.IOException;
4 import java.io.PrintWriter;
5 import java.util.List;
6
7 import javax.servlet.ServletException;
8 import javax.servlet.http.HttpServlet;
9 import javax.servlet.http.HttpServletRequest;
10 import javax.servlet.http.HttpServletResponse;
11
12 import edu.fjnu.domain.Student;
13 import edu.fjnu.service.StudentService;
14 import edu.fjnu.service.StudentServiceImpl;
15
16 public class StudentMgrServlet extends HttpServlet {
17
18 /**
19 * Constructor of the object.
20 */
21 public StudentMgrServlet() {
22 super();
23 }
24
25 /**
26 * The doGet method of the servlet. <br>
27 *
28 * This method is called when a form has its tag value method equals to get.
29 *
30 * @param request the request send by the client to the server
31 * @param response the response send by the server to the client
32 * @throws ServletException if an error occurred
33 * @throws IOException if an error occurred
34 */
35 public void doGet(HttpServletRequest request, HttpServletResponse response)
36 throws ServletException, IOException {
37
38 request.setCharacterEncoding("utf-8");
39 int stuCnt = Integer.parseInt(request.getParameter("stucnt"));
40
41 StudentService stuService = new StudentServiceImpl();
42 List<Student> stuList = stuService.loadStus(stuCnt);
43
44 request.setAttribute("stus", stuList);
45 request.getRequestDispatcher("list_student.jsp").forward(request, response);
46
47 // for(Student stu:stuList)
48 // System.out.println(stu);
49 }
50
51 /**
52 * The doPost method of the servlet. <br>
53 *
54 * This method is called when a form has its tag value method equals to post.
55 *
56 * @param request the request send by the client to the server
57 * @param response the response send by the server to the client
58 * @throws ServletException if an error occurred
59 * @throws IOException if an error occurred
60 */
61 public void doPost(HttpServletRequest request, HttpServletResponse response)
62 throws ServletException, IOException {
63
64 doGet(request, response);
65 }
66
67 }
1 /**
2 *
3 */
4 package edu.fjnu.domain;
5
6 /**
7 * @author Administrator
8 *
9 */
10 public class Student {
11 private Integer stuNO;
12 private String stuName;
13 private Integer stuAge;
14 /**
15 * @return the stuNO
16 */
17 public Integer getStuNO() {
18 return stuNO;
19 }
20 /**
21 * @param stuNO the stuNO to set
22 */
23 public void setStuNO(Integer stuNO) {
24 this.stuNO = stuNO;
25 }
26 /**
27 * @return the stuName
28 */
29 public String getStuName() {
30 return stuName;
31 }
32 /**
33 * @param stuName the stuName to set
34 */
35 public void setStuName(String stuName) {
36 this.stuName = stuName;
37 }
38 /**
39 * @return the stuAge
40 */
41 public Integer getStuAge() {
42 return stuAge;
43 }
44 /**
45 * @param stuAge the stuAge to set
46 */
47 public void setStuAge(Integer stuAge) {
48 this.stuAge = stuAge;
49 }
50 /* (non-Javadoc)
51 * @see java.lang.Object#toString()
52 */
53 @Override
54 public String toString() {
55 return "Student [stuNO=" + stuNO + ", stuName=" + stuName + ", stuAge="
56 + stuAge + "]";
57 }
58
59
60 }
1 /**
2 *
3 */
4 package edu.fjnu.service;
5
6 import java.util.List;
7
8 import edu.fjnu.domain.Student;
9
10
11
12 /**
13 * @author Administrator
14 *
15 */
16 public interface StudentService {
17 List<Student> loadStus(int stuCnt);
18 }
1 /**
2 *
3 */
4 package edu.fjnu.service;
5
6 import java.util.ArrayList;
7 import java.util.List;
8 import java.util.Random;
9
10 import edu.fjnu.domain.Student;
11
12 /**
13 * @author Administrator
14 *
15 */
16 public class StudentServiceImpl implements StudentService {
17
18 /* (non-Javadoc)
19 * @see edu.fjnu.service.StudentService#loadall()
20 */
21 @Override
22 public List<Student> loadStus(int stuCnt) {
23 Random r = new Random();
24 List<Student> stuList = new ArrayList<Student>();
25
26 for(int i=0;i<stuCnt;i++){
27 Student stu = new Student();
28 stu.setStuNO(r.nextInt(3000));
29 stu.setStuName("mary"+r.nextInt(100));
30 stu.setStuAge(r.nextInt(50));
31 stuList.add(stu);
32 }
33 return stuList;
34 }
35
36 }
![]()
1 /**
2 *
3 */
4 package edu.fjnu.test;
5
6 import java.util.List;
7
8 import edu.fjnu.domain.Student;
9 import edu.fjnu.service.StudentService;
10 import edu.fjnu.service.StudentServiceImpl;
11
12 /**
13 * @author Administrator
14 *
15 */
16 public class StudentTester {
17
18 /**
19 * @param args
20 */
21 public static void main(String[] args) {
22 StudentService stuService = new StudentServiceImpl();
23 List<Student> stuList = stuService.loadStus(30);
24
25 for(Student stu:stuList)
26 System.out.println(stu);
27
28 }
29
30 }
StudentTester.java
1 <?xml version="1.0" encoding="UTF-8"?>
2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
3 <display-name>JspWorkPrj</display-name>
4 <servlet>
5 <servlet-name>StudentMgrServlet</servlet-name>
6 <servlet-class>edu.fjnu.controller.StudentMgrServlet</servlet-class>
7 </servlet>
8
9 <servlet-mapping>
10 <servlet-name>StudentMgrServlet</servlet-name>
11 <url-pattern>/stuMgr</url-pattern>
12 </servlet-mapping>
13 <welcome-file-list>
14 <welcome-file>index.html</welcome-file>
15 <welcome-file>index.htm</welcome-file>
16 <welcome-file>index.jsp</welcome-file>
17 <welcome-file>default.html</welcome-file>
18 <welcome-file>default.htm</welcome-file>
19 <welcome-file>default.jsp</welcome-file>
20 </welcome-file-list>
21 </web-app>
1 <!DOCTYPE html>
2 <html>
3 <head>
4 <title>index.html</title>
5 <meta charset="utf-8">
6 <meta name="keywords" content="keyword1,keyword2,keyword3">
7 <meta name="description" content="this is my page">
8 <meta name="content-type" content="text/html; charset=UTF-8">
9
10 <!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
11
12 </head>
13
14 <body>
15 <h3>请输入要生成的学生数量 </h3>
16 <form action="stuMgr" method="post">
17 <div>
18 <span>学生数量</span>
19 <input type = "text" name = "stucnt">
20 </div>
21 <div>
22 <input type = "submit" value = "提交">
23 </div>
24 </form>
25 </body>
26 </html>
1 <%@ page language="java" import="java.util.*,edu.fjnu.domain.Student" pageEncoding="utf-8"%>
2 <%
3 String path = request.getContextPath();
4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
5
6 List<Student> stuList =(List<Student>)request.getAttribute("stus");
7 %>
8
9 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
10 <html>
11 <head>
12 <base href="<%=basePath%>">
13
14 <title>My JSP 'list_student.jsp' starting page</title>
15 <meta charset="utf-8">
16 <meta http-equiv="pragma" content="no-cache">
17 <meta http-equiv="cache-control" content="no-cache">
18 <meta http-equiv="expires" content="0">
19 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
20 <meta http-equiv="description" content="This is my page">
21 <!--
22 <link rel="stylesheet" type="text/css" href="styles.css">
23 -->
24 <style>
25 table{
26 width:50%;
27 margin:10px auto;
28 }
29
30 th,td{
31 text-align:center;
32 }
33
34
35 </style>
36
37 <script>
38 function removeRow(tableRow,stuName){
39 if(confirm("您确定要删除学生"+stuName+" 的记录吗?"))
40 tableRow.parentNode.deleteRow(tableRow.rowIndex);
41
42 }
43
44 </script>
45 </head>
46
47 <body>
48 <h3>学生名单如下:(共计<%=stuList.size()%>人)</h3>
49 <hr>
50
51 <table border="1" cellspacing="0">
52 <tr>
53 <th>学号</th>
54 <th>姓名</th>
55 <th>年龄</th>
56 <th>操作</th>
57 </tr>
58 <%for(Student stu:stuList){%>
59 <tr>
60 <td><%=stu.getStuNO()%></td>
61 <td><%=stu.getStuName()%></td>
62 <td><%=stu.getStuAge()%></td>
63 <td>
64 <button>修改</button>
65 <button onclick="removeRow(this.parentNode.parentNode,'<%=stu.getStuName()%> ');">删除</button>
66 </td>
67 </tr>
68 <%} %>
69 </table>
70 </body>
71 </html>
![]()
![]()