JavaWeb新手小项目以及源码

javaweb 专栏收录该内容
17 篇文章 1 订阅

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

先来几张截图:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

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

<%@ page language="java" import="java.util.*" pageEncoding="utf-8" %>
<html>
	<head>
		<title>WormJam-登录</title>
		<link rel="stylesheet" href="css/style.css" type="text/css">
	</head>
	<body>
		<div class="loginHeader">
			<div class="title">
				<center><span>WormJam</span></center>
				<center><h3>秀出你的源代码</h3></center>
			</div>
		</div>		
		<div class="form">
			<form method="post" name="loginForm">
				账号:<input type="text" name="account" onInput="accountChange()">
				<span id="accountText"> </span><span id="loginInfo"></span><br><br>
				密码:<input type="password" name="password" onInput="passwordChange()">
				<span id="passwordText"> </span><br><br>	
				验证码:<input type="text" name="code" size="10" onInput="validate()">
				<img name="imgValidate" "refresh()" border=0 src="validate.jsp"></img>
				<span id="codeText" name="codeText" style="color:green"> </span><br><br>	
				<input type="button" value="登录" "validateSubmit()">
				<a href="register.jsp">注册</a>
			</form>
		</div>
		<script>
			//验证账号密码格式
			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<17){
					passwordText.innerHTML = "√";
					passwordText.style.color = "green";
				}else{
					passwordText.innerHTML = "密码必须是6至16位!";
					passwordText.style.color = "red";
				}
			}
			//验证码刷新
			function refresh(){
				//加入随机参数防止调用浏览器缓存
				loginForm.imgValidate.src = "validate.jsp" + "?id=" + Math.random();
			}
			//表单提交验证
			function validateSubmit(){
				if(accountText.innerHTML=="√" && passwordText.innerHTML == "√" && codeText.innerHTML == "√"){
					var account = loginForm.account.value;
					var password = loginForm.password.value;
					var xmlHttp = new XMLHttpRequest();
					var url = "LoginServlet?account=" + account + "&password=" + password;
					xmlHttp.open("POST",url,true);
					xmlHttp.onreadystatechange = function(){
						if(xmlHttp.readyState == 4){
							loginInfo.innerHTML = xmlHttp.responseText;
						}else{
							loginInfo.innerHTML = "正在验证密码……";
						}
				}
				xmlHttp.send();
				}else{
					return false;
				}
			}			
			//AJAX
			function validate(){
				var code = loginForm.code.value;
				var xmlHttp = new XMLHttpRequest();
				var url = "ValidateServlet?code=" + code;
				xmlHttp.open("POST",url,true);
				xmlHttp.onreadystatechange = function(){
					if(xmlHttp.readyState == 4){
						codeText.innerHTML = xmlHttp.responseText;
					}else{
						codeText.innerHTML = "正在验证……";
					}
				}
				xmlHttp.send();
			}
		</script>
	</body>
</html>

登录的表单里先不看验证码,为了限制账号及密码 的格式为6至16位的数字或字母,用input里的onInput事件,当账号或密码框中的值发生改变时即调用accountChange()或passwordChange()函数进行输入格式的验证。accountChange()中的正则表达式/[a-zA-Z0-9]{6,16}/;表示6至16位的字母或数字,passwordChange()中的/1+$/表示键盘上能够输入的符号。

二、生成验证码validate.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8" %>
<%@ page import="java.awt.*" import="java.awt.image.BufferedImage" import="javax.imageio.ImageIO" %>
<html>
<body>
<%
	response.setHeader("Cache-Control", "no-cache");
	//在内存中创建图像
	int width = 60,height = 20;
	BufferedImage image = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
	//获取画笔
	Graphics g = image.getGraphics();
	//设置背景色
	g.setColor(new Color(200,200,200));
	g.fillRect(0,0,width,height);
	//随机产生验证码
	Random rnd = new Random();
	int randNum = rnd.nextInt(8999) + 1000;
	String randStr = String.valueOf(randNum);
	//将验证码存入session
	session.setAttribute("randStr",randStr);
	//将验证码显示在图像中
	g.setColor(Color.black);
	g.setFont(new Font("",Font.PLAIN,20));
	g.drawString(randStr,10,17);
	//随机产生100个随机点
	for(int i=0;i<100;i++){
		int x = rnd.nextInt(width);
		int y = rnd.nextInt(height);
		g.drawOval(x,y,1,1);
	}
	//输出图像到页面
	ImageIO.write(image,"JPEG", response.getOutputStream());
	out.clear();
	out = pageContext.pushBody();
%>
</body>
</html>

这段代码生成一个验证码并放在图片中显示,然后login.jsp的登录表单中用img标签把这个验证码图片加载进来。

三、然后写个ValidateServlet对输入的验证码进行验证:

package servlets;
 
import java.io.IOException;
import java.io.PrintWriter;
import java.util.*;
 
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
 
public class ValidateServlet extends HttpServlet {	//对验证码验证
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		//得到提交的验证码
		String code = request.getParameter("code");
		//获取session中的验证码
		HttpSession session = request.getSession();
		String randStr = (String)session.getAttribute("randStr");
		//response.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();
		if(!code.equals(randStr)){
			out.println("");
		}
		else{
			out.print("√");
		}
	}
}

当输入的验证码正确时输出"√",错误时输出空格(无显示),顺便说一句,login.jsp里的validateSubmit()可以看到,我的登录表单是通过判定输出的文本来提交的,即如果账号密码和验证码后面的文本输出不全是"√"的话就无法提交表单。
现在的问题是,在servlet中输出的话,浏览器会新打开一个页面来显示验证码验证的结果,所以要用到AJAX把输出显示在登录页验证码图片的后面,login.jsp里的validate()就使用了AJAX,使验证码验证的结果显示在id为codeText的span标签里,并传一个名为code值为你输入的验证码的参数给ValidateServlet。验证正确时就可以提交给LoginServlet进行密码的验证了。

四、在这之前先写注册页面register.jsp:

<%@ page language="java" import="java.util.*" pageEncoding="utf-8" %>
<html>
	<head>
		<title>WormJam-注册</title>
		<link rel="stylesheet" href="css/style.css" type="text/css">
	</head>
	<body>
		<div class="loginHeader">
			<div class="title">
				<center><span>WormJam</span></center>
				<center><h3>秀出你的源代码</h3></center>
			</div>
		</div>	
		<div class="form">
			<form method="post" name="registerForm">
				输入账号:<input type="text" name="account" onInput="accountChange()"><br>
				<span id="accountText"></span><br>
				输入密码:<input type="password" name="password1" onInput="password1Change()"><br>
				<span id="passwordText1"></span><br>
				确认密码: <input type="password" name="password2" onInput="password2Change()"><br>
				<span id="passwordText2"></span><br>
				<span id="info"></span><br>
				<input type="button" value="注册" "formSubmit()">
				<a href="login.jsp">返回登录</a>
			</form>
		</div>
		<script>
			function accountChange(){
				var str = registerForm.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 password1Change(){
				var str = registerForm.password1.value;
				var reg = /^[x00-x7f]+$/;
				if(reg.test(str) && str.length>5 && str.length<17){
					passwordText1.innerHTML = "√";
					passwordText1.style.color = "green";
				}else{
					passwordText1.innerHTML = "密码必须是6至16位!";
					passwordText1.style.color = "red";
				}
			}
			function password2Change(){
				var str1 = registerForm.password1.value;
				var str2 = registerForm.password2.value;
				if(str1 == str2){
					passwordText2.innerHTML = "√";
					passwordText2.style.color = "green";
				}else{
					passwordText2.innerHTML = "两次密码不一致";
					passwordText2.style.color = "red";
				}
			}
			//表单验证
			function formSubmit(){
				if(accountText.innerHTML=="√" && passwordText1.innerHTML=="√" && passwordText2.innerHTML=="√"){
					registerSubmit();
				}else{
					return false;
				}
			}
			//AJAX
			function registerSubmit(){
				var account = registerForm.account.value;
				var password = registerForm.password1.value;
				var xmlHttp = new XMLHttpRequest();
				var url = "RegisterServlet?account=" + account + "&password=" + password;
				xmlHttp.open("POST",url,true);
				xmlHttp.onreadystatechange = function(){
					if(xmlHttp.readyState == 4){
						info.innerHTML = xmlHttp.responseText;
					}else{
						info.innerHTML = "正在注册……";
					}
				}
				xmlHttp.send();
			}
		</script>
	</body>
</html>

表单方面包括格式验证、AJAX都和登录页面同理,提交给RegisterServlet注册,现在就要开始连接数据库操作了。JSP连接数据库的一些配置我就不赘述了。

五、数据库脚本:

create table ACCOUNTS(
	ACNAME varchar(16) not null,
	PASSWD varchar(16) not null
);

为了方便就写了这两列,账号名和密码,16位不为空。然后数据库我是采用DAO设计模式。

六、先是VO类Account.java:

package VO;
 
public class Account {
	private String account;					//定义账户
	private String password;				//定义密码
	public String getAccount(){
		return account;
	}
	public void setAccount(String account){
		this.account = account;
	}
	public String getPassword(){
		return password;
	}
	public void setPassword(String password){
		this.password = password;
	}
}

分别定义了get和set方法。

七、然后是数据库连接类DatabaseConnection.java:

package dbc;
 
import java.sql.*;
 
public class DatabaseConnection {
	private static final String DBDRIVER = "org.gjt.mm.mysql.Driver" ; 
	private static final String DBURL = "jdbc:mysql://localhost:3306/wormjam" ;
	private static final String DBUSER = "root" ;
	private static final String DBPASSWORD = "你的密码" ;
	private Connection conn ;
	public DatabaseConnection() throws Exception {				//在构造方法中进行数据库连接
		Class.forName(DBDRIVER) ;								//加载驱动程序
		this.conn = DriverManager.getConnection(DBURL,DBUSER,DBPASSWORD) ;
	}
	public Connection getConnection(){							//取得数据库连接
		return this.conn ;
	}
	public void close() throws Exception {						//关闭数据库
		if(this.conn != null){
			try{
				this.conn.close() ;
			}catch(Exception e){
				throw e ;
			}
		}
	}
}

八、然后定义DAO操作标准IAccountDAO.java:

package DAO ;
 
import java.util.* ;
import VO.Account;
 
public interface IAccountDAO {			//定义DAO操作
	public boolean doCreate(Account account) throws Exception ;
	public String findByAccount(String acname) throws Exception ;
}

定义了两个操作:创建和寻找。创建用来注册用户,而寻找用来比对密码是否正确。

九、接着是真实主题实现类AccountDAOImpl.java:

package DAO ;
 
import java.util.* ;
import java.sql.* ;
import DAO.IAccountDAO; 
import VO.Account;
 
public class AccountDAOImpl implements IAccountDAO {
	private Connection conn = null ;						//数据库连接对象
	private PreparedStatement pstmt = null ;				//数据库操作对象
	public AccountDAOImpl(Connection conn){					//通过构造方法取得数据库连接
		this.conn = conn ;
	}
	public boolean doCreate(Account account) throws Exception{ //添加账户
		boolean flag = false ;								//定义标志位
		String sql = "INSERT INTO ACCOUNTS(ACNAME,PASSWD) VALUES (?,?)" ;
		this.pstmt = this.conn.prepareStatement(sql) ;		//实例化PrepareStatement对象
		this.pstmt.setString(1,account.getAccount()) ;
		this.pstmt.setString(2,account.getPassword()) ;
		if(this.pstmt.executeUpdate() > 0){					//如果有更新操作
			flag = true ;
		}
		this.pstmt.close() ;
		return flag ;
	}
	public String findByAccount(String acname) throws Exception{//按账号查找密码
		String password = null;
		String sql = "SELECT PASSWD FROM ACCOUNTS WHERE ACNAME=?" ;
		this.pstmt = this.conn.prepareStatement(sql) ;
		this.pstmt.setString(1,acname);
		ResultSet rs = this.pstmt.executeQuery() ;
		if(rs.next()){
			password = rs.getString("PASSWD");
		}
		this.pstmt.close() ;
		return password ;
	}
}

十、代理主题实现类AccountDAOProxy.java:

package DAO ;
 
import java.util.* ;
import java.sql.* ;
 
import DAO.AccountDAOImpl; 
import DAO.IAccountDAO;
import dbc.DatabaseConnection;
import VO.Account;
 
public class AccountDAOProxy implements IAccountDAO {
	private DatabaseConnection dbc = null ;
	private IAccountDAO dao = null ;
	public AccountDAOProxy() throws Exception {		//在构造方法中实例化连接,同时实例化dao对象
		this.dbc = new DatabaseConnection() ;
		this.dao = new AccountDAOImpl(this.dbc.getConnection()) ;
	}
	public boolean doCreate(Account account) throws Exception{				
		boolean flag = false ;
		try{
			if(this.dao.findByAccount(account.getAccount()) == null){
				flag = this.dao.doCreate(account) ;
			}
		}catch(Exception e){
			throw e ;
		}finally{
			this.dbc.close() ;
		}
		return flag ;
	}
	public String findByAccount(String acname) throws Exception{
		String password = null ;
		try{
			password = this.dao.findByAccount(acname) ;
		}catch(Exception e){
			throw e ;
		}finally{
			this.dbc.close() ;
		}
		return password ;
	}
}

十一、DAO工厂类DAOFactory:

package factory;
 
import DAO.IAccountDAO;
import DAO.AccountDAOProxy;
import DAO.IUploadFileDAO;
import DAO.UploadFileDAOProxy;
 
public class DAOFactory {
	public static IAccountDAO getIAccountDAOInstance() throws Exception{  //取得DAO接口实例
		return new AccountDAOProxy();				//取得代理类实例
	}
	public static IUploadFileDAO getIUploadFileDAOInstance() throws Exception{
		return new UploadFileDAOProxy();
	}
}

不要问我为什么这么多类这么麻烦。。这是小项目看起来是很麻烦,如果是大项目的话这样就更方便了。

十二、写完这些就可以在RegisterServlet里愉快的注册了:

package servlets;
 
import java.io.IOException;
import java.io.PrintWriter;
import java.util.*;
 
import md5.MD5;
import factory.DAOFactory;
import VO.Account;
 
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
public class RegisterServlet extends HttpServlet {	//注册账号
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		PrintWriter out = response.getWriter();
		Account account = new Account() ;
		account.setAccount(request.getParameter("account"));
		try {
			account.setPassword(MD5.generateCode(request.getParameter("password")));
		} catch (Exception e1) {
			e1.printStackTrace();
		}
		try{
			if(DAOFactory.getIAccountDAOInstance().doCreate(account)){  //执行添加操作
				out.print("注册成功!");
			} else {
				out.print("该用户已被注册!");
			}
		}catch(Exception e){
			e.printStackTrace() ;
			out.print("注册失败!");
		}
	}
}

十三、这里当向数据库增加一个账号时使用了MD5加密,

这样在数据库中存放的密码就是经过处理的密文,

就算查询数据库看到的密码也是看不懂的乱码。MD5.java:

package md5;
 
import java.security.MessageDigest;
 
public class MD5 {
	public static String generateCode(String str) throws Exception{    //MD5加密
		MessageDigest md5 = MessageDigest.getInstance("MD5");
		byte[] srcBytes = str.getBytes();
		md5.update(srcBytes);
		byte[] resultBytes = md5.digest();
		String result = new String(resultBytes);
		return result;
	}
}

以上就能成功地注册了。
既然能注册了,也就能登录了。

十四、现在写LoginServlet:

package servlets;
 
import java.io.IOException;
import java.io.PrintWriter;
import java.util.*;
import factory.DAOFactory;
import VO.Account;
import md5.MD5;
 
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
public class LoginServlet extends HttpServlet {    //验证账号密码
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		PrintWriter out = response.getWriter();
		String account = request.getParameter("account");
		try {
			String password = MD5.generateCode(request.getParameter("password"));
			if(DAOFactory.getIAccountDAOInstance().findByAccount(account).equals(password)){
				request.getSession().setAttribute("account", account);
				response.sendRedirect("toIndex.jsp");
			}else{
				out.print("账号或密码不正确");
			}
		}catch(Exception e){
			e.printStackTrace() ;
			out.print("登录错误");
		}
	}
}

十五、这里当验证成功时跳转到toIndex:

<%@ page language="java" import="java.util.*" pageEncoding="utf-8" %>
<html>
	<head><meta http-equiv="refresh" content="0;url=index.jsp"> </head>
	<body>
	正在登录……
	</body>
</html>

为什么不直接跳到主页呢?因为我在登录页面写了AJAX,使LoginServlet的输出显示在登录页上,比如密码错时就把”账号或密码不正确“显示在登录页。但是登录成功时如果直接跳转到Index.jsp会让首页显示在登录页那里。所以设置一个过渡页面,然后再通过标签跳转到首页。

十六、index.jsp:

<%@ page language="java" import="java.util.*" pageEncoding="utf-8" %>
<%@ page import="factory.DAOFactory" %>
<%@ page import="VO.UploadFile" %>
<% request.setCharacterEncoding("utf-8"); %>
<html>
	<head>
		<title>WormJam-首页</title>
		<head><link rel="stylesheet" href="css/style.css" type="text/css"></head>
	</head>
	<body>
		<div class="header">
		<div class="headerText">
		<%
			String account = (String)session.getAttribute("account");
			out.println("欢迎你," + account);
		 %>
		<a href="loginOut.jsp">退出登录</a>
		</div>
		</div>
		<div class="body">
		<h1>上传我的源代码</h1>
		<form action="UploadServlet" method="post" enctype="multipart/form-data">
			选择一个文件:
			<input type="file" name="myFile"><br>
			添加描述:<br>
			<textarea rows="3" cols="80" name="text"></textarea><br>
			<input type="submit" value="上传">
			<a href="myUpload.jsp">我上传的</a>
		</form>
		${msg }
		<hr>
		<h1>热门分享</h1>
		<table width="80%">
		<tr class="title">
			<td>文件名</td>
			<td>作者</td>
			<td>描述</td>
			<td>下载</td>
		</tr>
		<%
			List<UploadFile> all = DAOFactory.getIUploadFileDAOInstance().findAll();
			Iterator<UploadFile> iter = all.iterator();
			while(iter.hasNext()){
				UploadFile file = iter.next();
				out.println("<tr>");
				out.println("<td>" + file.getFileName() + "</td>");
				out.println("<td><a href='hisUpload.jsp?account=" + file.getAccount() + "'>" + file.getAccount() + "</td>");
				out.println("<td>" + file.getDescription() + "</td>");
				out.println("<td><a href='/WormJam/files/" + file.getFileName() +"'>下载</a>");
				out.println("<tr>");
			}
		 %>
		 </table>
		 </div>
	</body>
</html>

在首页,可以上传文件,然后再热门分享中把所有文件以及上传文件的作者列出来。我这里没有遍历文件夹来列出文件,而是在上传文件的同时记录到数据库。因此还应建立上传文件的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<UploadFile> findByAccount(String account) throws Exception;	//按账号查找文件
	public List<UploadFile> 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<UploadFile> findByAccount(String acname) throws Exception{//按账号查找密码
		List<UploadFile> all = new ArrayList<UploadFile>();
		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<UploadFile> findAll() throws Exception { //查找所有
		List<UploadFile> all = new ArrayList<UploadFile>();
		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<UploadFile> findByAccount(String acname) throws Exception{
		List<UploadFile> all = null;
		try{
			all = this.dao.findByAccount(acname) ;
		}catch(Exception e){
			throw e ;
		}finally{
			this.dbc.close() ;
		}
		return all ;
	}
	public List<UploadFile> findAll() throws Exception{
		List<UploadFile> 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.RequestDispatcher;
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();
		}
		RequestDispatcher rd = request.getRequestDispatcher("/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.getRequestDispatcher("login.jsp").forward(request, response);
		}
	}
	public void destroy(){}
}

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

二十三、loginOut.jsp:

<%@ page language="java" import="java.util.*" pageEncoding="utf-8" %>
<% 
	session.removeAttribute("account");
	response.sendRedirect("login.jsp");
%>

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

<%@ page language="java" import="java.util.*" pageEncoding="utf-8" %>
<%@ page import="factory.DAOFactory" %>
<%@ page import="VO.UploadFile" %>
<% request.setCharacterEncoding("utf-8"); %>
<html>
	<head>
		<title>WormJam-我的上传</title>
		<head><link rel="stylesheet" href="css/style.css" type="text/css"></head>
	</head>
	<body>
		<div class="header">
		<div class="headerText">
		<%
			String account = (String)session.getAttribute("account");
			out.println("欢迎你," + account);
		 %>
		<a href="loginOut.jsp">退出登录</a>
		<a href="index.jsp">返回首页</a>
		</div>
		</div>
		<div class="body">
		<h1>我的上传</h1>
		<table width="60%">
		<tr class="title">
			<td>文件名</td>
			<td>描述</td>
			<td>下载</td>
		</tr>
		<%
			List<UploadFile> all = DAOFactory.getIUploadFileDAOInstance().findByAccount(account);
			Iterator<UploadFile> iter = all.iterator();
			while(iter.hasNext()){
				UploadFile file = iter.next();
				out.println("<tr>");
				out.println("<td>" + file.getFileName() + "</td>");
				out.println("<td>" + file.getDescription() + "</td>");
				out.println("<td><a href='/WormJam/files/" + file.getFileName() +"'>下载</a>");
				out.println("<tr>");
			}
		 %>
		 </table>
		 </div>
	</body>
</html>

hisUpload.jsp:

<%@ page language="java" import="java.util.*" pageEncoding="utf-8" %>
<%@ page import="factory.DAOFactory" %>
<%@ page import="VO.UploadFile" %>
<% request.setCharacterEncoding("utf-8"); %>
<html>
	<head>
		<title>WormJam-他的上传</title>
		<head><link rel="stylesheet" href="css/style.css" type="text/css"></head>
	</head>
	<body>
		<div class="header">
		<div class="headerText">
		<%
			String account = (String)session.getAttribute("account");
			out.println("欢迎你," + account);
			String acname = request.getParameter("account");
		 %>
		<a href="loginOut.jsp">退出登录</a>
		<a href="index.jsp">返回首页</a>
		</div>
		</div>
		<div class="body">
		<h1><%= acname + "的上传" %></h1>
		<table width="60%">
		<tr class="title">
			<td>文件名</td>
			<td>描述</td>
			<td>下载</td>
		</tr>
		<%
			List<UploadFile> all = DAOFactory.getIUploadFileDAOInstance().findByAccount(acname);
			Iterator<UploadFile> iter = all.iterator();
			while(iter.hasNext()){
				UploadFile file = iter.next();
				out.println("<tr>");
				out.println("<td>" + file.getFileName() + "</td>");
				out.println("<td>" + file.getDescription() + "</td>");
				out.println("<td><a href='/WormJam/files/" + file.getFileName() +"'>下载</a>");
				out.println("<tr>");
			}
		 %>
		 </table>
		 </div>
	</body>
</html>

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

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


  1. x00-x7f ↩︎

  • 16
    点赞
  • 0
    评论
  • 82
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

表情包
插入表情
评论将由博主筛选后显示,对所有人可见 | 还能输入1000个字符
©️2020 CSDN 皮肤主题: 技术黑板 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值