練習:jsp輸出一個表格,裏面包含10個學生信息並模擬搜索功能

思路分析

代碼如下:

test2.jsp

<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2020/4/21
  Time: 15:19
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>test2</title>
</head>
<body>
<a href="http://localhost:8080/08_jsp/studentSearchServlet">搜索</a>
</body>
</html>

Student.java

package com.pojo;

/**
 * @author LIXICHEN
 * @create 2020-04-21 14:42
 */
public class Student {
    private Integer id;
    private String name;
    private Integer age;
    private String phone;


    public Student() {
    }

    public Student(Integer id, String name, Integer age, String phone) {
        this.id = id;
        this.name = name;
        this.age = age;
        this.phone = phone;
    }


    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    @Override
    public String toString() {
        return "Student{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age=" + age +
                ", phone='" + phone + '\'' +
                '}';
    }
}

StudentSeatchServlet.java

package com.study.servlet;

import com.pojo.Student;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author LIXICHEN
 * @create 2020-04-21 15:09
 */
public class StudentSearchServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //獲取請求的參數
        //發 sql 語句查詢學生信息
        //使用for循環生成查詢到的結果做模擬
        List<Student> students = new ArrayList<Student>();
        for (int i = 0; i < 10; i++) {
            int t = i + 1;
            students.add(new Student(t, "name" + t, 18 + t, "phone" + t));
        }
        //保存查詢到的結果 到 request 域中
        req.setAttribute("studentList",students);
        //請求轉發 到 showStudent.jsp 頁面
        req.getRequestDispatcher("/test/showStudent.jsp").forward(req,resp);



    }
}

web.xml

    <servlet>
        <servlet-name>StudentSearchServlet</servlet-name>
        <servlet-class>com.study.servlet.StudentSearchServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>StudentSearchServlet</servlet-name>
        <url-pattern>/studentSearchServlet</url-pattern>
    </servlet-mapping>

showStudent.jsp

<%@ page import="java.util.List" %>
<%@ page import="com.pojo.Student" %>
<%@ page import="java.util.ArrayList" %><%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2020/4/21
  Time: 14:40
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>學生信息</title>
    <style type="text/css">

        table{
            border: 1px blue solid;
            width: 600px;
            border-collapse: collapse;
        }
        td,tr,th{
            border: 1px blue solid;
        }

    </style>
</head>
<body>

<%
    List<Student> students = (List<Student>) request.getAttribute("studentList");
%>
<table>

    <tr>
        <th>編號</th>
        <th>姓名</th>
        <th>年齡</th>
        <th>電話</th>
        <th>操作</th>
    </tr>

    <%for (Student student : students) {%>

    <tr align="center">
        <td><%=student.getId()%></td>
        <td><%=student.getName()%></td>
        <td><%=student.getAge()%></td>
        <td><%=student.getPhone()%></td>
        <td>
            <a href="">刪除</a>
            <a href="">修改</a>
        </td>
    </tr>

    <% } %>
</table>

</body>
</html>

結果展示:

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章