知识屋:更实用的电脑技术知识网站
所在位置:首页 > 科技

JavaWeb新手小项目以及源码

发表时间:2022-03-23来源:网络

此项目主要实现的功能有:
jsp连接数据库、MD5加密、验证码验证、Ajax、文件的上传与下载、session登录验证等。

先来几张截图:


一、首先写登录页面 login.jsp

WormJam-登录 WormJam

秀出你的源代码

账号:

密码:

验证码:

注册 //验证账号密码格式 function accountChange(){ var str = loginForm.account.value; var reg = /[a-zA-Z0-9]{6,16}/; if(reg.test(str)){ accountText.innerHTML = "√"; accountText.style.color = "green"; } else{ accountText.innerHTML = "账号为6至16位数字或字母!"; accountText.style.color = "red"; } } function passwordChange(){ var str = loginForm.password.value; var reg = /^[x00-x7f]+$/; if(reg.test(str) && str.length>5 && str.length

在首页,可以上传文件,然后再热门分享中把所有文件以及上传文件的作者列出来。我这里没有遍历文件夹来列出文件,而是在上传文件的同时记录到数据库。因此还应建立上传文件的DAO类。

十七、VO类UploadFile.java:

package VO; public class UploadFile { //用户上传文件的类 private String account; private String fileName; private String description; public String getAccount(){ return account; } public void setAccount(String account){ this.account = account; } public String getFileName(){ return fileName; } public void setFileName(String fileName){ this.fileName = fileName; } public String getDescription(){ return description; } public void setDescription(String description){ this.description = description; } }

定义了文件名,作者及描述。

十八、DAO标准类IUploadFileDAO.java:

package DAO; import java.util.* ; import VO.UploadFile; public interface IUploadFileDAO { public boolean doCreate(UploadFile file) throws Exception; //写入 public List findByAccount(String account) throws Exception; //按账号查找文件 public List findAll() throws Exception; //列出所有文件 }

十九、真实主题实现类UploadFileDAOImpl.java:

package DAO ; import java.util.* ; import java.sql.* ; import DAO.IUploadFileDAO; import VO.UploadFile; public class UploadFileDAOImpl implements IUploadFileDAO { private Connection conn = null; //数据库连接对象 private PreparedStatement pstmt = null; //数据库操作对象 public UploadFileDAOImpl(Connection conn){ //通过构造方法取得数据库连接 this.conn = conn; } public boolean doCreate(UploadFile file) throws Exception{ //添加账户 boolean flag = false; //定义标志位 String sql = "INSERT INTO FILES(acname,file,descript) VALUES (?,?,?)"; this.pstmt = this.conn.prepareStatement(sql); //实例化PrepareStatement对象 this.pstmt.setString(1,file.getAccount()); this.pstmt.setString(2,file.getFileName()); this.pstmt.setString(3, file.getDescription()); if(this.pstmt.executeUpdate() > 0){ //如果有更新操作 flag = true ; } this.pstmt.close() ; return flag ; } public List findByAccount(String acname) throws Exception{//按账号查找密码 List all = new ArrayList(); String sql = "SELECT FILE,descript FROM FILES WHERE acname=?" ; this.pstmt = this.conn.prepareStatement(sql) ; this.pstmt.setString(1,acname); ResultSet rs = this.pstmt.executeQuery() ; UploadFile file = null; while(rs.next()){ file = new UploadFile(); file.setFileName(rs.getString(1)); file.setDescription(rs.getString(2)); all.add(file); } this.pstmt.close(); return all; } public List findAll() throws Exception { //查找所有 List all = new ArrayList(); String sql = "SELECT * FROM FILES"; this.pstmt = this.conn.prepareStatement(sql) ; ResultSet rs = this.pstmt.executeQuery() ; UploadFile file = null; while(rs.next()){ file = new UploadFile(); file.setAccount(rs.getString(1)); file.setFileName(rs.getString(2)); file.setDescription(rs.getString(3)); all.add(file); } this.pstmt.close(); return all; } }

二十、代理主题实现类UploadFileDAOProxy:

package DAO ; import java.util.* ; import java.sql.* ; import DAO.UploadFileDAOImpl; import DAO.IUploadFileDAO; import dbc.DatabaseConnection; import VO.UploadFile; public class UploadFileDAOProxy implements IUploadFileDAO { private DatabaseConnection dbc = null ; private IUploadFileDAO dao = null ; public UploadFileDAOProxy() throws Exception { //在构造方法中实例化连接,同时实例化dao对象 this.dbc = new DatabaseConnection() ; this.dao = new UploadFileDAOImpl(this.dbc.getConnection()) ; } public boolean doCreate(UploadFile file) throws Exception{ boolean flag = false ; try{ flag = this.dao.doCreate(file) ; }catch(Exception e){ throw e ; }finally{ this.dbc.close() ; } return flag ; } public List findByAccount(String acname) throws Exception{ List all = null; try{ all = this.dao.findByAccount(acname) ; }catch(Exception e){ throw e ; }finally{ this.dbc.close() ; } return all ; } public List findAll() throws Exception{ List all = null; try{ all = this.dao.findAll() ; }catch(Exception e){ throw e ; }finally{ this.dbc.close() ; } return all ; } }

然后就可以上传文件了,并且上传成功后会把文件名,作者,和描述保存在数据库。上传文件需要用到jsmartcom包,而且我在解决上传的文件名乱码这一块花了很长时间,注意,我的项目里所使用的都是utf-8编码。有些jsmartcom包不支持中文,可以下载到别人改过的支持中文的包。

二十一、上传文件用UploadServlet:

package servlets; import java.io.IOException; import java.io.PrintWriter; import java.util.ArrayList; import factory.DAOFactory; import VO.UploadFile; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.Re; import javax.servlet.ServletConfig; import javax.servlet.http.HttpSession; import com.jspsmart.upload.File; import com.jspsmart.upload.SmartUpload; import com.jspsmart.upload.SmartUploadException; public class UploadServlet extends HttpServlet { //上传文件时的处理 public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { UploadFile uploadfile = new UploadFile(); HttpSession session = request.getSession(); String account = (String)session.getAttribute("account"); //获取session中的用户名 uploadfile.setAccount(account); SmartUpload smartUpload = new SmartUpload(); ServletConfig config = this.getServletConfig(); smartUpload.initialize(config, request, response); try{ smartUpload.upload(); String text = smartUpload.getRequest().getParameter("text"); uploadfile.setDescription(text); File smartFile = smartUpload.getFiles().getFile(0); String fileName = smartFile.getFileName(); smartFile.saveAs("/files/" + fileName,smartUpload.SAVE_VIRTUAL); request.setAttribute("msg", "上传成功!"); uploadfile.setFileName(fileName); DAOFactory.getIUploadFileDAOInstance().doCreate(uploadfile); }catch(SmartUploadException e){ request.setAttribute("msg", "上传失败!"); e.printStackTrace(); } catch (Exception e) { request.setAttribute("msg", "上传失败!"); e.printStackTrace(); } Re rd = request.getRe("/index.jsp"); rd.forward(request, response); } }

需注意,session中的用户名是在登录成功时设置的,我用一个session登录验证的过滤器来确保用户只有在登录的时候能够访问主页等页面,不然跳到登录页面:

二十二、LoginFilter:

package filters; import javax.servlet.http.HttpServlet; import java.io.IOException; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; public class LoginFilter implements Filter { //登录验证过滤器 public void init(FilterConfig config) throws ServletException{} public void doFilter(ServletRequest request,ServletResponse response, FilterChain chain) throws IOException,ServletException{ HttpServletRequest req = (HttpServletRequest)request; HttpSession ses = req.getSession(); if(ses.getAttribute("account")!=null){ chain.doFilter(request, response); }else{ request.getRe("login.jsp").forward(request, response); } } public void destroy(){} }

而点击页面上方的”退出登录“时,就要删除session中的用户,并跳到登录页:

二十三、loginOut.jsp:

二十四、最后,还有点击”我的上传“和“他的上传”的页面:myUpload.jsp:

WormJam-我的上传 退出登录 返回首页

我的上传

文件名 描述 下载

hisUpload.jsp:

WormJam-他的上传 退出登录 返回首页

文件名 描述 下载

最后补充一点,点击下载时会新开一个页面显示文件的内容,而右键“下载”点击“链接另存为”时才会下载,特别是如果文件是zip等不能查看的类型就会出错。也有使点击就能下载的方法,我比较懒就没去试了。

转载自原创:https://blog.csdn.net/u012077155/article/details/43115507

x00-x7f ↩︎

收藏
  • 人气文章
  • 最新文章
  • 下载排行榜
  • 热门排行榜