1.技术介绍
java+servlet+mysql+JSP
开发工具:eclipse或IDEA
2.主要功能说明:
1)学生
注册、登录、个人信息修改、查看公告、查看失物信息、我的失物信息、我要挂失、查看拾物信息、我的拾物信息、发布拾物信息、我的认领、留言列表、我的留言、我要留言
2)管理员
用户管理、公告管理、拾物管理、失物管理、认领管理、留言管理
3.部分代码展示:
package controller;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.List;
import java.util.UUID;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import dao.GoodsDao;
import dao.UserDao;
import model.Goods;
import model.PageBean;
import model.Shiwu;
import model.User;
import utils.PageUtils;
/**
* Servlet implementation class GoodsServlet
*/
public class GoodsServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private GoodsDao goodsDao=new GoodsDao();
private UserDao userDao=new UserDao();
public GoodsServlet() {
super();
// TODO Auto-generated constructor stub
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String method=request.getParameter("method");
if(method!=null&&method.equals("addGoodsAct1")) {
Goods goods = fileUpload(request);
User user=(User) request.getSession().getAttribute("user");
goods.setU_id(user.getId());
boolean b=goodsDao.insertGoods(goods);//插入
if(b){
System.out.print(user.getId());
List<Goods> list=goodsDao.findMyInfo(goods.getU_id());
for(Goods g:list) {
User u =userDao.findById(g.getU_id());
g.setUser(u);
}
request.setAttribute("list", list);
request.getRequestDispatcher("/WEB-INF/users/goods_list.jsp").forward(request, response);
}
return;
}
if(method!=null&&method.equals("addGoodsAct2")) {
Goods goods = fileUpload(request);
boolean b=goodsDao.insertGoods(goods);//插入
if(b){
GoodsList2(request, response);
}
return;
}
if(method!=null&&method.equals("goodsListAct")) {
GoodsList(request,response);
return;
}
if(method!=null&&method.equals("goodsListAct1")) {
GoodsList1(request,response);
return;
}
//查询所有的失物信息
if(method!=null&&method.equals("goodsListAct2")) {
GoodsList2(request,response);
return;
}
if(method!=null&&method.equals("toAddGoodsAct1")) {
request.getRequestDispatcher("/WEB-INF/users/add_goods.jsp").forward(request, response);
return;
}
//跳转到添加页面
if(method!=null&&method.equals("toAddGoodsAct")) {
request.getRequestDispatcher("/WEB-INF/views/add_goods.jsp").forward(request, response);
return;
}
//跳转到更改界面
if(method!=null&&method.equals("queryGoodsAct")) {
int id=Integer.parseInt(request.getParameter("id"));
System.out.print(id+"****");
Goods record=goodsDao.findById(id);
request.setAttribute("list", record);
request.getRequestDispatcher("/WEB-INF/views/update_goods.jsp").forward(request, response);
return;
}
//删除物品
if(method!=null&&method.equals("deleteGoodsAct")) {
int id=Integer.parseInt(request.getParameter("id"));
String type=request.getParameter("type");
goodsDao.delete(id);
if(type.equals("1")) {
GoodsList1(request, response);
}else{
GoodsList2(request, response);
}
return;
}
if(method!=null&&method.equals("findByGnameAct")) {
String gname=request.getParameter("gname");
String lostPlace=request.getParameter("lostPlace");
System.out.print(lostPlace+"****");
List<Goods> list=goodsDao.findByGname(gname,lostPlace);
if(list.size()>0) {
request.setAttribute("list", list);
request.removeAttribute("msg");
request.getRequestDispatcher("/WEB-INF/views/list_goods.jsp").forward(request, response);
}else if(list.size()<=0) {
request.setAttribute("msg", "没有此物品信息");
request.getRequestDispatcher("/WEB-INF/views/list_goods.jsp").forward(request, response);
}
return;
}
if(method!=null&&method.equals("updateGoodsAct")) {
int id=Integer.parseInt(request.getParameter("id"));
String gname=request.getParameter("gname");
String lostPlace=request.getParameter("lostPlace");
String info=request.getParameter("info2");
System.out.print(info+"*****");
String createTime=request.getParameter("createTime");
String status=request.getParameter("status");
String phone=request.getParameter("phone");
Goods record=new Goods();
record.setGname(gname);
record.setLostPlace(lostPlace);
record.setInfo(info);
record.setPhone(phone);
record.setCreateTime(createTime);
record.setStatus(status);
record.setPhone(phone);
boolean b=goodsDao.updateGoods(record, id);//插入
if(b){
request.setAttribute("msg", "ok");
GoodsList2(request, response);
}else {
GoodsList2(request, response);
}
return;
}
}
private void GoodsList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String currentPage = request.getParameter("currentPage");
if (currentPage==null||currentPage.equals("")){
//如果第一次访问,没有传递currentPage参数,则当前页为1
currentPage="1";
}
//接收用户输入的每页显示数据的记录数
String pageSize = request.getParameter("pageSize");
//如果没有传递过来,设置一个默认值为2
//细节:当点击下一页或者别的,会导致pageSize丢失了,应该要带回去
if (pageSize==null||pageSize.equals("")){
//设置pageSize的默认值是2
pageSize="3";
}
//1)在service层调用获取PageBean对象的方法
PageUtils service = new PageUtils();
//只需要传入一个currentPage(当前页数)即可
PageBean pageBean = service.queryPageBean(currentPage,Integer.parseInt(pageSize));
//2)把PageBean对象放到域中
request.setAttribute("pageBean", pageBean);
request.getRequestDispatcher("/WEB-INF/users/list_goods.jsp").forward(request, response);
}
//用户查看
private void GoodsList1(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
User user =(User) request.getSession().getAttribute("user");
if(user!=null) {
List<Goods> list=goodsDao.findMyInfo(user.getId());
for(Goods g:list) {
User u =userDao.findById(g.getU_id());
g.setUser(u);
}
request.setAttribute("list", list);
request.getRequestDispatcher("/WEB-INF/users/goods_list.jsp").forward(request, response);
}else {
PrintWriter out = response.getWriter();
out.write("<script>");
out.write("alert('请先登录');");
out.write("location.href='GoodsServlet?method=loginOutAct'");
out.write("</script>");
out.close();
}
}
//管理员所有信息
private void GoodsList2(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String currentPage = request.getParameter("currentPage");
if (currentPage==null||currentPage.equals("")){
//如果第一次访问,没有传递currentPage参数,则当前页为1
currentPage="1";
}
//接收用户输入的每页显示数据的记录数
String pageSize = request.getParameter("pageSize");
//如果没有传递过来,设置一个默认值为2
//细节:当点击下一页或者别的,会导致pageSize丢失了,应该要带回去
if (pageSize==null||pageSize.equals("")){
//设置pageSize的默认值是2
pageSize="3";
}
//1)在service层调用获取PageBean对象的方法
PageUtils service = new PageUtils();
//只需要传入一个currentPage(当前页数)即可
PageBean pageBean = service.queryPageBean(currentPage,Integer.parseInt(pageSize));
//2)把PageBean对象放到域中
request.setAttribute("pageBean", pageBean);
//3)转发
request.getRequestDispatcher("/WEB-INF/views/goods_list.jsp").forward(request, response);
}
public String makeFileName(String filename){
String ext = filename.substring(filename.lastIndexOf(".") + 1);
return UUID.randomUUID().toString() + "." + ext;
}
private Goods fileUpload(HttpServletRequest request) {
Goods goods=new Goods();
try {
DiskFileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
List<FileItem> list = upload.parseRequest(request);
for(FileItem item : list){
if(item.isFormField()){
String name = item.getFieldName();
String value = item.getString("UTF-8");
BeanUtils.setProperty(goods, name, value);
}else{
String filename = item.getName();
String savefilename = makeFileName(filename);
//String savepath="D:\\ws\\LostGoods\\WebContent\\upload\\";
String savepath= this.getServletContext().getRealPath("/upload");//Tomcat虚拟路径
InputStream in = item.getInputStream();
FileOutputStream out = new FileOutputStream(savepath + "\\" + savefilename);
int len = 0;
byte buffer[] = new byte[1024];
while((len = in.read(buffer)) > 0){
out.write(buffer, 0, len);
}
in.close();
out.close();
item.delete();
goods.setImgUrl(savefilename);
System.out.println(goods.getImgUrl());
}
}
return goods;
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
}
4.系统演示地址:
链接:https://pan.baidu.com/s/1r9wn0BGL7YXuGDYyq4v7fQ
提取码:f6e2