![](https://i-blog.csdnimg.cn/blog_migrate/7dad1e65925c11f8504d4bf9bd1b0b11.png)
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>jakarta.servlet</groupId>
<artifactId>jakarta.servlet-api</artifactId>
<version>6.0.0</version>
</dependency>
<dependency>
<groupId>com.csdn</groupId>
<artifactId>pro03-fruit-optimize</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
- pro03-fruit-optimize是我自己封装的一个jar包,功能是可以实现水果系统的增删改查
1、查询水果清单IndexServlet
package com.csdn.fruit.servlet;
import com.csdn.fruit.dao.FruitDao;
import com.csdn.fruit.dao.impl.FruitDaoImpl;
import com.csdn.fruit.pojo.Fruit;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
@WebServlet("/index")
public class IndexServlet extends HttpServlet {
private FruitDao fruitDao = new FruitDaoImpl();
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
List<Fruit> fruitList = fruitDao.getFruitList();
fruitList.stream().forEach(System.out::println);
}
}
- 这里面的FruitDao水果数据访问对象接口,FruitDaoImpl水果数据访问对象实现类,都在pro03-fruit-optimize这个jar包里面
![](https://i-blog.csdnimg.cn/blog_migrate/e1dea8d5b44a19ef649231425cf2e475.png)
2、通过水果名查询水果EditServlet
package com.csdn.fruit.servlet;
import com.csdn.fruit.dao.FruitDao;
import com.csdn.fruit.dao.impl.FruitDaoImpl;
import com.csdn.fruit.pojo.Fruit;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet("/edit")
public class EditServlet extends HttpServlet {
FruitDao fruitDao = new FruitDaoImpl();
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String fname = req.getParameter("fname");
Fruit fruit = fruitDao.getFruitByFname(fname);
System.out.println(fruit);
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/15fcebf74d5bc27b2a70f3bdbc2ac9ef.png)
3、 根据水果名修改库存量UpdateServlet
package com.csdn.fruit.servlet;
import com.csdn.fruit.dao.FruitDao;
import com.csdn.fruit.dao.impl.FruitDaoImpl;
import com.csdn.fruit.pojo.Fruit;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet("/update")
public class UpdateServlet extends HttpServlet {
private FruitDao fruitDao = new FruitDaoImpl();
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//String fidStr = req.getParameter("fid");
//Integer fid = Integer.parseInt(fidStr);
String fname = req.getParameter("fname");
//Integer price = Integer.parseInt(req.getParameter("price"));
Integer fcount = Integer.parseInt(req.getParameter("fcount"));
//String remark = req.getParameter("remark");
fruitDao.updateFruit(new Fruit(0,fname, 0, fcount, null));
System.out.println("修改成功");
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/96ccb0aa13cfd150824d5a16a48d75c9.png)
4、根据水果名删除水果记录DelServlet
package com.csdn.fruit.servlet;
import com.csdn.fruit.dao.FruitDao;
import com.csdn.fruit.dao.impl.FruitDaoImpl;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet("/del")
public class DelServlet extends HttpServlet {
FruitDao fruitDao = new FruitDaoImpl();
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String fname = req.getParameter("fname");
fruitDao.delFruit(fname);
System.out.println("删除成功!");
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/ae71bc910e7e3b119c8ce495712ace1d.png)
5、添加水果AddServlet
package com.csdn.fruit.servlet;
import com.csdn.fruit.dao.FruitDao;
import com.csdn.fruit.dao.impl.FruitDaoImpl;
import com.csdn.fruit.pojo.Fruit;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet("/add")
public class AddServlet extends HttpServlet {
private FruitDao fruitDao = new FruitDaoImpl();
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String fname = req.getParameter("fname");
Integer price = Integer.parseInt(req.getParameter("price"));
Integer fcount = Integer.parseInt(req.getParameter("fcount"));
String remark = req.getParameter("remark");
Fruit fruit = new Fruit(fname, price, fcount, remark);
fruitDao.addFruit(fruit);
System.out.println("添加成功!");
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/49673e2911b22005510db941ce3b4125.png)
- 注意:这里面的 Post 请求,要把 enctype 的值调成 x-www-form-urlencoded
6、查询字符串(query string)
- 在HTTP GET请求中,查询字符串(query string)是 URL中问号 (?)后面的部分 ,用于传递参数给服务器。查询字符串的格式通常由键值对组成,键和值之间使用等号(=)连接,不同的键值对之间使用与号(&)连接。
- 例如,以下是一个包含查询字符串的URL示例:
-
https://example.com/api/users?name=John&age=30
- 在这个例子中,查询字符串是
name=John&age=30
。它包含两个键值对,分别是name=John
和age=30
。 - 在Java的
java.net.URI
类中,可以使用getRawQuery()
方法获取原始的查询字符串。例如: -
URI uri = new URI("https://example.com/api/users?name=John&age=30");
String queryString = uri.getRawQuery(); // 返回 "name=John&age=30"
- 注意,在获取查询字符串后,可以使用Java或其他编程语言的字符串处理方法来解析和提取具体的参数值。