1.index.jsp

<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<!DOCTYPE html>
<html>
<head>
<title>JSP - Hello World</title>
</head>
<body>
<h1 style="text-align: center;color: red">学生系统
</h1>
<br/>
<h2><a href="${pageContext.request.contextPath}/Servlet?method=add">发布活动信息</a></h2>
<h2><a href="${pageContext.request.contextPath}/Servlet?method=liulan">浏览活动信息(删除、更改)</a></h2>
<h2><a href="${pageContext.request.contextPath}/Servlet?method=find">查询活动信息</a></h2>
</body>
</html>

2.add.jsp
<%--
Created by IntelliJ IDEA.
User: jiachenglin
Date: 2022/10/24
Time: 19:30
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>添加界面</title>
<h1 style="color: red;text-align: center">添加界面</h1>
</head>
<body>
<form action="${pageContext.request.contextPath}/Servlet" method="post">
<p>姓名:</p><br>
<input type="text" name="name" id="ne" width="200px"><span id="ne_err" style="color: red;display: none">输入错误</span>
<p>学号:</p><br>
<input type="text" name="id" id="id11" width="200px"><span id="id1_err" style="color: red;display: none">输入错误</span>
<p>性别:</p><br>
<input type="radio" name="gender" id="gen" value="男">男<br>
<input type="radio" name="gender" id="gen1" value="女">女<br>
<p>地址:</p><br>
<input type="text" name="address" id="add" width="200px"><span id="add_err" style="color: red;display: none">输入错误</span>
<input value="add" name="method" type="hidden">
<input type="submit" value="提交">

<script>
let name1=document.getElementById("ne")
name1.onblur=function (){
let name2=name1.value.trim();
if(name2.length==0){
document.getElementById("ne_err").style.display='';//中间一定无空格,有空格也不显示
}
else {
document.getElementById("ne_err").style.display='none';
}
}
let id1=document.getElementById("id11")
id1.onblur=function (){
let id2=id1.value.trim();
if(id2.length==0){
document.getElementById("id1_err").style.display='';
}
else
document.getElementById("id1_err").style.display='none';
}
let address1=document.getElementById("add")
address1.onblur=function (){
let address2=address1.value.trim();
if(address2.length==0){
document.getElementById("add_err").style.display='';
}
else
document.getElementById("add_err").style.display='none';
}
</script>
</form>
</body>

</html>
3.update.jsp
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%--
Created by IntelliJ IDEA.
User: jiachenglin
Date: 2022/10/25
Time: 12:20
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<% int num= Integer.parseInt(request.getParameter("num"));%>
<title>更新界面</title>
<h1 style="color: red;text-align: center">更改信息</h1>
</head>
<body>
<c:forEach items="${list}" var="student">
<form action="${pageContext.request.contextPath}/Servlet" method="post">
<p>学号:</p><br>
<input type="text" name="id" id="id11" width="200px" value="${student.id}"><span id="id1_err" style="color: red;display: none">输入错误</span>
<p>性别:</p><br>
<input type="radio" name="gender" id="gen" value="男" checked>男<br>
<input type="radio" name="gender" id="gen1" value="女" >女<br>
<p>地址:</p><br>
<input type="text" name="address" id="add" width="200px"value="${student.address}"><span id="add_err" style="color: red;display: none">输入错误</span>
<input value="update" name="method" type="hidden">
<input value="<%=num%>" name="num" type="hidden">
<input type="submit" value="提交">

</c:forEach>
<script>

let id2=document.getElementById("id11");
id2.onblur=function (){
let id3=id2.value.trim();
if(id3.length==0){
document.getElementById("id1_err").style.display='';//中间无空格,有空格也不显示
}
else
document.getElementById("id1_err").style.display='none';
}
let address1=document.getElementById("add");
address1.onblur=function (){
let address2=address1.value.trim();
if(address2.length==0){
document.getElementById("add_err").style.display='';
}
else
document.getElementById("add_err").style.display='none';
}
</script>
</form>
</body>
</html>
4.err_find.jsp
<%--
Created by IntelliJ IDEA.
User: jiachenglin
Date: 2022/10/25
Time: 12:37
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>错误</title>
<h1 style="text-align: center;color:red;">输入错误</h1>
<h2 ><a href="find.jsp">点击返回</a> </h2>
</head>
<body>

</body>
</html>
5.find.jsp
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%--
Created by IntelliJ IDEA.
User: jiachenglin
Date: 2022/10/25
Time: 12:29
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<form action="${pageContext.request.contextPath}/Servlet" method="get">
<p>编号:</p><br>
<input type="text" name="num" id="nu" width="200px"><span id="nu_err" style="color: red;display: none">输入错误</span>

<p>姓名:</p><br>
<input type="text" name="name" id="ne" width="200px"><span id="ne_err" style="color: red;display: none">输入错误</span>
<p>学号:</p><br>
<input type="text" name="id" id="id1" width="200px"><span id="id1_err" style="color: red;display: none">输入错误</span>
<p>性别:</p><br>
<input type="radio" name="gender" id="gen" value="男">男<br>
<input type="radio" name="gender" id="gen1" value="女">女<br>
<p>地址:</p><br>
<input type="text" name="address" id="add" width="200px"><span id="add_err" style="color: red;display: none">输入错误</span>
<input value="find1" name="method" type="hidden">
<input type="submit" value="提交">
<table width="auto" style="margin-top: auto" border="1" cellspacing="0">
<tr>
<th>编号</th>
<th>姓名</th>
<th>学号</th>
<th>性别</th>
<th>地址</th>
</tr>
<c:forEach items="${list}" var="student">
<tr>
<td>${student.num}</td>
<td>${student.name}</td>
<td>${student.id}</td>
<td>${student.gender}</td>
<td>${student.address}</td>
</tr>
</c:forEach>
</table>
</form>
</body>
</html>
6.liulan.jsp
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%--
Created by IntelliJ IDEA.
User: jiachenglin
Date: 2022/10/24
Time: 20:43
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>浏览界面</title>
<h1 style="color: red;text-align: center"></h1>
</head>
<body>
<table width="auto" style="margin-top: auto" border="1" cellspacing="0">
<tr>
<th>编号</th>
<th>姓名</th>
<th>学号</th>
<th>性别</th>
<th>地址</th>
</tr>
<c:forEach items="${list}" var="student">
<tr>
<td>${student.num}</td>
<td>${student.name}</td>
<td>${student.id}</td>
<td>${student.gender}</td>
<td>${student.address}</td>
<td><a href="${pageContext.request.contextPath}/Servlet?method=update&num=${student.num}">修改</a> </td>
<td><a href="${pageContext.request.contextPath}/Servlet?method=delete&num=${student.num}">删除</a> </td>
</tr>
</c:forEach>
</table>

</body>
</html>
7.student
package Student;

import java.util.*;

public class student {
private String name;
private int num;

public student(int num,String name, String id, String gender, String address) {
this.name = name;
this.num = num;
this.id = id;
this.gender = gender;
this.address = address;
}

public student() {

}

public void setNum(int num) {
this.num = num;
}

public int getNum() {
return num;
}

private String id;
private String gender;
private String address;

public String getName() {
return name;
}

public String getId() {
return id;
}

public String getGender() {
return gender;
}

public String getAddress() {
return address;
}

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

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

public void setGender(String gender) {
this.gender = gender;
}

public void setAddress(String address) {
this.address = address;
}

@Override
public String toString() {
return "student{" +
"name='" + name + '\'' +
", id='" + id + '\'' +
", gender='" + gender + '\'' +
", address='" + address + '\'' +
'}';
}
}
8.JDBCTools
package util;

import java.sql.*;

public class JDBCTools {
private static Connection connection = null;
private static String url = "jdbc:mysql://localhost:3306/demo";
private static String usrname = "root";
private static String password = "QQ1003392478pp15";

static {
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}

public static Connection getConnection() {
try {
connection = DriverManager.getConnection(url, usrname, password);
} catch (SQLException e) {
e.printStackTrace();
}
return connection;
}

public static void release(Connection connection, Statement statement, ResultSet resultSet) {
try {
if (connection != null) {
connection.close();
}
if (statement != null) {
statement.close();
}
if (resultSet != null) {
resultSet.close();
}
} catch (SQLException e) {
e.printStackTrace();
}

}
}
9.StudentDao
package Dao;

import jdk.nashorn.internal.scripts.JD;
import util.JDBCTools;
import Student.student;

import java.sql.*;
import java.util.*;

public class StudentDao {

public List<student> findAll() {
List<student> list = new ArrayList<>();
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
try {
String sql = "select *from study ";
connection = JDBCTools.getConnection();
preparedStatement = connection.prepareStatement(sql);
resultSet = preparedStatement.executeQuery();
while (resultSet.next()) {
int num = Integer.parseInt(resultSet.getString(1));
String name = resultSet.getString(2);
String id = resultSet.getString(3);
String gender = resultSet.getString(4);
String address = resultSet.getString(5);
student student = new student(num, name, id, gender, address);
list.add(student);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
JDBCTools.release(connection, preparedStatement, resultSet);
}

return list;
}

public void add(String name, String id, String gender, String address) {
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
try {
connection = JDBCTools.getConnection();
String sql = "insert into study(name,id,gender,address) values(?,?,?,?)";
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1, name);
preparedStatement.setString(2, id);
preparedStatement.setString(3, gender);
preparedStatement.setString(4, address);
preparedStatement.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
JDBCTools.release(connection, preparedStatement, null);
}
}

public void delete(int num) {
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
try {
connection = JDBCTools.getConnection();
String sql = "delete from study where num=?";
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1, String.valueOf(num));
preparedStatement.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
JDBCTools.release(connection, preparedStatement, null);
}
}

public List<student> find(String num, String name, String id, String gender, String address) {
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
List<student> list = new ArrayList<>();
try {
connection = JDBCTools.getConnection();
StringBuffer sb = new StringBuffer();
List<Object> l = new ArrayList<>();
sb.append("select * from study where 1=1");
if (name != null && name != "") {
sb.append(" and name like '%' ? '%' ");
l.add(name);
}
if (gender != null && gender != "") {
sb.append(" and gender like '%' ? '%' ");
l.add(gender);
}
if (id != null && id != "") {
sb.append(" and id like '%' ? '%' ");
l.add(id);
}

if (num != null && num != "") {
sb.append(" and num like '%' ? '%' ");
l.add(num);
}
if (address != null && address != "") {
sb.append(" and address like '%' ? '%' ");
l.add(address);
}
preparedStatement = connection.prepareStatement(sb.toString());
for (int i = 1; i <= l.size(); i++) {
preparedStatement.setObject(i, l.get(i - 1));
}
resultSet = preparedStatement.executeQuery();
while (resultSet.next()) {
int num1 = Integer.parseInt(resultSet.getString(1));
String name1 = resultSet.getString(2);
String id1 = resultSet.getString(3);
String gender1 = resultSet.getString(4);
String address1 = resultSet.getString(5);
student student = new student(num1, name1, id1, gender1, address1);
list.add(student);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
JDBCTools.release(connection, preparedStatement, resultSet);
}
return list;
}

public void update(int num, String id, String gender, String address) {
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
try {
connection = JDBCTools.getConnection();
String sql = "update study set id=?, gender=? ,address=? where num=?";
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1, id);
preparedStatement.setString(2, gender);
preparedStatement.setString(3, address);
preparedStatement.setString(4, String.valueOf(num));
preparedStatement.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
JDBCTools.release(connection, preparedStatement, null);
}
}
}
10.Servlet

package StudentServlet;

import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import Student.student;
import Dao.StudentDao;

@WebServlet("/Servlet")
public class Servlet extends HttpServlet {
private StudentDao student = new StudentDao();

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String method = request.getParameter("method");
switch (method) {
case "add":
Cookie cookie=new Cookie("usrname","jcl");//创建cookie
request.getRequestDispatcher("add.jsp").forward(request, response);
break;
case "delete":
int num = Integer.parseInt(request.getParameter("num"));
List<student>list=student.find(String.valueOf(num),null,null,null,null);
request.setAttribute("list", list);
request.getRequestDispatcher("delete1.jsp").forward(request, response);
break;
case "update":
String num1 = request.getParameter("num");
List<student> list3 = student.find(num1,null,null,null,null);
request.setAttribute("list",list3);
request.getRequestDispatcher("update.jsp").forward(request, response);
break;
case "liulan":
List<student> list1 = student.findAll();
request.setAttribute("list", list1);
request.getRequestDispatcher("liulan.jsp").forward(request, response);
break;
case "find":
request.getRequestDispatcher("find.jsp").forward(request,response);
case "find1":

String num2 = request.getParameter("num");
String name = request.getParameter("name");
String id = request.getParameter("id");
String gender = request.getParameter("gender");
String address = request.getParameter("address");
List<student> list2 = student.find(num2, name, id, gender, address);
request.setAttribute("list", list2);
if (list2.isEmpty()) {
request.getRequestDispatcher("err_find.jsp").forward(request, response);

} else
request.getRequestDispatcher("find.jsp").forward(request, response);
break;
case "delete1":
num= Integer.parseInt(request.getParameter("num"));
student.delete(num);
List<student>list4=student.findAll();
request.setAttribute("list",list4);
request.getRequestDispatcher("liulan.jsp").forward(request,response);
break;

}
}

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
String method = request.getParameter("method");
switch (method) {
case "add":
String name = request.getParameter("name");
String id = request.getParameter("id");
String gender = request.getParameter("gender");
String address = request.getParameter("address");
student.add(name, id, gender, address);
break;
case "update":
int num = Integer.parseInt(request.getParameter("num"));
id = request.getParameter("id");
gender = request.getParameter("gender");
address = request.getParameter("address");
student.update(num,id,gender,address);
break;
}
response.sendRedirect("index.jsp");
}
}

11.delete1.jsp
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%--
Created by IntelliJ IDEA.
User: jiachenglin
Date: 2022/10/30
Time: 09:54
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>删除界面</title>
<h1 style="color: red;text-align: center">删除界面</h1>
</head>
<body>
<form action="${pageContext.request.contextPath}/Servlet" method="get">
<table width="auto" style="margin-top: auto" border="1" cellspacing="0">
<tr>
<th>编号</th>
<th>姓名</th>
<th>学号</th>
<th>性别</th>
<th>地址</th>
<th>操作</th>
</tr>
<c:forEach items="${list}" var="student">
<tr>
<td>${student.num}</td>
<td>${student.name}</td>
<td>${student.id}</td>
<td>${student.gender}</td>
<td>${student.address}</td>
<td><input onclick="return confirm('是否删除?')" type="submit" value="删除"></td>//弹窗验证!!!

</tr>
<input name="method" value="delete1" type="hidden">
<input type="hidden" value="${student.num}" name="num">
</c:forEach>
</form>
</body>
</html>
 

完整的javaweb文档的更多相关文章

  1. jcseg-1.8.7版本发布 - 多配置适应+完整开发帮助文档

    jcseg是使用java开发的一款开源中文分词器, 并且提供了最新版本的lucene和solr分词接口. jcseg 1.8.7版本发布了: 1. 更改了内部设计, 加入JcsegTaskConfig ...

  2. 完整的POM文档内容

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...

  3. 一个完整的WSDL文档及各标签详解

    <?xml version="1.0" encoding="UTF8" ?> <wsdl:definitions targetNamespac ...

  4. configmap使用-完整的configmap文档

    转发 https://www.jianshu.com/p/cf3e2218f283 转发 https://www.kubernetes.org.cn/3138.html 注意:configmap不用也 ...

  5. 一起学微软Power BI系列-官方文档-入门指南(7)发布与共享-终结篇+完整PDF文档

    接触Power BI的时间也只有几个月,虽然花的时间不多,但通过各种渠道了解收集,谈不上精通,但对一些重要概念和细节还是有所了解.在整理官方文档的过程中,也熟悉和了解了很多概念.所以从前到后把微软官方 ...

  6. OpenShare文档管理不完整功能清单

    文档结构管理 以网站集-网站的方式建立各部门的文档管理中心,让文档管理更加规范,使用户更加方便地进行浏览.查找和使用文档资源. 文档全生命周期管理 结合定制开发的工作流平台对文档的全生命周期(创建.获 ...

  7. POI生成word文档完整案例及讲解

    一,网上的API讲解 其实POI的生成Word文档的规则就是先把获取到的数据转成xml格式的数据,然后通过xpath解析表单式的应用取值,判断等等,然后在把取到的值放到word文档中,最后在输出来. ...

  8. C# 合并及拆分Word文档

    本文简要分析一下如何如何使用C#简单实现合并和拆分word文档.平时我们在处理多个word文档时,可能会想要将两个文档合并为一个,或者是将某个文档的一部分添加到另一个文档中,有的时候也会想要将文档拆分 ...

  9. 将C#文档注释生成.chm帮助文档

    由于最近需要把以前的一个项目写一个文档,但一时又不知道写成怎样的,又恰好发现了可以生成chm的工具,于是乎我就研究了下,感觉还不错,所以也给大家分享下.好了,不多废话,下面就来实现一下吧. 生成前的准 ...

  10. 使用Xcode HeaderDoc和Doxygen文档化你的Objective-C和Swift代码

    在一个应用的整个开发过程中涉及到了无数的步骤.其中一些是应用的说明,图片的创作,应用的实现,和实现过后的测试阶段.写代码可能组成了这个过程的绝大部分,因为正是它给了应用生命,但是这样还不够,与它同等重 ...

随机推荐

  1. Newtonsoft.Json高级用法--转载至 焰尾迭 随笔

    本人只做搬运工,以这方便自己学习的态度!以下内容均为拷贝! 如有不适请联系本人! 本文原作者:焰尾迭 本文地址:http://www.cnblogs.com/yanweidie/p/4605212.h ...

  2. [iOS]获取地理位置信息

    1.在工程的 info.plist 文件中增加两个key( 右键 - Add Row ) Privacy - Location Always and When In Use Usage Descrip ...

  3. Django安装与启动

    安装 python -m pip install Django  查看版本 python -m django --version打开命令行,cd 到一个你想放置你代码的目录,然后运行以下命令: dja ...

  4. Robot-Framework 基础操作和常用的语法

    1.打开浏览器 Open Browser 2.浏览器最大化 Maximize Browser Window 3.关闭浏览器 Test Teardown Close Browser 4.输入文本框  I ...

  5. 关于window的文件路径

    跟大家分享一下,初学者的坑.事情是这样的,windows下读取文件路径失败,以下是简单python代码: 点击查看代码 with open("F:\python_file_data\pyth ...

  6. pritunl zero 零信任系统

    pritunl zero 零信任系统 一.概述 1.介绍 Pritunl Zero是一个零信任系统,它可以在不使用VPN的情况下从不受信任的网络安全地访问内部服务. 2.背景 内网搭建了类百度文库系统 ...

  7. SVN: E155004: THERE ARE UNFINISHED WORK ITEMS IN ''; RUN 'SVN CLEANUP' FIRST

    eclipse的SVN更新或者还原都报错 使用clean up也不好用 解决办法 通过网址https://www.sqlite.org/download.html下载这个软件 解压放到.svn文件夹下 ...

  8. Oracle EBS 查看关联关系

    打比方说,如何知道这个视图/表/包/trigger 在其他包/视图等引用. 1. select * from all_dependencies a where a.REFERENCED_NAME = ...

  9. Vue案例——todolist

    最近在学习vue,实现todolist案例,实现效果如下: 该案例分为四个部分:header为输入框,body为列表,item是列表中的条目,footer为最下方的统计. 实现步骤: ①创建项目 vu ...

  10. MySQL 的limit

    题目 limit:从倒数第二行开始,往前查找一行