首页 > 编程语言 >完整的javaweb文档

完整的javaweb文档

时间:2022-10-25 19:46:18浏览次数:55  
标签:javaweb gender request 完整 文档 student address id String

1.index.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="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="add" name="method" type="hidden">
<input type="submit" value="提交">
</form>
</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();

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String method = request.getParameter("method");
switch (method) {
case "add":
request.getRequestDispatcher("add.jsp").forward(request, response);
break;
case "delete":
int num = Integer.parseInt(request.getParameter("num"));
student.delete(num);
List<student> list = student.findAll();
request.setAttribute("list", list);
request.getRequestDispatcher("liulan.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;


}
}

@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");
}
}

标签:javaweb,gender,request,完整,文档,student,address,id,String
From: https://www.cnblogs.com/joranger/p/16826036.html

相关文章

  • 软件项目上线完整流程
    初入互联网行业的小伙伴可能对软件的发布流程很好奇,今天我们就来了解一下软件项目上线的完整流程.根据软件生命周期并结合当前企业的实际情况,企业中的具体流程如下,包......
  • Vitepress搭建组件库文档(上)—— 基本配置
    在vite出现以前,vuepress是搭建组件库文档不错的工具,支持以Markdown方式编写文档。伴随着vite的发展,vitepress已经到了1.0.0-alpha.22版本,很多博客还是基于0.x......
  • 新建文本文档
    数据类型扩展强类型语言要求变量的使用要严格符合规定,所有变量都必须先定义才能使用。整数扩展在定义的数字前加上0b代表二进制数字,加上0代表八进制,加上0x代表十六进......
  • Javaweb知识复习--MyBatis+Mapper代理开发
    一种持久层框架,主要用于简化JDBCMyBatis应用步骤1、在数据库里面创建一个表2、创建模块,导入坐标就是新建一个Maven项目,在pom.xml里面导入mybatis相应导包依赖代码:3、......
  • 从0搭建vue3组件库: 如何完整搭建一个前端脚手架?
    相信大家在前端开发中都使用过很多前端脚手架,如vue-cli,create-vite,create-vue等;本篇文章将会为大家详细介绍这些前端脚手架是如何实现的,并且从零实现一个create-kitty脚手......
  • 针对特殊数据类型JSON.parse()无法完整实现深拷贝的情况下的解决方法
    //判断数据类型functiontypeOf(obj){consttoString=Object.prototype.toStringconstmap={'[objectBoolean]':'boolean','[objectNumber]':'number','[o......
  • GAS 文档阅读摘抄笔记
    GAS文档阅读摘抄笔记AbilitySystemComponent所有期望使用GameplayAbility,包含Attribute,或者接受GameplayEffect的Actor都必须附加ASC.ASC附加的Actor被引用作为......
  • 学习微信小程序开发文档总结
    微信小程序开发完成小程序注册微信公众平台(qq.com)根据步骤进行小程序注册,最终获得一个AppID,下载开发者工具,安装后建立第一个小程序项目,填入AppID,才可通过微信扫码进行......
  • 基于ssm高校科研管理系统-计算机毕业设计源码+LW文档
    【摘要】高校科研管理是一项重要而又繁琐的工作,有效的信息管理平台可以大大缓解科研管理压力,减少工作量。本文以石河子大学信息科学与技术学院为应用背景,开发教师教学信息......
  • 基于ssm工商学院办公用品管理信息系统设计与实现-计算机毕业设计源码+LW文档
    摘 要本高校科研管理系统设计目标是实现高校科研管理的信息化管理,提高管理效率,使得高校科研管理工作规范化、科学化、高效化。本文研究的高校科研管理系统基于SSM架构,采......