主:项目地址:http://download.csdn.net/download/qq_28316183/9977095:

1.首先看数据表结构

user表:

album表:

picture表:

2.查看项目的目录树;

3.登录页面index.jsp:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head><base href="<%=basePath%>"><title>登录我的相册</title><meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0">    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><!--<link rel="stylesheet" type="text/css" href="styles.css">--><meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" type="text/css" href="css/normalize.css" /><link rel="stylesheet" type="text/css" href="css/demo.css" /><link rel="stylesheet" type="text/css" href="css/component.css" /><link rel="stylesheet" type="text/css" href="css/content.css" /><script src="js/modernizr.custom.js"></script></head><body><body><div class="container"><header class="codrops-header"><h1>相册管理系统</h1><p>记录下美好瞬间</p><nav class="codrops-demos"><a class="current-demo" href="index.html">登录/注册</a></nav></header><section><p>请选择以下<strong>选项</strong>:</p><div class="mockup-content"><p>1234567890</p><div class="morph-button morph-button-modal morph-button-modal-2 morph-button-fixed"><button type="button">登录</button><div class="morph-content"><div><div class="content-style-form content-style-form-1"><span class="icon icon-close">Close the dialog</span><h2>登录</h2><form action="LoginServlet" method="post"><p><label>账号:</label><input type="text" name="username" /></p><p><label>密码:</label><input type="password" name="password" /></p><p><input type="submit" id="button" value="登录"></p></form></div></div></div></div><!-- morph-button --><strong class="joiner">or</strong><div class="morph-button morph-button-modal morph-button-modal-3 morph-button-fixed"><button type="button">注册</button><div class="morph-content"><div><div class="content-style-form content-style-form-2"><span class="icon icon-close">Close the dialog</span><h2>注册</h2><form action="SaveUser" method="post"><p><label>账号:</label><input type="text" name="username" /></p><p><label>密码:</label><input type="password" name="password" /></p><p><label>重新输入密码:</label><input type="password"  name="repassword"/></p><p><input type="submit" id="button" value="注册"></p></form></div></div></div></div><!-- morph-button --><p>Kohlrabi radish okra azuki bean corn fava bean mustard tigernut juccama green bean celtuce collard greens avocado quandong.</p></div><!-- /form-mockup --></section></div><!-- /container --><script src="js/classie.js"></script><script src="js/uiMorphingButton_fixed.js"></script><script>(function() {var docElem = window.document.documentElement, didScroll, scrollPosition;// trick to prevent scrolling when opening/closing buttonfunction noScrollFn() {window.scrollTo( scrollPosition ? scrollPosition.x : 0, scrollPosition ? scrollPosition.y : 0 );}function noScroll() {window.removeEventListener( 'scroll', scrollHandler );window.addEventListener( 'scroll', noScrollFn );}function scrollFn() {window.addEventListener( 'scroll', scrollHandler );}function canScroll() {window.removeEventListener( 'scroll', noScrollFn );scrollFn();}function scrollHandler() {if( !didScroll ) {didScroll = true;setTimeout( function() { scrollPage(); }, 60 );}};function scrollPage() {scrollPosition = { x : window.pageXOffset || docElem.scrollLeft, y : window.pageYOffset || docElem.scrollTop };didScroll = false;};scrollFn();[].slice.call( document.querySelectorAll( '.morph-button' ) ).forEach( function( bttn ) {new UIMorphingButton( bttn, {closeEl : '.icon-close',onBeforeOpen : function() {// don't allow to scrollnoScroll();},onAfterOpen : function() {// can scroll againcanScroll();},onBeforeClose : function() {// don't allow to scrollnoScroll();},onAfterClose : function() {// can scroll againcanScroll();}} );} );// for demo purposes only[].slice.call( document.querySelectorAll( 'form button' ) ).forEach( function( bttn ) { bttn.addEventListener( 'click', function( ev ) { ev.preventDefault(); } );} );})();</script></body></body>
</html>

用户登录的Servlet:LoginServlet:

package com.jkx.web.servlet;import java.io.IOException;
import java.io.PrintWriter;
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.HttpSession;import com.jkx.dao.AlbumDao;
import com.jkx.dao.UserDao;
import com.jkx.po.Album;
import com.jkx.po.User;public class LoginServlet extends HttpServlet {public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {doPost(request, response);}public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {//对获取的参数有效,但不一定能解决问题,默认只适合post提交response.setCharacterEncoding("utf-8");response.setContentType("text/html");PrintWriter out =response.getWriter();String qq=request.getParameter("username");qq=new String(qq.getBytes("gbk"),"utf-8");//支队所传的参数有效String pass=request.getParameter("password");UserDao userdao=UserDao.getIntance();User user=userdao.getUserByusernameAndPasswoed(qq, pass);int userid=user.getUserid();if(user!=null){AlbumDao albumdao=AlbumDao.getIntance();List<Album> albumlist=albumdao.getAlbumListbyuseris(userid);request.setAttribute("albumlist", albumlist);HttpSession session=request.getSession();session.setAttribute("user", qq);request.getRequestDispatcher("/ImageAlbum.jsp").forward(request,response);return ; //结束语句}else{
//          request.getRequestDispatcher("/fail.jsp").forward(request,response);response.sendRedirect("/fail.jsp");return ; //����}}}

用户注册的Servlet:SaveUser

package com.jkx.web.servlet;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 com.jkx.dao.UserDao;
import com.jkx.po.User;public class SaveUser extends HttpServlet {public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {doPost(request, response);}public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {response.setContentType("text/html");response.setCharacterEncoding("utf-8");PrintWriter out = response.getWriter();UserDao userdao = UserDao.getIntance();String username = request.getParameter("username");String password = request.getParameter("password");String repassword = request.getParameter("repassword");User name = userdao.getUserByusername(username);User user = new User();if(name==null){if (password.equals(repassword)) {boolean boo = false;try {user.setUsername(username);user.setPassword(password);boo = userdao.saveUser(user);} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}if (boo == true) {request.getRequestDispatcher("/index.jsp").forward(request,response);return; // 结束语句} else {response.sendRedirect("/fail.jsp");return;}} else {out.println("<script>alert('两次输入的密码不一样');window.location.href='index.jsp';"+ "</script>");}}else{out.println("<script>alert('用户已存在');window.location.href='index.jsp';"+ "</script>");}}}

相册列表页面:ImageAlbum.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head><base href="<%=basePath%>"><title>My JSP 'ShowImage.jsp' starting page</title><meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0">    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><!--<link rel="stylesheet" type="text/css" href="styles.css">-->
<link href="css/bootstrap.css" rel="stylesheet" type="text/css" media="all" />
<!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
<script src="js/jquery.min.js"></script>
<!-- Custom Theme files -->
<!--theme-style-->
<link href="css/style.css" rel="stylesheet" type="text/css" media="all" />
<!--//theme-style-->
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="keywords" content="Music Club Responsive web template, Bootstrap Web Templates, Flat Web Templates, Andriod Compatible web template,
Smartphone Compatible web template, free webdesigns for Nokia, Samsung, LG, SonyErricsson, Motorola web design" />
<script type="application/x-javascript"> addEventListener("load", function() { setTimeout(hideURLbar, 0); }, false); function hideURLbar(){ window.scrollTo(0,1); } </script>
<script src="js/modernizr.custom.97074.js"></script>
<!--script--><script src="js/jquery.chocolat.js"></script><link rel="stylesheet" href="css/chocolat.css" type="text/css" media="screen" charset="utf-8"><!--light-box-files --><!--      <script type="text/javascript" charset="utf-8">$(function() {$('.gallery a').Chocolat();});</script>--></head><body><!--header-->
<div class="header header-main"><div class="container"><div class="header-top"><div class="logo"><h1><a href="index.html">Music Club</a></h1></div><div class="top-nav"><span class="menu"><img src="data:images/menu.png" alt=""> </span><ul><li ><a href="AllAlbumServlet?username=${user }" class="hvr-sweep-to-bottom color"><i class="glyphicon glyphicon-home"></i>首页  </a> </li><li ><a href="CreateAlbum.jsp?username=${user }" class="hvr-sweep-to-bottom color1"><i class="glyphicon glyphicon-picture"></i>创建相册  </a> </li><li><a href="GetAlbumNamesServlet?username=${user }"  class="hvr-sweep-to-bottom color2"><i class="glyphicon glyphicon-tags"></i>上传图片</a></li><li><a href="index.jsp" class="hvr-sweep-to-bottom color3"><i class="glyphicon glyphicon-calendar"></i>退出 </a></li><li><a  class="hvr-sweep-to-bottom color4"><i class="glyphicon glyphicon-envelope"></i>${user} </a></li><div class="clearfix"> </div></ul><!--script--><script>$("span.menu").click(function(){$(".top-nav ul").slideToggle(500, function(){});});</script>                </div><div class="clearfix"> </div></div></div>
</div>
<!--//header-->
<!--gallery--><div class="gallery"><div class="container"><h3>我的相册|</h3><c:forEach var="album" items="${albumlist }"><ul id="da-thumbs" class="da-thumbs"><li><a href="QueryAlbumPicImageServlet?albumid=${album.albumid }" rel="title" class="b-link-stripe b-animate-go  thickbox"><img src="data:images/a1.jpg" alt="" /><div><h5>${album.albumname }</h5><span>${album.albumdiscript }</span></div></a></li></ul></c:forEach><script type="text/javascript" src="js/jquery.hoverdir.js"></script>    <script type="text/javascript">$(function() {$(' #da-thumbs > li ').each( function() { $(this).hoverdir(); } );});</script></div></div>
<!--//gallery-->
<!--footer-->
<div class="footer"><div class="container"><h2><a href="index.html">Music Club</a></h2><ul><li ><a href="index.html" >Home  </a> </li><li ><a href="album.html" >Albums  </a> </li><li><a href="blog.html"  >Blog</a></li><li><a href="typo.html" >Events </a></li><li><a href="mail.html" >Mail </a></li><div class="clearfix"> </div></ul><p >Copyright &copy; 2015.Company name All rights reserved.More Templates <a href="http://www.cssmoban.com/" target="_blank" title="模板之家">模板之家</a> - Collect from <a href="http://www.cssmoban.com/" title="网页模板" target="_blank">网页模板</a></p></div>
</div>
<!--//footer--></body>
</html>

添加相册页面:CreateAlbum.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"  %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head><base href="<%=basePath%>"><title>My JSP 'ShowImage.jsp' starting page</title><meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0">    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><!--<link rel="stylesheet" type="text/css" href="styles.css">-->
<link href="css/bootstrap.css" rel="stylesheet" type="text/css" media="all" />
<!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
<script src="js/jquery.min.js"></script>
<!-- Custom Theme files -->
<!--theme-style-->
<link href="css/style.css" rel="stylesheet" type="text/css" media="all" />
<!--//theme-style-->
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="keywords" content="Music Club Responsive web template, Bootstrap Web Templates, Flat Web Templates, Andriod Compatible web template,
Smartphone Compatible web template, free webdesigns for Nokia, Samsung, LG, SonyErricsson, Motorola web design" />
<script type="application/x-javascript"> addEventListener("load", function() { setTimeout(hideURLbar, 0); }, false); function hideURLbar(){ window.scrollTo(0,1); } </script>
<script src="js/modernizr.custom.97074.js"></script>
<!--script-->
<script src="js/jquery.chocolat.js"></script><link rel="stylesheet" href="css/chocolat.css" type="text/css" media="screen" charset="utf-8"><!--light-box-files --><script type="text/javascript" charset="utf-8">$(function() {$('.gallery a').Chocolat();});</script></head><body><!--header-->
<div class="header header-main"><div class="container"><div class="header-top"><div class="logo"><h1><a href="index.html">Music Club</a></h1></div><div class="top-nav"><span class="menu"><img src="data:images/menu.png" alt=""> </span><!--script--><script>$("span.menu").click(function(){$(".top-nav ul").slideToggle(500, function(){});});</script>               </div><div class="clearfix"> </div></div></div>
</div>
<!--//header-->
<!--gallery--><center> <form action="AddAlbumServlet" method="post"><input type="hidden" name="username" value="${user }"><font color="green" size="6">相册名:<input type="text" name="albumname"><br>描述:<input type="text" name="albumdiscript"><br><input type="submit" value="提交"><input type="button" value="返回"class="ok" onClick="javascript:history.back(-1);" /></font></form></center>
<!--//gallery-->
<!--footer-->
<div class="footer"><div class="container"><h2><a href="index.html">Music Club</a></h2><ul><li ><a href="index.html" >Home  </a> </li><li ><a href="album.html" >Albums  </a> </li><li><a href="blog.html"  >Blog</a></li><li><a href="typo.html" >Events </a></li><li><a href="mail.html" >Mail </a></li><div class="clearfix"> </div></ul><p >Copyright &copy; 2015.Company name All rights reserved.More Templates <a href="http://www.cssmoban.com/" target="_blank" title="模板之家">模板之家</a> - Collect from <a href="http://www.cssmoban.com/" title="网页模板" target="_blank">网页模板</a></p></div>
</div>
<!--//footer--></body>
</html>

AddAlbumServlet

package com.jkx.web.servlet;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 com.jkx.dao.AlbumDao;
import com.jkx.dao.UserDao;
import com.jkx.po.Album;
import com.jkx.po.User;public class AddAlbumServlet extends HttpServlet {public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {doPost(request, response);}public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {response.setContentType("text/html");response.setCharacterEncoding("utf-8");PrintWriter out =response.getWriter();AlbumDao albumdao=AlbumDao.getIntance();UserDao userdao=UserDao.getIntance();String albumname=request.getParameter("albumname");String albumdiscript=request.getParameter("albumdiscript");String username=request.getParameter("username");User user=userdao.getUserByusername(username);int userid=user.getUserid();Album album=new Album();boolean boo=false;try {album.setAlbumname(albumname);album.setAlbumdiscript(albumdiscript);album.setUserid(userid);boo=albumdao.saveAlbum(album);} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}if(boo==true){List<Album> albumlist=albumdao.getAlbumListbyuseris(userid);request.setAttribute("albumlist", albumlist);request.setAttribute("user", username);request.getRequestDispatcher("/ImageAlbum.jsp").forward(request,response);return ; //结束语句}else{
//      request.getRequestDispatcher("/fail.jsp").forward(request,response);response.sendRedirect("/DBconn/fail.jsp");return ; //����}}}

AllAlbumServlet

package com.jkx.web.servlet;import java.io.IOException;
import java.io.PrintWriter;
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.HttpSession;import com.jkx.dao.AlbumDao;
import com.jkx.dao.UserDao;
import com.jkx.po.Album;
import com.jkx.po.User;public class AllAlbumServlet extends HttpServlet {public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {doPost(request, response);}public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {//对获取的参数有效,但不一定能解决问题,默认只适合post提交response.setCharacterEncoding("utf-8");response.setContentType("text/html");PrintWriter out =response.getWriter();String qq=request.getParameter("username");UserDao userdao=UserDao.getIntance();User user=userdao.getUserByusername(qq);int userid=user.getUserid();AlbumDao albumdao=AlbumDao.getIntance();List<Album> albumlist=albumdao.getAlbumListbyuseris(userid);request.setAttribute("albumlist", albumlist);request.getRequestDispatcher("/ImageAlbum.jsp").forward(request,response);return ; //结束语句}}

ShowImage.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"  %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head><base href="<%=basePath%>"><title>My JSP 'ShowImage.jsp' starting page</title><meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0">    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><!--<link rel="stylesheet" type="text/css" href="styles.css">-->
<link href="css/bootstrap.css" rel="stylesheet" type="text/css" media="all" />
<!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
<script src="js/jquery.min.js"></script>
<!-- Custom Theme files -->
<!--theme-style-->
<link href="css/style.css" rel="stylesheet" type="text/css" media="all" />
<!--//theme-style-->
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="keywords" content="Music Club Responsive web template, Bootstrap Web Templates, Flat Web Templates, Andriod Compatible web template,
Smartphone Compatible web template, free webdesigns for Nokia, Samsung, LG, SonyErricsson, Motorola web design" />
<script type="application/x-javascript"> addEventListener("load", function() { setTimeout(hideURLbar, 0); }, false); function hideURLbar(){ window.scrollTo(0,1); } </script>
<script src="js/modernizr.custom.97074.js"></script>
<!--script-->
<script src="js/jquery.chocolat.js"></script><link rel="stylesheet" href="css/chocolat.css" type="text/css" media="screen" charset="utf-8"><!--light-box-files --><script type="text/javascript" charset="utf-8">$(function() {$('.gallery a').Chocolat();});</script></head><body><!--header-->
<div class="header header-main"><div class="container"><div class="header-top"><div class="logo"><h1><a href="index.html">我的相片</a></h1></div><div class="top-nav"><span class="menu"><img src="data:images/menu.png" alt=""> </span><ul><li ><a href="AllAlbumServlet?username=${user }" class="hvr-sweep-to-bottom color"><i class="glyphicon glyphicon-home"></i>首页  </a> </li><li ><a href="CreateAlbum.jsp?username=${user }" class="hvr-sweep-to-bottom color1"><i class="glyphicon glyphicon-picture"></i>创建相册  </a> </li><li><a href="GetAlbumNamesServlet?username=${user }"  class="hvr-sweep-to-bottom color2"><i class="glyphicon glyphicon-tags"></i>上传图片</a></li><li><a href="index.jsp" class="hvr-sweep-to-bottom color3"><i class="glyphicon glyphicon-calendar"></i>退出 </a></li><li><a  class="hvr-sweep-to-bottom color4"><i class="glyphicon glyphicon-envelope"></i>${user} </a></li><div class="clearfix"> </div></ul><!--script--><script>$("span.menu").click(function(){$(".top-nav ul").slideToggle(500, function(){});});</script>              </div><div class="clearfix"> </div></div></div>
</div>
<!--//header-->
<!--gallery--><div class="gallery"><div class="container"><h3>我的相片|相册:${albumname }</h3><c:forEach var="picture" items="${picturelist }"><section><ul id="da-thumbs" class="da-thumbs"><li><a href="http://localhost:8080/Image/File/${picture.picturename }" rel="title" class="b-link-stripe b-animate-go  thickbox"><img src="http://localhost:8080/Image/File/${picture.picturename }" alt="" /><div><h5>${picture.picturename }</h5><span>aaaaaa</span></div></a></li></ul></section></c:forEach><script type="text/javascript" src="js/jquery.hoverdir.js"></script>    <script type="text/javascript">$(function() {$(' #da-thumbs > li ').each( function() { $(this).hoverdir(); } );});</script></div></div>
<!--//gallery-->
<!--footer-->
<div class="footer"><div class="container"><h2><a href="index.html">Music Club</a></h2><ul><li ><a href="index.html" >Home  </a> </li><li ><a href="album.html" >Albums  </a> </li><li><a href="blog.html"  >Blog</a></li><li><a href="typo.html" >Events </a></li><li><a href="mail.html" >Mail </a></li><div class="clearfix"> </div></ul><p >Copyright &copy; 2015.Company name All rights reserved.More Templates <a href="http://www.cssmoban.com/" target="_blank" title="模板之家">模板之家</a> - Collect from <a href="http://www.cssmoban.com/" title="网页模板" target="_blank">网页模板</a></p></div>
</div>
<!--//footer--></body>
</html>

GetAlbumNamesServlet

package com.jkx.web.servlet;import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;import com.jkx.dao.AlbumDao;
import com.jkx.dao.UserDao;
import com.jkx.po.Album;
import com.jkx.po.User;public class GetAlbumNamesServlet extends HttpServlet {public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {doPost(request, response);}public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {String username=request.getParameter("username");UserDao userdao=UserDao.getIntance();User user=userdao.getUserByusername(username);System.out.println(username);int userid=user.getUserid();AlbumDao albumdao=AlbumDao.getIntance();List<Album> albumNameList=albumdao.getAlbumListbyuseris(userid);request.setAttribute("albumNameList", albumNameList);request.getRequestDispatcher("/UploadPicture.jsp").forward(request,response);}}

UploadPicture.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%String path = request.getContextPath();String basePath = request.getScheme() + "://"+ request.getServerName() + ":" + request.getServerPort()+ path + "/";
%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>"><title>My JSP 'ShowImage.jsp' starting page</title><meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--<link rel="stylesheet" type="text/css" href="styles.css">-->
<link href="css/bootstrap.css" rel="stylesheet" type="text/css"media="all" />
<!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
<script src="js/jquery.min.js"></script>
<!-- Custom Theme files -->
<!--theme-style-->
<link href="css/style.css" rel="stylesheet" type="text/css" media="all" />
<!--//theme-style-->
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="keywords"content="Music Club Responsive web template, Bootstrap Web Templates, Flat Web Templates, Andriod Compatible web template,
Smartphone Compatible web template, free webdesigns for Nokia, Samsung, LG, SonyErricsson, Motorola web design" />
<script type="application/x-javascript"> addEventListener("load", function() { setTimeout(hideURLbar, 0); }, false); function hideURLbar(){ window.scrollTo(0,1); } </script>
<script src="js/modernizr.custom.97074.js"></script>
<!--script-->
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" type="text/css" href="css/normalize.css" />
<link rel="stylesheet" type="text/css" href="css/component.css" />
<link rel="stylesheet" type="text/css" href="css/content.css" />
<script src="js/modernizr.custom.js"></script><script src="js/jquery.chocolat.js"></script>
<link rel="stylesheet" href="css/chocolat.css" type="text/css"media="screen" charset="utf-8">
<!--light-box-files -->
<script type="text/javascript" charset="utf-8">$(function() {$('.gallery a').Chocolat();});$(document).ready(function() {$("#sub").click(function() {if ($("#file").val() == "") {alert("请选择文件" + $("#file").val());} else {$("#form").submit();}});});
</script></head><body><!--header--><div class="header header-main"><div class="container"><div class="header-top"><div class="logo"><h1><a href="index.html">Music Club</a></h1></div><div class="top-nav"><span class="menu"><img src="data:images/menu.png" alt=""></span><!--script--><script>$("span.menu").click(function() {$(".top-nav ul").slideToggle(500, function() {});});</script></div><div class="clearfix"></div></div></div></div><!--//header--><!--gallery--><div class="gallery"><div class="container"><h3>我的图片|</h3><center><form action="UploadPictureServlet?username=${user }" method="post"enctype="multipart/form-data" name="form" οnsubmit="onOK()" id="form"><font color="green" size="6">上传到相册: <select name="albumname"><c:forEach var="albumNameList" items="${albumNameList }"><option name="albumname">${albumNameList.albumname }</option></c:forEach></select><br> <input type="file" name="picturename" id="file"><br><input type="submit" value="添加" id="sub" /> <input type="button" value="返回" class="ok" onClick="javascript:history.back(-1);" /> </font></form></center><script type="text/javascript" src="js/jquery.hoverdir.js"></script><script type="text/javascript">$(function() {$(' #da-thumbs > li ').each(function() {$(this).hoverdir();});});</script></div></div><!--//gallery--><!--footer--><div class="footer"><div class="container"><h2><a href="index.html">Music Club</a></h2><ul><li><a href="index.html">Home </a></li><li><a href="album.html">Albums </a></li><li><a href="blog.html">Blog</a></li><li><a href="typo.html">Events </a></li><li><a href="mail.html">Mail </a></li><div class="clearfix"></div></ul><p>Copyright &copy; 2015.Company name All rights reserved.MoreTemplates <a href="http://www.cssmoban.com/" target="_blank"title="模板之家">模板之家</a> - Collect from <ahref="http://www.cssmoban.com/" title="网页模板" target="_blank">网页模板</a></p></div></div><!--//footer--></body>
</html>

UploadPictureServlet.

package com.jkx.web.servlet;import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.List;
import java.util.Set;
import java.util.UUID;import javax.servlet.Servlet;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;import com.jkx.dao.AlbumDao;
import com.jkx.dao.PictureDao;
import com.jkx.po.Album;
import com.jkx.po.Picture;public class UploadPictureServlet extends HttpServlet {public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {doPost(request, response);}public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {response.setContentType("text/html;charset=utf-8");
//       String filepath=null;int albumid=0;Album album=null;AlbumDao albumdao=AlbumDao.getIntance();PictureDao picturedao=PictureDao.getIntance();Picture pic=new Picture();try {// 创建工厂DiskFileItemFactory factory = new DiskFileItemFactory();File f = new File("/File");if (!f.exists()) {f.mkdir();}// 设置文件的缓存路径factory.setRepository(f);// 创建fileupload组件ServletFileUpload fileUpload = new ServletFileUpload(factory);fileUpload.setHeaderEncoding("gbk");// 解析requestList<FileItem> fileItems =fileUpload.parseRequest(request);PrintWriter write=response.getWriter();//遍历集合//遍历集合for (FileItem fileItem : fileItems) {if (fileItem.isFormField()) {//判断是否为普通字段//获取字段名和字段值String name=fileItem.getFieldName();String value=fileItem.getString("utf-8");album=albumdao.getAlbumName(value);albumid=album.getAlbumid();write.println("上传者:"+value);}else{//上传文件路径String filename=fileItem.getName();write.println("文件来源:"+filename);//截取文件名filename =filename.substring(filename.lastIndexOf("\\")+1);write.println("成功上传的文件:"+filename);//文件名唯一filename = UUID.randomUUID().toString()+"_"+filename;//在服务器创建同名文件String webPath="/File/";String filepath=getServletContext().getRealPath(webPath+filename);//创建文件File file = new File(filepath);file.getParentFile().mkdirs();file.createNewFile();//获得上传文件流InputStream in=fileItem.getInputStream();//获得写入文件流OutputStream out=new FileOutputStream(file);//byte[] buffer =new byte[1024];int len ;while((len = in.read(buffer))>0){out.write(buffer,0,len);}//上传数据到数据库pic.setAlbumid(albumid);pic.setPicturename(filename);System.out.println(filename);pic.setUrl(filepath);boolean boo=false;try {boo=picturedao.savePicture(pic);} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}if(boo==true){List<Picture> p=picturedao.getPictureListbyalbumid(albumid);request.setAttribute("picturelist", p);request.setAttribute("albumname", album.getAlbumname());//根据albumid获取图片request.getRequestDispatcher("ShowImage.jsp").forward(request,response);}else{write.println("<script>alert('上传失败');window.location.href='UploadPicture.jsp';"+ "</script>");}//关闭流in.close();out.close();//删除临时文件fileItem.delete();}}} catch (FileUploadException e) {// TODO Auto-generated catch blocke.printStackTrace();}//     response.setContentType("text/html");
//      response.setCharacterEncoding("utf-8");
//
//      AlbumDao albumdao=AlbumDao.getIntance();
//      String picturename=request.getParameter("picturename");
//      String albumname=request.getParameter("albumname");
//      System.out.println(albumname+"___"+picturename);
//      Album album=albumdao.getAlbumName(albumname);
//      System.out.println(album.getAlbumid());
//      int albumid=album.getAlbumid();
//      // 获取文件路径
//      String realPath = this.getServletContext().getRealPath("/img");
//      System.out.println(realPath);
//      if (picturename != null) {
//
//          File savefile = new File(new File(realPath), picturename);
//          System.out.println("____"+realPath + "\\" + picturename);
//          // 将路径保存
//          PictureDao picturedao=PictureDao.getIntance();
//          Picture pic=new Picture();
//          pic.setAlbumid(albumid);
//          pic.setPicturename(picturename);
//          pic.setUrl(picturename);
//          boolean boo=false;
//          try {
//              boo=picturedao.savePicture(pic);
//          } catch (SQLException e) {
//              // TODO Auto-generated catch block
//              e.printStackTrace();
//          }
//          if (!savefile.getParentFile().exists())
//              savefile.getParentFile().mkdirs();
//          if(boo==true)
//          {
//              request.getRequestDispatcher("ShowImage.jsp").forward(request,
//                      response);
//          }else
//          {
//              out.println("<script>alert('上传失败');window.location.href='UploadPicture.jsp';"
//                      + "</script>");
//          }
//      }
//
//  }}}

效果图:

用户登录到相册查看图片(只是简单的图片展示,并没有实现相册和图片的管理,即删除和)相关推荐

  1. PS:将webp后缀图片最简单最快速的方法另存为png后缀的图片

    PS:将webp后缀图片最简单最快速的方法另存为png后缀的图片 目录 解决问题 解决方法 解决问题 将webp后缀图片最简单最快速的方法另存为png后缀的图片 解决方法 只需要两步,先保存,然后另存 ...

  2. 注册界面php mysql_php:用户登录注册并存入数据库的简单网页示例

    学习了一下php的一些基础知识,就迫不及待的想做一些相关的实例来巩固一下自己的学习,这样才知道这些函数语法在实际上的作用和效果,下面是一个比较简单的实例,实现了用户在注册的时候,把用户的数据存到数据库 ...

  3. microsoftsql新建登录用户登录失败_史上最简单的Spring Security教程(九):自定义用户登录失败页面...

    生活中肯定存在这样的场景,在登录某个网站时,难免会忘记密码,或是验证码输入错误,造成多次尝试.所以,有必要适度的提醒用户,到底是什么原因造成了登录失败,如用户名密码不正确.验证码错误等等.由于 Spr ...

  4. Python实现:某个用户登录后,查看自己拥有所有权限

    权限管理               权限表:             1   订单管理             2   用户管理             3   菜单管理             4 ...

  5. oracle查看有哪些数据库用户登录,oracle如何查看当前有哪些用户连接到数据库,oracle当前有...

    oracle如何查看当前有哪些用户连接到数据库,oracle当前有 可以执行以下语句: select username,serial#, sid from v$session; ---查询用户会话 a ...

  6. Java游戏用户登录注册_java___控制台可以实现简单的登录注册的小游戏平台

    话不多说直接上代码: package pt; import java.util.Scanner; public class Test { public static void main(String[ ...

  7. MySQL不能查看表_mysql root用户登录后无法查看数据库全部表

    首先停掉mysql服务,在/etc/my.cnf中添加 skip-grant-tables,同时可以添加skip-networking选项来禁用网络功能,防止这时其他人通过网络连接到数据库 [mysq ...

  8. vue点击图片后复制图片url_简单漂亮的(图床工具)开源图片上传工具——PicGo...

    介绍 PicGo: 一个用于快速上传图片并获取图片URL链接的工具,由vue-cli-electron-builder构建的简单漂亮的图片上传工具!基于electron-vue开发,支持macOS,W ...

  9. html用户登录页面设计,简洁时尚的用户登录界面设计效果

    这是一款简洁时尚的用户登录界面设计效果.该用户登录效果使用CSS和JS来完成,它使用元素滑动效果来制作登录表单动画,字体效果时尚大方,是非常不错的用户登录界面UI设计效果. 使用方法 HTML结构 该 ...

  10. PHP用户登录功能实现

    PHP用户登录功能实现 设计数据库连接,简单查询,表单提交,直接上代码,大家很容易明白. 我建了一个很简单的数据库,表单包括用户名,ID,密码. 用户登录界面 <html><head ...

最新文章

  1. 安装QT的时候出现PATH_MAX错误
  2. Zookeeper集群配置
  3. SpringBoot集成Mybatis动态多数据源后,MybatisPlus的IPage失效的问题解决方案
  4. 深度学习核心技术精讲100篇(三十)-ClickHouse在字节跳动广告业务中的应用
  5. BZOJ2002 [HNOI2010] 弹飞绵羊
  6. 现代控制理论第八版第二章读书笔记
  7. 混合APP开发框架资料汇总
  8. 交错级数如何判断收敛_从微分方程的级数解到两个特殊方程(1):关于平凡点的解...
  9. windows10使用多套public key pair进行SSH连接github的配置文件
  10. 使用MVC模式制作游戏-教程和简介
  11. 洛谷P3389 【模板】高斯消元法
  12. 2016-2017中国房地产走势大数据报告亮相
  13. openresty连接mysql_在CentOS 6上使用openresty访问mysql
  14. 福彩3D Matlab统计分析,基于ANSYS和遗传算法的3D打印桨叶结构优化设计的制作方法...
  15. RQNOJ 169 最小乘车费用
  16. win7 插入鼠标自动禁用触摸板
  17. python math 数学函数教程
  18. python使用pd.to_numeric()方法将数据转为数字类型int或float
  19. linux can 总线socket接口测试使用
  20. 图片上传到阿里云OSS

热门文章

  1. 通天阁塔机器人图片_世界最大双足机器人亮相世博大阪馆
  2. Ubuntu 中apt update和upgrade 的区别
  3. Elasticsearch安装及SpringBoot整合ElasticSearch
  4. mysql 批量update sql_批量更新sql |批量update sql
  5. 大模型的三大法宝:Finetune, Prompt Engineering, Reward
  6. iPhone 6屏幕:4.7英寸1334*750分辨率,5.5英寸1920*1080,UI适配不困难
  7. 【转载】用Word编辑论文的几个建议
  8. MongoDB技术实践与应用案例征集中
  9. 【NoteBook】吴晓波:《影响商业的50本书》——第三部分:动荡年代与潮汐的方向
  10. 茄子快传数据分析之原理分析及数据清洗