长沙建个网站一般需要多少钱,wordpress 数据导出,wordpress游戏,手机版网站开发框架版权声明
本文原创作者#xff1a;谷哥的小弟作者博客地址#xff1a;http://blog.csdn.net/lfdfhl 在本教程教程中#xff0c;我们实现学生列表的显示。
Student
请在bean包下创建Student类#xff0c;代码如下#xff1a;
package com.cn.bean;
/*** 本文作者#…
版权声明
本文原创作者谷哥的小弟作者博客地址http://blog.csdn.net/lfdfhl 在本教程教程中我们实现学生列表的显示。
Student
请在bean包下创建Student类代码如下
package com.cn.bean;
/*** 本文作者谷哥的小弟* 博客地址http://blog.csdn.net/lfdfhl*/
public class Student {private int id;private String name;private int age;private String gender;private String hobby;public Student() {}public Student(int id, String name, int age, String gender, String hobby) {this.id id;this.name name;this.age age;this.gender gender;this.hobby hobby;}public int getId() {return id;}public void setId(int id) {this.id id;}public String getName() {return name;}public void setName(String name) {this.name name;}public int getAge() {return age;}public void setAge(int age) {this.age age;}public String getGender() {return gender;}public void setGender(String gender) {this.gender gender;}public String getHobby() {return hobby;}public void setHobby(String hobby) {this.hobby hobby;}Overridepublic String toString() {return Student{ id id , name name \ , age age , gender gender \ , hobby hobby \ };}
}
图示如下 ShowStudentServlet
请在servlet包下创建ShowStudentServlet当查询到所有学生后将请求转发至学生列表页面。
代码如下
package com.cn.servlet;import com.cn.util.C3P0Utils;
import com.cn.bean.Student;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;WebServlet(name ShowStudentServlet, urlPatterns /ShowStudentServlet)
public class ShowStudentServlet extends HttpServlet {Overrideprotected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {doPost(request, response);}Overrideprotected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {ArrayListStudent studentArrayList new ArrayList();Connection connection null;PreparedStatement preparedStatement null;ResultSet resultSet null;try {connection C3P0Utils.getConnection();String sql SELECT * FROM student;preparedStatement connection.prepareStatement(sql);resultSet preparedStatement.executeQuery();while (resultSet.next()) {int id resultSet.getInt(id);String name resultSet.getString(name);int age resultSet.getInt(age);String gender resultSet.getString(gender);String hobby resultSet.getString(hobby);Student student new Student(id, name, age, gender, hobby);studentArrayList.add(student);}} catch (Exception e) {e.printStackTrace();} finally {C3P0Utils.release(connection, preparedStatement, resultSet);}request.setAttribute(students, studentArrayList);RequestDispatcher requestDispatcher request.getRequestDispatcher(/studentList.jsp);requestDispatcher.forward(request, response);}
}图示如下
学生列表页面
请在web文件夹下创建学生列表页面studentList.jsp代码如下
% taglib urihttp://java.sun.com/jsp/jstl/core prefixc %
% page contentTypetext/html;charsetUTF-8 languagejava %
html
headtitle学生列表/titlestyle typetext/csstr {text-align: center;}.out {display: inline-flex;}/stylescript typetext/javascriptfunction logout() {window.location.href ${pageContext.request.contextPath}/LogoutServlet;}function showStudent() {window.location.href ${pageContext.request.contextPath}/ShowStudentServlet;}function addStudent() {window.location.href ${pageContext.request.contextPath}/studentAdd.jsp;}function updateStudent(id) {window.location.href ${pageContext.request.contextPath}/QueryStudentServlet?id id;}function deleteStudent(id) {let flag window.confirm(您确定要删除吗);if (flag) {window.location.href ${pageContext.request.contextPath}/DeleteStudentServlet?id id;}}/script
/head
body background-color#e2e6c2
%--欢迎登录--%
div idwelcomeh5欢迎${user.username}/h5
/div
div aligncenterform action${pageContext.request.contextPath}/SearchStudentServlet methodpost%--搜索学生--%input typetext namecontent placeholder请输入姓名或学号input typesubmit value搜索/form%--新增学生--%input typebutton namecreate value新增 onclickaddStudent()/%--刷新学生列表--%input typebutton nameshowList value刷新 onclickshowStudent()/%--退出登录--%input typebutton namelogout value退出 onclicklogout()/
/div
br/%--学生列表--%
table border1 cellpadding5px cellspacing0px aligncentertrth学号/thth姓名/thth年龄/thth性别/thth爱好/thth操作/thth操作/th/trc:forEach varstudent items${students}trtd${student.id}/tdtd${student.name}/tdtd${student.age}/tdtd${student.gender}/tdtd${student.hobby}/tdtdinput idupdate typebutton value修改 onclickupdateStudent(${student.id})//tdtdinput iddelete typebutton value删除 onclickdeleteStudent(${student.id})//td/tr/c:forEach
/table
/body
/html
图示如下
学生列表显示效果如下图所示