javaweb学习记录
学习javaweb是完完全全自学的,从mysql,tomcat,到Servlet,html都是自学的,主要是看黑马次程序员,但是因为时间原因,我并没有哪个都看,在寒假时我需要再次观看一遍。
首先是mysql吧,mysql主要是SQL语句的学习,也不用被,因为IDEA的可视化工具可以很方便的增删改查,但是在增删改查的方法中,
String sql=""是需要用的,下面放出SQL语句链接:https://blog.csdn.net/weixin_43294936/article/details/123268223,以后在进行SQL语句的应用时就可以在这里寻找。还有,mysql默认字符集是latini,要把它设置成UTF-8,否则无法储存中文。
Tomcat主要是将web项目部署上,其实安装好了之后是没有什么大问题了,只不过在configurations中要部署正确。在部署成功后就不用再管它了,不过要注意的是,Tomcat 10以后的版本,javax.Servlet变成了jakarta.Servlet,不过我的是9.0.68,安装好了,也不用再管了,在Tomcat的logs目录中是运行日志,可以查看,不过还是在idea的services中看Tomcat的日志方便一些。
这里
接下来说一下IDEA吧,IDEA有一个Maven插件,是针对于java的一个插件,我已经配置好了,一个本地仓库一个阿里云远程仓库,这个Maven主要是导包方便,在网上一搜就都出来了,下面是mysql-connector,standard,jstl三个包的配置代码:
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.0.7</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
把这个放到pom.xml中,然后右上位置点一下刷新,即可(因为我的设置成自动刷新了,所以右上部分没有)。等到红字消失即可。
IDEA是非常人性化的,右边有Database按钮,点一下就可以看到。
下面来详细讲一下增删改查的文件结构,在java里创建三个类,分别是JDBCTools(连接数据库、释放资源的类)、student类(可以根据情况改变,与数据库中字段名相对应)、studentRespoitry类(用来实现增删改查的类)。
上面三个类最好放在不同的包里,也可以一个包里。然后接下来在java中创建一个Servlet,这个Servlet在下面的Web和java中都可以看到。对于jsp文件,需要在webapp里创建,注意不要创建到WEB-INF里(否则会出现404问题)。
在Servlet中我们要对doGet和doPost进行复写,下面来说一下这两者的区别。通俗来讲,我们在进行一个网站时,拿增删改查举例,在主界面(index.jsp)我们选择增,那么这时候用"<a href"进行跳转,这时就是调用doGet中的add方法,在doGet中的add方法只需要将页面跳转至add.jsp文件即可。跳转至add.jsp文件后,需要用<form action="/Servlet method ="post" "来将这个页面输入的内容传递给doPost中的add方法,因为在这个界面中无法将method=add传入,需要定义<input type="hidden" name="method" value="add"/>来将隐形method传给doPost,doPost才可以在switch-case中进行区分,否则将报NullPointerException——空指针错误。
接下来讲一个比较近阶的但也不是很难得,比如我想将update1.jsp中的一个变量name传给update2.jsp,但是又得经过Servlet,如下图:
此图为(update1.jsp)运行结果
此时我想将姓名中的数据传给update2.jsp,因为我想修改这个人的信息,我的sql语句必须要根据name进行修改那么该怎么办?
先在update1.jsp中将name传给doGet,<a href="${pageContext.request.contextPath}/Servlet?method=update2&name=${people.name}">修改</a>
再在doGet的update2中接收,并传给update2.jsp;
接着在update2.jsp中接收,在合适位置使用。
接收:<%String name=request.getParameter("name");%>;
使用:<h2 style="color: red">姓名: <%=name%></h2>;
效果如图:(此图为update2.jsp)图
那么这个问题也就解决了,所以doGet一般不执行获取参数,当然也会获取,换句话说,doGet可以执行查和删,但是增和改一定是需要doPost的。
接下来说一下删改查界面,删改查界面最好制表,用<table>来制表,<c:forEach items="${list}" var="people">循环将目前所有存在元素进行输出显示,当然需要在index界面先跳到doGet方法,在doGet里list循环所用元素,再将循环元素传入界面。
界面这么写
对于删除,我们要在doGet里写两个delete,第一个只是从主界面跳转到删除界面,在删除界面点的删除才是真正的删除。并且删除后定位到的界面依旧要是delete.jsp界面,并且为了刷新结果,要用list循环
如下图:
接下来是404与500问题,404问题相对来说比较好解决,一般是跳转时文件名文件路径不对,仔细看一下报错信息即可。有时说不是一个开放文件,那么这时可能是将JSP文件放入WEB-INF中,这个文件是不对外开放的,所以要注意不要再WEB-INF中创建JSP文件,要在webapp中创建,与WEB-INF平级。
500问题可能是代码问题,主要是空指针问题,或者是SQL语句中有问题(问好与变量对应不对,变量与数据库变量类型不对等)。总之一句话,404与500的问题就看报错信息,着重在IDEA里看报错信息,网页报错信息的可以浅看一下。
下面把一个实例放出:
1.首先是index.jsp:首页面
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<title>JSP - Hello World</title>
</head>
<body>
<h1 style="text-align: center;color: black"><%= "人口普查系统" %>
</h1>
<h2 style="color: red"><a href="${pageContext.request.contextPath}/Servlet?method=add">人口信息登记</a></h2>
<h2 style="color: red"><a href="${pageContext.request.contextPath}/Servlet?method=update">人口信息修改</a></h2>
<h2 style="color: red"><a href="${pageContext.request.contextPath}/Servlet?method=delete"> 人口信息删除</a></h2>
<h2 style="color: red"><a href="${pageContext.request.contextPath}/Servlet?method=findAll">人口信息浏览</a></h2>
<h2 style="color: red"><a href="find.jsp">人口信息查询</a></h2>
<br/>
</body>
</html>
如下图:
2.add.jsp:
<%--
Created by IntelliJ IDEA.
User: jiachenglin
Date: 2022/10/16
Time: 14:25
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>人口信息登记</title>
</head>
<body>
<form action="${pageContext.request.contextPath}/Servlet" method="post">
<h1 style="text-align: center;color: black"><%= "人口信息登记界面" %></h1>
<br>户别:</br></p><input type="radio" name="hubie" value="家庭户">家庭户
<input type="radio" name="hubie" value="家庭户">集体户</p>
<p>住房类型:</br>
<input type="radio"name="leixing" value="家庭住宅">家庭住宅</br>
<input type="radio"name="leixing" value="集体住所">集体住所</br>
<input type="radio"name="leixing" value="工作地住所">工作地住所</br>
<input type="radio"name="leixing" value="其他住宅">其他住宅</br>
<input type="radio"name="leixing" value="无住宅">无住宅</br>
</p>
<p>本户现住房面积:</br><input type="text" style="width: 200px"name="mianji" id="mj">平米</p><span style="color: red ;display:none" id="err_mianji">输入有误</span>
<p>本户现住房数:</br><input type="text" style="width: 200px"name="zhufangshu" id="zfs">间</p><span style="color: red ;display:none" id="err_zhufangshu">输入有误</span>
<p>户主名:</br><input type="text" style="width: 200px"name="name" id="ne"></p>
<span id="err_name" style="color: red;display: none" >输入有误</span>
<p>身份证:<input type="text" style="width: 200px"name="id" id="shenfenzheng"> <span style="color: red ;display:none" id="err">身份证输入有误</span>
</p>
<p>性别:</br><input type="radio" name="gender" value="男">男
<input type="radio" name="gender" value="女">女</p>
<p>民族:</br><input type="text" style="width: 200px"name="minzu"></p>
<p>受教育程度:</br> <select name="jiaoyu" id="jiaoyu">
<option value="研究生" >研究生</option>
<option value="大学本科" >大学本科</option>
<option value="高中">高中</option>
<option value="初中">初中</option>
<option value="小学">小学</option>
<option value="未上过学">未上过学</option>
</select>
</p>
<br>
<script>
let id= document.getElementById("shenfenzheng");
id.onblur=function (){
let id1=id.value.trim();
let flag=0;
if( id1.charAt(17)!=='0'||id1.charAt(17)!=='1'||id1.charAt(17)!=='2'||id1.charAt(17)!=='3'||id1.charAt(17)!=='4'||id1.charAt(17)!=='5'||id1.charAt(17)!=='6'||id1.charAt(17)!=='7'||id1.charAt(17)!=='8'||id1.charAt(17)!=='9'||id1.charAt(17)!=='X')
flag=1;
if( id1.charAt(17)==='0'||id1.charAt(17)==='1'||id1.charAt(17)==='2'||id1.charAt(17)==='3'||id1.charAt(17)==='4'||id1.charAt(17)==='5'||id1.charAt(17)==='6'||id1.charAt(17)==='7'||id1.charAt(17)==='8'||id1.charAt(17)==='9'||id1.charAt(17)==='X')
flag=0;
if(id1.length<18||flag===1)
{
document.getElementById("err").style.display='';
}
else
{
document.getElementById("err").style.display='none';
}
}
let mj1= document.getElementById("mj");
mj1.onblur=function (){
let mj2=mj1.value.trim();
if(mj2<0||mj2.length==0)
{
document.getElementById("err_mianji").style.display='';
}
else
{
document.getElementById("err_mianji").style.display='none';
}
}
let zfs1= document.getElementById("zfs");
zfs1.onblur=function (){
let zfs2=zfs1.value.trim();
if(zfs2<0||zfs2.length==0)
{
document.getElementById("err_zhufangshu").style.display='';
}
else
{
document.getElementById("err_zhufangshu").style.display='none';
}
}
let name1=document.getElementById("ne")
name1.onblur=function (){
let name2=name1.value.trim();
if(name2.length==0){
document.getElementById("err_name").style.display='';
}
else {
document.getElementById("err_name").style.display='none';
}
}
</script>
<input type="submit" value="提交"/>
<input type="hidden" name="method" value="add"/>//主要依靠这个,在Servlet中识别method。
</form>
</body>
</html>
3.update1.jsp
<%--
Created by IntelliJ IDEA.
User: jiachenglin
Date: 2022/10/17
Time: 10:22
To change this template use File | Settings | File Templates.
--%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<h1 style="text-align: center">人口信息修改界面</h1>
<body>
<table>
<tr>
<th>户别</th>
<th>类型</th>
<th>面积</th>
<th>住房数</th>
<th>姓名</th>
<th>身份证号</th>
<th>性别</th>
<th>受教育程度</th>
</tr>
<c:forEach items="${list}" var="people">
<tr>
<td>${people.hubie}</td>
<td>${people.leixing}</td>
<td>${people.mianji}</td>
<td>${people.zhufangshu}</td>
<td>${people.name}</td>
<td>${peoplet.id}</td>(看看哪个傻逼写的吧,妈的people拼错了都看不见)
<td>${people.gender}</td>
<td>${people.jiaoyu}</td>
<td>
<a href="${pageContext.request.contextPath}/Servlet?method=update2&name=${people.name}">修改</a>
</td>
</tr>
</c:forEach>
</table>
</body>
</html>
4.update2.jsp
<%--
Created by IntelliJ IDEA.
User: jiachenglin
Date: 2022/10/16
Time: 16:31
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
<%String name=request.getParameter("name");%>//从update1.jsp中获取
</head>
<form action="${pageContext.request.contextPath}/Servlet" method="post">
<body>
<h1 style="text-align: center" t>人口信息修改界面</h1>
<h2 style="color: red">姓名: <%=name%></h2>
<p>户别:</br><input type="radio" name="hubie" value="家庭户">家庭户
<input type="radio" name="hubie" value="家庭户">集体户</p>
<p>住房类型:</br>
<input type="radio"name="leixing" value="家庭住宅">家庭住宅</br>
<input type="radio"name="leixing" value="集体住所">集体住所</br>
<input type="radio"name="leixing" value="工作地住所">工作地住所</br>
<input type="radio"name="leixing" value="其他住宅">其他住宅</br>
<input type="radio"name="leixing" value="无住宅">无住宅</br>
</p>
<p>本户现住房面积:</br><input type="text" style="width: 200px"name="mianji" id="mj">平米</p><span style="color: red ;display:none" id="err_mianji">输入有误</span>
<p>本户现住房数:</br><input type="text" style="width: 200px"name="zhufangshu" id="zfs">间</p><span style="color: red ;display:none" id="err_zhufangshu">输入有误</span>
<p>性别:</br><input type="radio" name="gender" value="男">男
<input type="radio" name="gender" value="女">女</p>
<p>民族:</br><input type="text" style="width: 200px"name="minzu"></p>
<p>受教育程度:</br> <select name="jiaoyu">
<option value="研究生" >研究生</option>
<option value="大学本科" >大学本科</option>
<option value="高中">高中</option>
<option value="初中">初中</option>
<option value="小学">小学</option>
<option value="未上过学">未上过学</option>
</select>
</p>
<input type="hidden" name="method" value="update"/>
<input type="hidden" name="name" value="<%=name%>"/>
<input type="submit" value="提交"/>
</form>
<script>
let mj1= document.getElementById("mj");
mj1.onblur=function (){
let mj2=mj1.value.trim();
if(mj2<0||mj2.length==0)
{
document.getElementById("err_mianji").style.display='';
}
else
{
document.getElementById("err_mianji").style.display='none';
}
}
let zfs1= document.getElementById("zfs");
zfs1.onblur=function (){
let zfs2=zfs1.value.trim();
if(zfs2<0||zfs2.length==0)
{
document.getElementById("err_zhufangshu").style.display='';
}
else
{
document.getElementById("err_zhufangshu").style.display='none';
}
}
</script>
</body>
</html>
5.delete.jsp
<%--
Created by IntelliJ IDEA.
User: jiachenglin
Date: 2022/10/16
Time: 18:41
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>人口删除系统</title>
</head>
<h1 style="text-align: center;color: black"><%= "人口删除界面" %></h1>
<body>
<table>
<tr>
<th>户别</th>
<th>类型</th>
<th>面积</th>
<th>住房数</th>
<th>姓名</th>
<th>身份证号</th>
<th>性别</th>
<th>受教育程度</th>
</tr>
<c:forEach items="${list}" var="people">
<tr>
<td>${people.hubie}</td>
<td>${people.leixing}</td>
<td>${people.mianji}</td>
<td>${people.zhufangshu}</td>
<td>${people.name}</td>
<td>${peoplet.id}</td>(傻逼还没看见哪错了)
<td>${people.gender}</td>
<td>${people.jiaoyu}</td>
<td>
<a href="${pageContext.request.contextPath}/Servlet?method=delete2&name=${people.name}">删除</a>
</td>
</tr>
</c:forEach>
</table>
<a href="index.jsp">返回主菜单</a>
</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/16
Time: 14:41
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<h1 style="text-align: center">人口信息浏览界面</h1>
<body>
<form action="${pageContext.request.contextPath}/Servlet" method="post">
<table width="60%" style="margin-top: auto">
<tr>
<th>户别</th>
<th>类型</th>
<th>面积</th>
<th>住房数</th>
<th>姓名</th>
<th>身份证号</th>
<th>性别</th>
<th>受教育程度</th>
</tr>
<c:forEach items="${list}" var="people">
<tr>
<td>${people.hubie}</td>
<td>${people.leixing}</td>
<td>${people.mianji}</td>
<td>${people.zhufangshu}</td>
<td>${people.name}</td>
<td>${peoplet.id}</td>
<td>${people.gender}</td>
<td>${people.jiaoyu}</td>
<td>
</td>
</tr>
</c:forEach>
</table>
<a href="index.jsp">返回主菜单</a>
</form>
</body>
</html>
其实在这里就可以看出,浏览、删除、修改界面其实可以合并成一个界面,就是在浏览界面里在每一行后面添加两个按钮:删除、修改。
7.JDBCTools.java
package util;
import java.sql.*;
public class JDBCTools {
private static Connection connection;
private static String url="jdbc:mysql://localhost:3306/RUNOOB";
private static String user="root";//用户名
private static String pass="QQ1003392478pp15";//密码
static {
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public static Connection getConnection(){
try {
connection= DriverManager.getConnection(url,user,pass);
} 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();
}
}
}
8.people.java
package entity;
public class people {
private String hubie;
private String leixing;
private int mianji;
private int zhufangshu;
private String name;
private String id;
private String gender;
private String jiaoyu;
public people(String hubie, String leixing, int mianji, int zhufangshu, String name, String id, String gender, String jiaoyu) {
this.hubie = hubie;
this.leixing = leixing;
this.mianji = mianji;
this.zhufangshu = zhufangshu;
this.name = name;
this.id = id;
this.gender = gender;
this.jiaoyu = jiaoyu;
}
public String getLeixing() {
return leixing;
}
public int getMianji() {
return mianji;
}
public int getZhufangshu() {
return zhufangshu;
}
public String getName() {
return name;
}
public String getId() {
return id;
}
public String getGender() {
return gender;
}
public String getJiaoyu() {
return jiaoyu;
}
public String getHubie() {
return hubie;
}
public void setHubie(String hubie) {
this.hubie = hubie;
}
public void setLeixing(String leixing) {
this.leixing = leixing;
}
public void setMianji(int mianji) {
this.mianji = mianji;
}
public void setZhufangshu(int zhufangshu) {
this.zhufangshu = zhufangshu;
}
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 setJiaoyu(String jiaoyu) {
this.jiaoyu = jiaoyu;
}
@Override
public String toString() {
return "people{" +
"hubie='" + hubie + '\'' +
", leixing='" + leixing + '\'' +
", mianji=" + mianji +
", zhufangshu=" + zhufangshu +
", name='" + name + '\'' +
", id=" + id +
", gender='" + gender + '\'' +
", jiaoyu='" + jiaoyu + '\'' +
'}';
}
}
9.peout.java
package Peopleutils;
import entity.people;
import util.JDBCTools;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
public class Peout {
public List<people> findAll(){
List<people> list=new ArrayList<>();
Connection connection=null;
PreparedStatement preparedStatement=null;
ResultSet resultSet=null;
try {
//调用JDBCTools连接mysql数据库
connection= JDBCTools.getConnection();
String sql="select * from people";//查询语句
preparedStatement=connection.prepareStatement(sql);
resultSet = preparedStatement.executeQuery();
while (resultSet.next()){
//从resultSet拿出每个属性数据
String hubie=resultSet.getString(1);
String leixing=resultSet.getString(2);
int mianji=resultSet.getInt(3);
int zhufangshu=resultSet.getInt(4);
String name=resultSet.getString(5);
String id=resultSet.getString(6);
String gender=resultSet.getString(7);
String jiaoyu=resultSet.getString(8);
//这里可以理解为,resultSet拿出每个属性数据赋予student对象,形成一个有数据的student对象
people people = new people(hubie,leixing,mianji,zhufangshu,name,id,gender,jiaoyu);
list.add(people);//可能多条数据,放到集合中
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
//调用JDBCTools,关闭connection,preparedStatement,resultSet
JDBCTools.release(connection,preparedStatement,resultSet);
}
return list;
}
//添加操作
public void add(String hubie, String leixing, int mianji, int zhufangshu, String name, String id, String gender, String jiaoyu){
Connection connection=null;
PreparedStatement preparedStatement=null;
try {
connection= JDBCTools.getConnection();
String sql="insert into people(hubie,leixing,mianji,zhufangshu,name,id,gender,jiaoyu) values (?,?,?,?,?,?,?,?)";
preparedStatement=connection.prepareStatement(sql);
//这里注意第一个参数对应sql语句问号的序号,
preparedStatement.setString(1,hubie);//就是把id替代sql的第一个问号,id由前端传过来
preparedStatement.setString(2,leixing);
preparedStatement.setInt(3,mianji);
preparedStatement.setInt(4,zhufangshu);
preparedStatement.setString(5,name);
preparedStatement.setString(6,id);
preparedStatement.setString(7,gender);
preparedStatement.setString(8,jiaoyu);
preparedStatement.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
JDBCTools.release(connection,preparedStatement,null);
}
}
//删除操作
public void deleteById(String name){
Connection connection=null;
PreparedStatement preparedStatement=null;
try {
connection= JDBCTools.getConnection();
String sql="delete from people where name=?";
preparedStatement=connection.prepareStatement(sql);
preparedStatement.setString(1,name);
preparedStatement.executeUpdate();
} catch (SQLException e) {//Exception:可控的错误
e.printStackTrace();
}finally {
JDBCTools.release(connection,preparedStatement,null);
}
}
//模糊查询
public List<people> findById(String name1,String gender2){
Connection connection=null;
PreparedStatement preparedStatement=null;
ResultSet resultSet=null;
people people=null;
List<people> list=new ArrayList<>();
try {
connection= JDBCTools.getConnection();
StringBuffer sb=new StringBuffer();
List<Object>l=new ArrayList<>();
/*
只能这样写,必须全部用append来连接,否则会出现"?"变乱码的情况
String sql = "select * from student where 1=1";
*/
sb.append("select * from people where 1=1");
if(name1!=null&&name1!=""){
//注意and前一定要带空格,每个单词之前也要有空格,否则SQL语句语法错误
sb.append(" and name like '%' ? '%' ");
l.add(name1);
}
if(gender2!=null&&gender2!=""){
sb.append(" and gender like '%' ? '%' ");
l.add(gender2);
}
/*整数怎么办
int age1=0;
if(age!=null&&age!=""){
age1=Integer.parseInt(age);
sb.append("and age like '%' ? '%'");
l.add(age1);
}*/
preparedStatement = connection.prepareStatement(sb.toString());
System.out.println(preparedStatement);
for(int i=1;i<=l.size();i++){
preparedStatement.setObject(i,l.get(i-1));
}
resultSet = preparedStatement.executeQuery();
while (resultSet.next()){
String hubie=resultSet.getString(1);
String leixing=resultSet.getString(2);
int mianji=resultSet.getInt(3);
int zhufangshu=resultSet.getInt(4);
String name=resultSet.getString(5);
String id=resultSet.getString(6);
String gender=resultSet.getString(7);
String jiaoyu=resultSet.getString(8);
people = new people(hubie,leixing,mianji,zhufangshu,name,id,gender,jiaoyu);
list.add(people);
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
JDBCTools.release(connection,preparedStatement,resultSet);
}
return list;
}
//更新操作
public void update(String hubie, String leixing, int mianji, int zhufangshu, String name, String id, String gender, String jiaoyu){
Connection connection=null;
PreparedStatement preparedStatement=null;
try {
connection= JDBCTools.getConnection();
String sql="update people set hubie=?,leixing=?,mianji=?,zhufangshu=?,id=?,gender=?,jiaoyu=? where name=?";
preparedStatement=connection.prepareStatement(sql);
preparedStatement.setString(1,hubie);
preparedStatement.setString(2,leixing);
preparedStatement.setInt(3,mianji);
preparedStatement.setInt(4,zhufangshu);
preparedStatement.setString(5,id);
preparedStatement.setString(6,gender);
preparedStatement.setString(7,jiaoyu);
preparedStatement.setString(8,name);
preparedStatement.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
JDBCTools.release(connection,preparedStatement,null);
}
}
}
10.Servlet.java
package PeopleServlet;
import Peopleutils.Peout;
import com.sun.xml.internal.ws.api.ha.StickyFeature;
import entity.people;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.List;
@WebServlet("/Servlet")
public class Servlet extends HttpServlet {
private Peout peout=new Peout();
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String method=request.getParameter("method");
if (method==null){
method="findAll";
}
switch (method) {
case "findAll"://查询所有数据
List<people> list = peout.findAll();//调用StudentRepository中的findAll()方法
request.setAttribute("list",list);//存入request中
request.getRequestDispatcher("liulan.jsp").forward(request,response);//转发到index.jsp中
case "update":
String name4=request.getParameter("name");
List<people> list3 = peout.findAll();//调用StudentRepository中的findAll()方法
request.setAttribute("list",list3);//存入request中
request.getRequestDispatcher("update1.jsp").forward(request,response);
break;
case "add":
peout.findAll();
request.getRequestDispatcher("add1.jsp").forward(request,response);
break;
case "delete":
List<people> list1 = peout.findAll();//调用StudentRepository中的findAll()方法
request.setAttribute("list",list1);
request.getRequestDispatcher("delete.jsp").forward(request,response);
case "delete2":
String name3=request.getParameter("name");
peout.deleteById(name3);//根据id删
List<people> list2 = peout.findAll();
request.setAttribute("list",list2);
request.getRequestDispatcher("delete.jsp").forward(request,response);
response.sendRedirect("delete.jsp");
break;
case "update2":
name4=request.getParameter("name");
request.getRequestDispatcher("update2.jsp").forward(request,response);
break;
case "findById":
String name=request.getParameter("name");
String gender=request.getParameter("gender");
List<people> list4 = peout.findById(name,gender);//调用StudentRepository中的findById()方法
request.setAttribute("list",list4);
if(list4.isEmpty()){//如果list为空,则表示输入有误
request.getRequestDispatcher("error1.jsp").forward(request,response);
}
request.getRequestDispatcher("find2.jsp").forward(request,response);
}
}
@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 hubie=request.getParameter("hubie");
String leixing=request.getParameter("leixing");
int mianji= Integer.parseInt(request.getParameter("mianji"));
int zhufangshu= Integer.parseInt(request.getParameter("zhufangshu"));
String name=request.getParameter("name");
String id= request.getParameter("id");
String gender=request.getParameter("gender");
String jiaoyu=request.getParameter("jiaoyu");
peout.add(hubie,leixing,mianji,zhufangshu,name,id,gender,jiaoyu);//调用add方法
break;
case "update"://更新操作
hubie=request.getParameter("hubie");
leixing=request.getParameter("leixing");
mianji= Integer.parseInt(request.getParameter("mianji"));
zhufangshu= Integer.parseInt(request.getParameter("zhufangshu"));
name=request.getParameter("name");
gender=request.getParameter("gender");
jiaoyu=request.getParameter("jiaoyu");
peout.update(hubie,leixing,mianji,zhufangshu,name,gender,jiaoyu);//调用add方法
break;
}
response.sendRedirect("index.jsp");//返回首页
}
}
11.find.jsp
<%--
Created by IntelliJ IDEA.
User: jiachenglin
Date: 2022/10/21
Time: 13:57
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<body>
<head>
<form action="${pageContext.request.contextPath}/Servlet" method="get">
<h1 style="text-align: center"> 人口查询界面</h1>
<p>姓名:<input type="text" name="name" width="200px"></p><br>
<p>性别:<input type="text" name="gender" width="200px"></p><br>
<input type="submit" value="提交">
<input type="hidden" name="method" value="findById"/>
</form>
</body>
</html>
12.find2.jsp
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%--
Created by IntelliJ IDEA.
User: jiachenglin
Date: 2022/10/21
Time: 17:16
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<h1> 结果</h1>
</head>
<body>
<table>
<tr>
<th>户别</th>
<th>类型</th>
<th>面积</th>
<th>住房数</th>
<th>姓名</th>
<th>身份证号</th>
<th>性别</th>
<th>受教育程度</th>
</tr>
<c:forEach items="${list}" var="people">
<tr>
<td>${people.hubie}</td>
<td>${people.leixing}</td>
<td>${people.mianji}</td>
<td>${people.zhufangshu}</td>
<td>${people.name}</td>
<td>${peoplet.id}</td>
<td>${people.gender}</td>
<td>${people.jiaoyu}</td>
<td>
</td>
</tr>
</c:forEach>
</table>
<a href="index.jsp">返回主菜单</a>
<a href="find.jsp">返回查找界面</a>
</body>
</html>
13.error1.jsp
<%--
Created by IntelliJ IDEA.
User: jiachenglin
Date: 2022/10/21
Time: 17:49
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
<h1 style="text-align: center; color: red">输入有误!!</h1>
<a href="find.jsp">点击返回查找界面</a>
</head>
<body>
</body>
</html>
——————————————————————————————————————————————————————————
这次学习了JavaScript的表单验证,具体如下图显示,主要在add和update中增加代码。
主要新增代码如下:
<script>
let id= document.getElementById("shenfenzheng");//根据id与身份证建立连接
id.onblur=function (){
let id1=id.value.trim();取id的值并去掉前后空格
let flag=0;
if( id1.charAt(17)!=='0'||id1.charAt(17)!=='1'||id1.charAt(17)!=='2'||id1.charAt(17)!=='3'||id1.charAt(17)!=='4'||id1.charAt(17)!=='5'||id1.charAt(17)!=='6'||id1.charAt(17)!=='7'||id1.charAt(17)!=='8'||id1.charAt(17)!=='9'||id1.charAt(17)!=='X')
flag=1;//验证身份证最后一位是否合法
if( id1.charAt(17)==='0'||id1.charAt(17)==='1'||id1.charAt(17)==='2'||id1.charAt(17)==='3'||id1.charAt(17)==='4'||id1.charAt(17)==='5'||id1.charAt(17)==='6'||id1.charAt(17)==='7'||id1.charAt(17)==='8'||id1.charAt(17)==='9'||id1.charAt(17)==='X')
flag=0;
if(id1.length<18||flag===1)
{
document.getElementById("err").style.display='';
}
else
{
document.getElementById("err").style.display='none';
}
}
let mj1= document.getElementById("mj");
mj1.onblur=function (){
let mj2=mj1.value.trim();
if(mj2<0||mj2.length==0)
{
document.getElementById("err_mianji").style.display='';
}
else
{
document.getElementById("err_mianji").style.display='none';
}
}
let zfs1= document.getElementById("zfs");
zfs1.onblur=function (){
let zfs2=zfs1.value.trim();
if(zfs2<0||zfs2.length==0)
{
document.getElementById("err_zhufangshu").style.display='';
}
else
{
document.getElementById("err_zhufangshu").style.display='none';
}
}
let name1=document.getElementById("ne")
name1.onblur=function (){
let name2=name1.value.trim();
if(name2.length==0){
document.getElementById("err_name").style.display='';
}
else {
document.getElementById("err_name").style.display='none';
}
}
</script>
在输入框中要加入如下:
<p>本户现住房面积:</br><input type="text" style="width: 200px"name="mianji" id="mj">平米</p><span style="color: red ;display:none" id="err_mianji">输入有误</span>
//注意id与Script中对应
这次遇到了这么个问题,在用list循环时报错:Property [clas] not found on type [Student.student],这个问题主要是说这个变量名没有在student类中,我改变量名没有用,问题在于toString方法中:绿色字“cla”与clas不匹配,注意这点
回显数据:回显数据主要用在修改数据时显示之前已存在的用户资料,其实内核很简单,主要书通过判断要修改的编号,通过find函数查到这个人的数据并存入List中,再将数据传到JSP文件中,利用value来显示即可。
Servlet的代码如下:
case "update"://doGet方法中
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;
update.jsp代码如下:
<body>
<c:forEach items="${list}" var="student">
<form action="${pageContext.request.contextPath}/Servlet" method="post">
<p>学号:</p><br>
<input type="text" name="id" id="id1" 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="提交">
</form>
</c:forEach>
</body>