jsp中提供了el表达式,可以获取服务端域对象中的数据

    el表达式语法${后端域对象中的key}

    JSTL:JSP标准标签库,也称为c标签

    jsp页面中导入jstl标签库

    <%@ taglib prefix=”c” uri=”http://java.sun.com/jsp/jstl/core“%>

    用jstl标签对集合内容遍历并排版

    jsp语法

    1. <table></table>表格
    2. <tr></tr> 表格中的行
    3. <td></td> tr的子标签,表示一行中的一个单元
    4. <c:forEach> </c:forEach> jstl提供的遍历标签
    5. items 要遍历的对象。要遍历对象从域对象中获取,所以要通过el表达式从域对象中获取
    6. var 遍历的每个元素,用来存放当前指向的元素
    7. border 边框,表格的属性
    8. align="center" 表格居中
    9. <th></th> 会对内容进行加粗
    10. <h1 align="center"> 学生管理页面</h1>标题标签
    1. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    2. <%--导入jstl标签库--%>
    3. <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
    4. <html>
    5. <head>
    6. <title>学生列表</title>
    7. </head>
    8. <body>
    9. <%--jsp中提供了el表达式,可以获取服务端域对象中的数据--%>
    10. <%--
    11. el表达式语法
    12. ${后端域对象中的key}
    13. --%>
    14. <%--在当前jsp页面中导入jstl标签(也称之为c标签)--%>
    15. <%--${stuList}--%>
    16. <h1 align="center"> 学生管理页面</h1>
    17. <table border="1px" align="center">
    18. <tr>
    19. <th>学生姓名</th>
    20. <th>学生年龄</th>
    21. <th>学生性别</th>
    22. <th>学生地址</th>
    23. <th>学生生日</th>
    24. <th>学生爱好</th>
    25. </tr>
    26. <c:forEach items="${stuList}" var="stu">
    27. <tr>
    28. <td>${stu.sname}</td>
    29. <td>${stu.age}</td>
    30. <td>${stu.sex}</td>
    31. <td>${stu.address}</td>
    32. <td>${stu.birthday}</td>
    33. <td>${stu.hobby}</td>
    34. </tr>
    35. </c:forEach>
    36. </table>
    37. </body>
    38. </html>

    运行结果如图
    Java学习十二——web案例(一) - 图1
    web具体代码如下:
    Student.java

    1. package com.jy.pojo;
    2. public class Student {
    3. private Integer sid;
    4. private String sname;
    5. private Integer age;
    6. private Integer sex;
    7. private String address;
    8. private String birthday;
    9. private String hobby;
    10. public Student(Integer sid, String sname, Integer age, Integer sex, String address, String birthday, String hobby) {
    11. this.sid = sid;
    12. this.sname = sname;
    13. this.age = age;
    14. this.sex = sex;
    15. this.address = address;
    16. this.birthday = birthday;
    17. this.hobby = hobby;
    18. }
    19. public Student() {
    20. }
    21. public Integer getSid() {
    22. return sid;
    23. }
    24. public String getSname() {
    25. return sname;
    26. }
    27. public Integer getAge() {
    28. return age;
    29. }
    30. public Integer getSex() {
    31. return sex;
    32. }
    33. public String getAddress() {
    34. return address;
    35. }
    36. public String getBirthday() {
    37. return birthday;
    38. }
    39. public String getHobby() {
    40. return hobby;
    41. }
    42. public void setSid(Integer sid) {
    43. this.sid = sid;
    44. }
    45. public void setSname(String sname) {
    46. this.sname = sname;
    47. }
    48. public void setAge(Integer age) {
    49. this.age = age;
    50. }
    51. public void setSex(Integer sex) {
    52. this.sex = sex;
    53. }
    54. public void setAddress(String address) {
    55. this.address = address;
    56. }
    57. public void setBirthday(String birthday) {
    58. this.birthday = birthday;
    59. }
    60. public void setHobby(String hobby) {
    61. this.hobby = hobby;
    62. }
    63. @Override
    64. public String toString() {
    65. return "Student{" +
    66. "sid=" + sid +
    67. ", sname='" + sname + '\'' +
    68. ", age=" + age +
    69. ", sex=" + sex +
    70. ", address='" + address + '\'' +
    71. ", birthday='" + birthday + '\'' +
    72. ", hpbby='" + hobby + '\'' +
    73. '}';
    74. }
    75. }

    StudentServlet.java

    1. package com.jy.servlet;
    2. import com.jy.pojo.Student;
    3. import com.jy.util.JDBCUtils;
    4. import org.apache.taglibs.standard.tag.common.core.ChooseTag;
    5. import org.omg.CosNaming.NamingContextExtPackage.StringNameHelper;
    6. import javax.servlet.RequestDispatcher;
    7. import javax.servlet.ServletException;
    8. import javax.servlet.http.HttpServlet;
    9. import javax.servlet.http.HttpServletRequest;
    10. import javax.servlet.http.HttpServletResponse;
    11. import java.io.IOException;
    12. import java.sql.*;
    13. import java.util.ArrayList;
    14. public class StudentServlet extends HttpServlet {
    15. @Override
    16. protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    17. doPost(req,resp);
    18. }
    19. @Override
    20. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    21. //处理中文乱码
    22. request.setCharacterEncoding("utf-8");
    23. //接收前端传递的标识,决定具体执行的方法
    24. String flag = request.getParameter("flag");
    25. if(flag != null && flag.equals("list")){
    26. getList(request,response);
    27. }else if(flag != null &&flag.equals("add")){
    28. getAdd(request,response);
    29. }else if(flag != null && flag.equals("delete")){
    30. getDelete(request,response);
    31. }
    32. }
    33. /*
    34. * 根据id进行删除
    35. * */
    36. private void getDelete(HttpServletRequest request, HttpServletResponse response) {
    37. //接收id值
    38. String id = request.getParameter("id");
    39. Connection connection =null;
    40. PreparedStatement preparedStatement = null;
    41. //jdbc执行访问数据库执行删除
    42. try {
    43. connection = JDBCUtils.getConnection();
    44. preparedStatement = connection.prepareStatement("delete from tb_student where id= ?");
    45. preparedStatement.setInt(1,Integer.parseInt(id));
    46. int i = preparedStatement.executeUpdate();
    47. if(i>0){
    48. System.out.println("删除成功");
    49. response.sendRedirect("http://localhost:8080/managerWeb/stu?flag=list");
    50. }else{
    51. System.out.println("删除失败");
    52. }
    53. }catch(Exception e){
    54. e.printStackTrace();
    55. }finally {
    56. JDBCUtils.JDBCClose(connection,preparedStatement,null);
    57. }
    58. }
    59. //执行添加的方法
    60. public void getAdd(HttpServletRequest request, HttpServletResponse response) {
    61. //接收客户端数据
    62. String sname = request.getParameter("sname");
    63. String age = request.getParameter("age");
    64. String sex = request.getParameter("sex");
    65. String address = request.getParameter("address");
    66. String birthday = request.getParameter("birthday");
    67. //接收多个值
    68. String[] hobbies = request.getParameterValues("hobby");
    69. //测试数据
    70. // System.out.println(sname+" " +age+ " "+sex+" "+ address+" "+birthday);
    71. String hobbys = "";
    72. //遍历爱好数组
    73. for (String hobby : hobbies) {
    74. hobbys += ","+hobby;
    75. }
    76. //字符串切割
    77. hobbys=hobbys.substring(1);
    78. Connection connection = null;
    79. PreparedStatement preparedStatement = null;
    80. //使用jdbc保存数据到数据库
    81. try {
    82. //获取数据库连接
    83. connection = JDBCUtils.getConnection();
    84. //
    85. String sql = "insert tb_student(sname,age,sex,address,birthday,hobby)
    86. values(?,?,?,?,?,?)";
    87. //获取数据库操作对象
    88. preparedStatement = connection.prepareStatement(sql);
    89. //给占位符赋值
    90. preparedStatement.setString(1,sname);
    91. preparedStatement.setInt(2,Integer.parseInt(age));
    92. preparedStatement.setInt(3,Integer.parseInt(sex));
    93. preparedStatement.setString(4,address);
    94. preparedStatement.setString(5,birthday);
    95. int i = preparedStatement.executeUpdate();
    96. if(i>0){
    97. System.out.println("success");
    98. //请求重定向
    99. response.sendRedirect("http://localhost:8080/managerWeb/stu?flag=list");
    100. }else {
    101. System.out.println("defeat");
    102. }
    103. } catch (ClassNotFoundException e) {
    104. e.printStackTrace();
    105. } catch (SQLException throwables) {
    106. throwables.printStackTrace();
    107. } catch (IOException e) {
    108. e.printStackTrace();
    109. } finally {
    110. JDBCUtils.JDBCClose(connection,preparedStatement,null);
    111. }
    112. }
    113. //具体实现学生列表的方法
    114. public void getList(HttpServletRequest request, HttpServletResponse response){
    115. /**
    116. * 1、通过jdbc访问数据库
    117. */
    118. Connection connection =null;
    119. PreparedStatement preparedStatement = null;
    120. ResultSet resultSet = null;
    121. try{
    122. //注册驱动
    123. Class.forName("com.mysql.cj.jdbc.Driver");
    124. //获取数据库连接对象
    125. connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/test_stu?serverTimezone=GMT", "root", "root");
    126. //获取数据库操作对象,并预编译sql
    127. preparedStatement = connection.prepareStatement("select * from tb_student");
    128. //执行SQL语句
    129. resultSet = preparedStatement.executeQuery();
    130. ArrayList<Student> stuList = new ArrayList<>();
    131. //遍历结果集
    132. while(resultSet.next()){
    133. //根据表字段取出数据
    134. String sname = resultSet.getString("sname");
    135. int age = resultSet.getInt("age");
    136. int sex = resultSet.getInt("sex");
    137. String address = resultSet.getString("address");
    138. String birthday = resultSet.getString("birthday");
    139. String hobby = resultSet.getString("hobby");
    140. //取出ID,用于删除与修改
    141. int id = resultSet.getInt("id");
    142. System.out.println(sname+ " "+ age+" "+sex+ " "+ address+" "+birthday+" "+hobby);
    143. //创建学生对象
    144. Student student = new Student();
    145. //给对象赋值
    146. student.setSname(sname);
    147. student.setAge(age);
    148. student.setSex(sex);
    149. student.setBirthday(birthday);
    150. student.setAddress(address);
    151. student.setHobby(hobby);
    152. student.setSid(id);
    153. //添加学生对象到集合
    154. stuList.add(student);
    155. }
    156. //System.out.println("集合的长度为 "+stuList.size());
    157. //遍历集合数据
    158. for (Student student:stuList){
    159. System.out.println(student);
    160. }
    161. /**
    162. * 二、响应对象到客户端
    163. * 1、把集合对象存放到域中
    164. * 2、请求转发跳转资源
    165. */
    166. //存放集合对象到域中
    167. request.setAttribute("stuList",stuList);
    168. //请求转发
    169. RequestDispatcher requestDispatcher = request.getRequestDispatcher("/jsp/list.jsp");
    170. //走向具体的资源
    171. requestDispatcher.forward(request,response);
    172. }catch(Exception e){
    173. e.printStackTrace();
    174. }finally {
    175. //关闭数据库资源
    176. if(resultSet !=null){
    177. try {
    178. resultSet.close();
    179. } catch (SQLException throwables) {
    180. throwables.printStackTrace();
    181. }
    182. }
    183. if(preparedStatement!=null){
    184. try {
    185. preparedStatement.close();
    186. } catch (SQLException throwables) {
    187. throwables.printStackTrace();
    188. }
    189. }
    190. if(connection !=null){
    191. try {
    192. connection.close();
    193. } catch (SQLException throwables) {
    194. throwables.printStackTrace();
    195. }
    196. }
    197. }
    198. }
    199. }

    JDBCUtils.java

    1. package com.jy.util;
    2. import java.sql.*;
    3. /**
    4. *
    5. * 封装jdbc工具类
    6. * 1、构造方法私有化,不希望工具类可以实例化对象
    7. *
    8. * 2、所有调用的方法都用static关键字修饰
    9. */
    10. public class JDBCUtils {
    11. private JDBCUtils(){}
    12. /**
    13. *
    14. * 把注册驱动写在静态代码块,静态代码块在类加载器的时候执行且只执行一次
    15. */
    16. static {
    17. //注册驱动
    18. try {
    19. Class.forName("com.mysql.cj.jdbc.Driver");
    20. } catch (ClassNotFoundException e) {
    21. e.printStackTrace();
    22. }
    23. }
    24. /**
    25. * 获取数据库连接
    26. */
    27. public static Connection getConnection() throws ClassNotFoundException, SQLException {
    28. //获取连接
    29. Connection connection =
    30. DriverManager.getConnection("jdbc:mysql://localhost:3306/test_stu?serverTimezone=GMT","root","root");
    31. return connection;
    32. }
    33. /**
    34. *
    35. * 关闭数据库资源
    36. */
    37. public static void JDBCClose(Connection connection, Statement statement, ResultSet resultSet) {
    38. //关闭结果集对象
    39. if(resultSet !=null){
    40. try {
    41. resultSet.close();
    42. } catch (SQLException throwables) {
    43. throwables.printStackTrace();
    44. }
    45. }
    46. //关闭数据库操作对象
    47. if(statement != null){
    48. try {
    49. statement.close();
    50. } catch (SQLException throwables) {
    51. throwables.printStackTrace();
    52. }
    53. }
    54. //关闭数据库连接对象
    55. if(connection != null){
    56. try {
    57. connection.close();
    58. } catch (SQLException throwables) {
    59. throwables.printStackTrace();
    60. }
    61. }
    62. }
    63. }

    list.jsp

    1. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    2. <%--导入jstl标签库--%>
    3. <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
    4. <html>
    5. <head>
    6. <title>学生列表</title>
    7. </head>
    8. <body>
    9. <h1 align="center"> 学生管理页面</h1>
    10. <table border="1px" align="center">
    11. <tr>
    12. <td colspan="8" align="center">
    13. <a href="http://localhost:8080/managerWeb/jsp/add.jsp">添加学生</a></td>
    14. </tr>
    15. <tr>
    16. <th>学生姓名</th>
    17. <th>学生年龄</th>
    18. <th>学生性别</th>
    19. <th>学生地址</th>
    20. <th>学生生日</th>
    21. <th>学生爱好</th>
    22. <th>操作</th>
    23. </tr>
    24. <c:forEach items="${stuList}" var="stu">
    25. <tr>
    26. <td>${stu.sname}</td>
    27. <td>${stu.age}</td>
    28. <td>${stu.sex}</td>
    29. <td>${stu.address}</td>
    30. <td>${stu.birthday}</td>
    31. <td>${stu.hobby}</td>
    32. <td>
    33. <a href="http://localhost:8080/managerWeb/stu?flag=delete&id=${stu.sid}">删除记录</a>
    34. <a href="http://localhost:8080/managerWeb/stu?flag=delete">编辑</a>
    35. </td>
    36. </tr>
    37. </c:forEach>
    38. </table>
    39. </body>
    40. </html>

    add.jsp

    1. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    2. <html>
    3. <head>
    4. <title>添加学生</title>
    5. </head>
    6. <body>
    7. <h1 align="center"> 学生添加页面</h1>
    8. <%--
    9. <input type="radio"> 单选框
    10. <input type="checkbox"> 多选框
    11. <input type="date"> 日期选择
    12. name 用于分组
    13. 文本输入框的name属性将作为获取数据的key
    14. --%>
    15. <form action="http://localhost:8080/managerWeb/stu?flag=add" method="post">
    16. <table align="center">
    17. <tr>
    18. <td>学生姓名:</td>
    19. <td>
    20. <input type="text" name="sname">
    21. </td>
    22. </tr>
    23. <tr>
    24. <td>学生年龄:</td>
    25. <td>
    26. <input type="text" name="age">
    27. </td>
    28. </tr>
    29. <tr>
    30. <td>学生性别:</td>
    31. <td>
    32. 男:<input type="radio" name="sex" value="1">
    33. 女:<input type="radio" name="sex" value="2">
    34. </td>
    35. </tr>
    36. <tr>
    37. <td>学生住址:</td>
    38. <td>
    39. <input type="text" name="address">
    40. </td>
    41. </tr>
    42. <tr>
    43. <td>生日:</td>
    44. <td>
    45. <input type="date" name="birthday">
    46. </td>
    47. </tr>
    48. <tr>
    49. <td>爱好:</td>
    50. <td>
    51. 下棋:<input type="checkbox" name="hobby" value="下棋">
    52. 舞剑:<input type="checkbox" name="hobby" value="舞剑">
    53. 论道:<input type="checkbox" name="hobby" value="论道">
    54. 布阵:<input type="checkbox" name="hobby" value="布阵">
    55. 弄枪:<input type="checkbox" name="hobby" value="弄枪">
    56. </td>
    57. </tr>
    58. <tr align="center">
    59. <td colspan="10"><input type="submit" value="添加" ></td>
    60. </tr>
    61. </table>
    62. </form>
    63. </body>
    64. </html>