1、 Web 开发与应用实验报告系 别 电 子 信 息 系专 业 计 算 机 科 学 与 技 术班级学号姓 名指导教师2010 年 12 月 27 月一, 相关技术本系统主要采用 jsp,servlet,session,mysql,html,xml,等知识点,配合 Navicat 软件,采用以下技术实现:1 数据存储由于本系统是个简易网盘,故涉及到的数据量比较小,故采用小巧的MySQL,鉴于 MySQL 没有企业管理器,于是采用 Navicat 软件以简化 mysql 的使用。2 页面显示由于涉及到的动态页面,故页面的主语言选用 jsp。3 后台处理由于涉及到连接数据库,处于安全考虑,有关数据库的
2、连接操作至于后台;而动态页面需要的动态事件的触发、以及响应等主要事件均放在 servlet 中进行。4 数据传递鉴于本系统的页面较多,故使用 session 进行数据的传递。二, 系统需求分析1.用户登陆未注册用户可以进入注册页面进行注册,已注册的用户可以进行登录,若用户不存或者密码错误则提示重新输入。用户名密码匹配,则进入个人主页。2.用户注册能够进行注册验证,对“用户名重复” , “密码与确认不一致”等问题就行纠错,并能将正确的数据输入到数据库中。3.上传文件能够上传文件到 D:中,并能将文件的名字存入到数据库中。4.下载文件会在页面中显示每个用户已经上传的全部文件,并可以下载上述文件。三
3、, 系统设计1.数据库drop table if exists user;drop table if exists file;/* Table: user */create table data(id varchar(100),password varchar(100),primary key (id);/* Table: file */create table user(id varchar(20),url varchar(20),primary key (id,url),foreign key (id) references user (id) );2.详细代码:=Login.jsp=“M
4、y JSP Login.jsp starting page欢迎! 用户名: 密码:还未注册?=testLogin.jsp=注册alert(欢迎回来“+ reg_name+ “!);location.replace(UploadFile.jsp);“);flag=true;break; else if (reg_name.equals(rs.getString(1)location.replace(Login.jsp);“);flag=true;break; if(!flag)out.println(“alert(用户名不存在!);location.replace(Login.jsp);“);r
5、s.close();con.close(); catch (Exception e) out.println(e);%=Register.jsp=“My JSP Login.jsp starting page欢迎加入我们! 请输入用户名: 请输入密码:请再次输入密码:=testRegister.jsp=注册请重新注册“);out.println(“alert(对不起,用户名“+ reg_name+ “已存在);location.replace(Register.jsp);“);flag1 = false;System.out.println(“moved1“);break;if (flag1)
6、System.out.println(“moved“);if(!reg_pass.equals(reg_rpass) out.println(“alert(密码不一致,请重新输入);location.replace(Register.jsp)“); else if ( reg_pass.equals(reg_rpass) PreparedStatement stat = con.prepareStatement(“insert user values(?,?)“);stat.setString(1, reg_name);stat.setString(2, reg_pass);stat.exec
7、uteUpdate();out.println(“注册成功“);out.println(“您的用户名是:“ + reg_name+ “);out.println(“您的密码是: “ + reg_pass + “);stat.close();String name_reged = null;session.setAttribute(“username“, reg_name);out.println(“alert(谢谢注册);location.replace(UploadFile.jsp)“);rs.close();con.close(); catch (Exception e) out.prin
8、tln(e);%=UploadFile.jsp=“UploadFile你的名字是:请选择你要上传的文件:=DowmloadFile.jsp=“My JSP DownloadFile.jsp starting page你的名字是:请选择你要下载的文件:“);out.println(“download this file“);out.println(“);out.println(“ “);rs.close() ;con.close() ; catch(Exception e)out.println(e);%=Upload.java=package servlet;import java.io.Fi
9、le;import java.io.IOException;import java.io.PrintWriter;import java.sql.SQLException;import java.util.List;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.http
10、.HttpSession;import mons.fileupload.DefaultFileItemFactory;import mons.fileupload.DiskFileUpload;import mons.fileupload.FileItem;import com.mysql.jdbc.Connection;import com.mysql.jdbc.PreparedStatement;import com.mysql.jdbc.ResultSet;import com.mysql.jdbc.Statement;import util.JdbcUtil;public class
11、Upload extends HttpServlet public Upload() super();public void destroy() super.destroy(); / Just puts “destroy“ string in log/ Put your code herepublic void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException String result = “;HttpSession session = req
12、uest.getSession();String name=(String) session.getAttribute(“username“);String path = “D:“;Connection con=(Connection) JdbcUtil.getConnection();String sql = “insert into file values(?,?)“; String nm = null;File foder = new File(path);/ 如果文件夹不存在,则创建文件夹if (foder.exists() = false) foder.mkdirs();/ 多级目录
13、/ foder.mkdir();/只创建一级目录try DefaultFileItemFactory factory = new DefaultFileItemFactory();DiskFileUpload up = new DiskFileUpload(factory);List ls = up.parseRequest(request);System.out.println(ls.size();for (FileItem fileItem : ls) if (fileItem.isFormField() String FieldName = fileItem.getFieldName()
14、;String Content = fileItem.getString(“gbk“);request.setAttribute(FieldName, Content); else nm = fileItem.getName().substring(fileItem.getName().lastIndexOf(“) + 1);File mkr = new File(path, nm);if (mkr.createNewFile() System.out.println(“uploading“);System.out.println(fileItem.getFieldName();System.
15、out.println(nm);fileItem.write(mkr);result = “success!“; catch (Exception e) e.printStackTrace();result = “fail!“;try PreparedStatement stat=(PreparedStatement) con.prepareStatement(sql);stat.setString(1,name);stat.setString(2,nm);stat.executeUpdate();stat.close(); catch (SQLException e1) / TODO Aut
16、o-generated catch blocke1.printStackTrace();System.out.println(nm); response.setContentType(“text/html“);PrintWriter out = response.getWriter();out.println(“);out.println(“);out.println(“ A Servlet“);out.println(“ “);out.println(“alert(succeess);location.replace(/Mysqltest/DownloadFile.jsp)“);out.pr
17、intln(“ “);out.println(“);out.flush();out.close();public void init() throws ServletException / Put your code here=Download.java=package servlet;import java.io.File;import java.io.FileInputStream;import java.io.IOException;import java.io.InputStream;import java.io.PrintWriter;import .URLEncoder;impor
18、t javax.servlet.ServletException;import javax.servlet.ServletOutputStream;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;public class Download extends HttpServlet public Download() super();public void destroy() super.d
19、estroy(); / Just puts “destroy“ string in log/ Put your code herepublic void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException String filepath = request.getParameter(“filepath“);String fullFilePath = “D:“ + filepath;/*读取文件,到 D 盘下载*/File file = new Fi
20、le(fullFilePath);/*如果文件存在*/if (file.exists() String filename = URLEncoder.encode(file.getName(), “utf-8“);response.reset();response.setContentType(“application/x-msdownload“);response.addHeader(“Content-Disposition“, “attachment; filename=“ + filename + “);int fileLength = (int) file.length();respon
21、se.setContentLength(fileLength);/*如果文件长度大于 0*/if (fileLength != 0) /*创建输入流*/InputStream inStream = new FileInputStream(file);byte buf = new byte4096;/*创建输出流*/ServletOutputStream servletOS = response.getOutputStream();int readLength;while (readLength = inStream.read(buf) != -1) servletOS.write(buf, 0
22、, readLength);inStream.close();servletOS.flush();servletOS.close();public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException response.setContentType(“text/html“);PrintWriter out = response.getWriter();out.println(“);out.println(“);out.println(“
23、A Servlet“);out.println(“ “);out.print(“ This is “);out.print(this.getClass();out.println(“, using the POST method“);out.println(“ “);out.println(“);out.flush();out.close();public void init() throws ServletException / Put your code here=JdbcUtil.java=package util;import java.sql.Connection;import ja
24、va.sql.DriverManager;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;public class JdbcUtil static try Class.forName(“com.mysql.jdbc.Driver“); catch (ClassNotFoundException e) e.printStackTrace();public static Connection getConnection()Connection con=null;String url=“
25、jdbc:mysql:/localhost:3306/test“;String userName=“root“;String pwd=“sql“;try con=DriverManager.getConnection(url, userName, pwd); catch (SQLException e) e.printStackTrace();return con;public static void relaseResource(ResultSet rs,Statement st,Connection conn)if(rs!=null)try rs.close(); catch (SQLException e) e.printStackTrace();if(st!=null)try st.close(); catch (SQLException e) e.printStackTrace();if(conn!=null)try conn.close(); catch (SQLException e) e.printStackTrace();四, 实验结果