基于javaweb+mysql的jsp+servlet个人博客系统(java+jsp+servlet+mysql)
运行环境
Java≥8、MySQL≥5.7、Tomcat≥8
开发工具
eclipse/idea/myeclipse/sts等均可配置运行
适用
课程设计,大作业,毕业设计,项目练习,学习演示等
功能说明
技术框架
JSP Servlet MySQL
基于javaweb+mysql的JSP+Servlet个人博客系统(java+jsp+servlet+mysql)
/**
* 游客浏览博客
* @param request
* @param response
* @throws IOException
* @throws ServletException
*/
public void visitorBlog(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException{
String userId = request.getParameter("userid");
int userid = 0;
if(userId!=null){
userid = Integer.parseInt(userId);
}
User user = userDao.getUser(userid);
if(user!=null){
HttpSession session = request.getSession();
session.setAttribute("VISITOR", user);
}
request.getRequestDispatcher("UserArticleServlet?method=list").forward(request, response);
}
}
/**
* Servlet implementation class CategroyServlet
*/
public class CategroyServlet extends HttpServlet {
}
request.getRequestDispatcher("UserArticleServlet?method=list").forward(request, response);
}
}
/**
* Servlet implementation class CategroyServlet
*/
public class CategroyServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private CategroyDao categroyDao = new CategroyDao();
/**
* @see HttpServlet#HttpServlet()
*/
public CategroyServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String method = request.getParameter("method");
if ("list".equals(method)) {
listCategroy(request, response);
}
session.removeAttribute("ARTICLES");//移除上一次查询的博文列表
if(articles!=null&&articles.size()>0){
session.setAttribute("ARTICLES", articles);
}
request.getRequestDispatcher("CategroyServlet?method=list").forward(request, response);
}
public void addArticle(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
String cateid = request.getParameter("cateId");
int cateId = 0;
if(cateid!=null){
cateId = Integer.parseInt(cateid);
}
String articleTitle = request.getParameter("title");
String articleContent = request.getParameter("content");
HttpSession session = request.getSession();
User user = (User)session.getAttribute("USER");
Categroy categroy = new Categroy(cateId,user.getBlog(),null);
Article article = new Article(0,user.getBlog(),categroy,articleTitle,articleContent,new Date(),0,0);
int result = articleDao.addArticle(article);
if(result>0){
request.getRequestDispatcher("UserArticleServlet?method=list&query=true").forward(request, response);
}else{
request.getRequestDispatcher("article_add.jsp").forward(request, response);
}
}
public void listUserPageArticle(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
String pno = request.getParameter("pageNo");
int pageNo = 0;
if(pno!=null){
pageNo = Integer.parseInt(pno);
}
HttpSession session = request.getSession();
User user = (User)session.getAttribute("USER");
PageUtil page = null;
if(user!=null){
page = articleDao.listPageArticle(user.getBlog(), pageNo, 2);
page.setPageNo(pageNo);
request.setAttribute("page", page);
}
request.getRequestDispatcher("article_manager.jsp").forward(request, response);
}
public void getArticle(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
String aid = request.getParameter("aid");
@Override
public void init(ServletConfig config) throws ServletException {
// TODO Auto-generated method stub
super.init(config);
String projectName = config.getInitParameter("projectName");
System.out.println("正在访问"+projectName);
ServletContext servletContext = config.getServletContext();
String company = servletContext.getInitParameter("company");
System.out.println(company);
}
}
/**
* Servlet implementation class UploadUserServlet
*/
@MultipartConfig
public class UploadUserServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public UploadUserServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
HttpSession session = request.getSession();
User user = (User)session.getAttribute("USER");
Categroy categroy = new Categroy(cateId,user.getBlog(),null);
Article article = new Article(0,user.getBlog(),categroy,articleTitle,articleContent,new Date(),0,0);
int result = articleDao.addArticle(article);
if(result>0){
request.getRequestDispatcher("UserArticleServlet?method=list&query=true").forward(request, response);
}else{
request.getRequestDispatcher("article_add.jsp").forward(request, response);
}
}
public void listUserPageArticle(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
String pno = request.getParameter("pageNo");
int pageNo = 0;
if(pno!=null){
pageNo = Integer.parseInt(pno);
}
HttpSession session = request.getSession();
User user = (User)session.getAttribute("USER");
PageUtil page = null;
if(user!=null){
page = articleDao.listPageArticle(user.getBlog(), pageNo, 2);
page.setPageNo(pageNo);
request.setAttribute("page", page);
}
request.getRequestDispatcher("article_manager.jsp").forward(request, response);
}
public void getArticle(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
String aid = request.getParameter("aid");
int articleId = 0;
if(aid!=null){
articleId = Integer.parseInt(aid);
}
Article article = articleDao.getArticle(articleId);
}
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
public void reg(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//设置request对象字符编码
//request.setCharacterEncoding("UTF-8");
String uname = request.getParameter("uname");
// uname = new String(uname.getBytes("ISO-8859-1"),"UTF-8");
String upass = request.getParameter("pwd");
String sex = request.getParameter("sex");
String email = request.getParameter("email");
String info = request.getParameter("content");
String fileName = request.getParameter("fileName");
// String[] loves = request.getParameterValues("love");
// System.out.println(uname);
// for(String love : loves){
// System.out.println(love);
// }
//对接收的表单数据进行封装
User user = new User(0,null,uname,upass,sex,email,info,fileName);
//调用Dao类方法处理数据
int result = userDao.insertUser(user);
request.setAttribute("RESULT", result);
request.getRequestDispatcher("/loginMessage.jsp").forward(request, response);
// response.sendRedirect("http://www.sina.com");
// if(result>0){
// //请求转发
// //request.getRequestDispatcher("/loginMessage.jsp").forward(request, response);
// //重定向
// response.sendRedirect("/myblog/loginMessage.jsp");
// }else{
// request.getRequestDispatcher("login.jsp").forward(request, response);
// }
// response.setCharacterEncoding("UTF-8");
// response.setContentType("text/html;charset=UTF-8");
// PrintWriter out = response.getWriter();
}
/**
* Servlet Filter implementation class LoginFilter
*/
public class LoginFilter implements Filter {
/**
* Default constructor.
*/
public LoginFilter() {
// TODO Auto-generated constructor stub
}
/**
* @see Filter#destroy()
*/
public void destroy() {
// TODO Auto-generated method stub
}
/**
* @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
*/
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
User user = (User)((HttpServletRequest)request).getSession().getAttribute("USER");
if(user==null){
((HttpServletResponse)response).sendRedirect("../login.jsp");
return;
}
chain.doFilter(request, response);
}
/**
* @see Filter#init(FilterConfig)
*/
public void init(FilterConfig fConfig) throws ServletException {
// TODO Auto-generated method stub
request.setAttribute("page", page);
}
request.getRequestDispatcher("article_manager.jsp").forward(request, response);
}
public void getArticle(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
String aid = request.getParameter("aid");
int articleId = 0;
if(aid!=null){
articleId = Integer.parseInt(aid);
}
Article article = articleDao.getArticle(articleId);
HttpSession session = request.getSession();
if(article!=null){
session.setAttribute("ARTICLE", article);
}
request.getRequestDispatcher("article_modify.jsp").forward(request, response);
}
public void updateArticle(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
String aid = request.getParameter("aid");
int articleId = 0;
if(aid!=null){
articleId = Integer.parseInt(aid);
}
String cateid = request.getParameter("cateId");
int cateId = 0;
if(cateid!=null){
cateId = Integer.parseInt(cateid);
}
String articleTitle = request.getParameter("title");
String articleContent = request.getParameter("content");
HttpSession session = request.getSession();
User user = (User)session.getAttribute("USER");
Categroy categroy = new Categroy(cateId,user.getBlog(),null);
Article article = new Article(articleId,user.getBlog(),categroy,articleTitle,articleContent,new Date(),0,0);
int result = articleDao.updateArticle(article);
if(result>0){
request.getRequestDispatcher("UserArticleServlet?method=showPage&pageNo=1").forward(request, response);
}else{
request.getRequestDispatcher("article_modify.jsp").forward(request, response);
}
}
throws ServletException, IOException {
// 取得客户端提交的文件信息
Part part = request.getPart("file");
// 取得请求头信息
String headerInfo = part.getHeader("content-disposition");
// 从头信息字符串中截取文件名称
String fileName = headerInfo.substring(headerInfo.lastIndexOf("=") + 2, headerInfo.length() - 1);
// 得到服务器端存放上传图片的物理地址
String fileSavingFolder = this.getServletContext().getRealPath("/images");
// 得到服务器端图片的物理地址
String fileSavingPath = fileSavingFolder + File.separator + fileName;
// 创建upload文件夹
File f = new File(fileSavingFolder + File.separator);
if (!f.exists()) {
f.mkdirs();
}
part.write(fileSavingPath);
String blogTitle = request.getParameter("blogTitle");
Blog blog = new Blog(0, blogTitle, fileName);
int blogId = blogDao.insertBlog(blog);
if (blogId > 0) {
response.sendRedirect("UserInfoServlet?method=updateBlogId&blogId=" + blogId);
} else {
response.sendRedirect("user/blog.jsp");
}
}
}
/**
* Servlet implementation class HelloServlet
*/
@WebServlet(name="HelloServlet",urlPatterns={"/HelloServlet"})
public class HelloServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
Article article = new Article(0,user.getBlog(),categroy,articleTitle,articleContent,new Date(),0,0);
int result = articleDao.addArticle(article);
if(result>0){
request.getRequestDispatcher("UserArticleServlet?method=list&query=true").forward(request, response);
}else{
request.getRequestDispatcher("article_add.jsp").forward(request, response);
}
}
public void listUserPageArticle(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
String pno = request.getParameter("pageNo");
int pageNo = 0;
if(pno!=null){
pageNo = Integer.parseInt(pno);
}
HttpSession session = request.getSession();
User user = (User)session.getAttribute("USER");
PageUtil page = null;
if(user!=null){
page = articleDao.listPageArticle(user.getBlog(), pageNo, 2);
page.setPageNo(pageNo);
request.setAttribute("page", page);
}
request.getRequestDispatcher("article_manager.jsp").forward(request, response);
}
public void getArticle(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
String aid = request.getParameter("aid");
int articleId = 0;
if(aid!=null){
articleId = Integer.parseInt(aid);
}
Article article = articleDao.getArticle(articleId);
HttpSession session = request.getSession();
if(article!=null){
session.setAttribute("ARTICLE", article);
}
request.getRequestDispatcher("article_modify.jsp").forward(request, response);
}
public void updateArticle(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
String aid = request.getParameter("aid");
int articleId = 0;
if(aid!=null){
articleId = Integer.parseInt(aid);
}
String cateid = request.getParameter("cateId");
int cateId = 0;
/**
* Servlet implementation class HelloServlet
*/
@WebServlet(name="HelloServlet",urlPatterns={"/HelloServlet"})
public class HelloServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public HelloServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @throws IOException
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//System.out.println("Hello World");
response.setContentType("text/html;charset=UTF-8");
response.setCharacterEncoding("UTF-8");
//输出流对象
PrintWriter out = response.getWriter();
// PrintWriter out = null;
// String[] s = {"hello","world"};
// out.println(s[2]);
out.println("你好,世界");
out.flush();//清空缓存数据
out.close();//关闭流
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
@Override
public void init(ServletConfig config) throws ServletException {
// TODO Auto-generated method stub
super.init(config);
/**
* Servlet implementation class CategroyServlet
*/
public class CategroyServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private CategroyDao categroyDao = new CategroyDao();
/**
* @see HttpServlet#HttpServlet()
*/
public CategroyServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String method = request.getParameter("method");
if ("list".equals(method)) {
listCategroy(request, response);
}
if ("add".equals(method)) {
addCategroy(request, response);
}
if("showPage".equals(method)){
listUserPageCategroy(request,response);
}
if("del".equals(method)){
delCategroy(request,response);
}
}
}
/**
* Servlet implementation class UserInfoServlet
*/
public class UserInfoServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private UserDao userDao = new UserDao();
/**
* @see HttpServlet#HttpServlet()
*/
public UserInfoServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String method = request.getParameter("method");
if("reg".equals(method)){
reg(request,response);
}
if("login".equals(method)){
login(request,response);
}
if("logout".equals(method)){
logout(request,response);
}
if("updateBlogId".equals(method)){
updateBlogId(request,response);
}
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public UserServlet() {
super();
System.out.println("调用了构造方法实例化");
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("调用了服务方法");
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
@Override
public void init(ServletConfig config) throws ServletException {
// TODO Auto-generated method stub
super.init(config);//不能删除
System.out.println("调用了初始化方法");
String projectName = config.getInitParameter("projectName");
System.out.println("正在访问"+projectName);
ServletContext servletContext = config.getServletContext();
String company = servletContext.getInitParameter("company");
System.out.println(company);
}
@Override
public void destroy() {
// TODO Auto-generated method stub
super.destroy();
System.out.println("调用了销毁方法!");
}
}
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//取得客户端提交的文件信息
Part part = request.getPart("file");
//取得请求头信息
String headerInfo = part.getHeader("content-disposition");
//从头信息字符串中截取文件名称
String fileName = headerInfo.substring(headerInfo.lastIndexOf("=")+2,headerInfo.length()-1);
//得到服务器端存放上传图片的物理地址
String fileSavingFolder = this.getServletContext().getRealPath("/upload");
//得到服务器端图片的物理地址
String fileSavingPath = fileSavingFolder+File.separator+fileName;
//创建upload文件夹
File f = new File(fileSavingFolder+File.separator);
if(!f.exists()){
f.mkdirs();
}
part.write(fileSavingPath);
request.getRequestDispatcher("UserInfoServlet?fileName="+fileName).forward(request, response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
/**
* Servlet implementation class UserServlet
*/
public class UserServlet extends HttpServlet {
request.getRequestDispatcher("article_manager.jsp").forward(request, response);
}
public void getArticle(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
String aid = request.getParameter("aid");
int articleId = 0;
if(aid!=null){
articleId = Integer.parseInt(aid);
}
Article article = articleDao.getArticle(articleId);
HttpSession session = request.getSession();
if(article!=null){
session.setAttribute("ARTICLE", article);
}
request.getRequestDispatcher("article_modify.jsp").forward(request, response);
}
public void updateArticle(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
String aid = request.getParameter("aid");
int articleId = 0;
if(aid!=null){
articleId = Integer.parseInt(aid);
}
String cateid = request.getParameter("cateId");
int cateId = 0;
if(cateid!=null){
cateId = Integer.parseInt(cateid);
}
String articleTitle = request.getParameter("title");
String articleContent = request.getParameter("content");
HttpSession session = request.getSession();
User user = (User)session.getAttribute("USER");
Categroy categroy = new Categroy(cateId,user.getBlog(),null);
Article article = new Article(articleId,user.getBlog(),categroy,articleTitle,articleContent,new Date(),0,0);
int result = articleDao.updateArticle(article);
if(result>0){
request.getRequestDispatcher("UserArticleServlet?method=showPage&pageNo=1").forward(request, response);
}else{
*/
public class UserServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public UserServlet() {
super();
System.out.println("调用了构造方法实例化");
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("调用了服务方法");
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
@Override
public void init(ServletConfig config) throws ServletException {
// TODO Auto-generated method stub
super.init(config);//不能删除
System.out.println("调用了初始化方法");
String projectName = config.getInitParameter("projectName");
System.out.println("正在访问"+projectName);
ServletContext servletContext = config.getServletContext();
String company = servletContext.getInitParameter("company");
System.out.println(company);
}
@Override
public void destroy() {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//System.out.println("Hello World");
response.setContentType("text/html;charset=UTF-8");
response.setCharacterEncoding("UTF-8");
//输出流对象
PrintWriter out = response.getWriter();
// PrintWriter out = null;
// String[] s = {"hello","world"};
// out.println(s[2]);
out.println("你好,世界");
out.flush();//清空缓存数据
out.close();//关闭流
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
@Override
public void init(ServletConfig config) throws ServletException {
// TODO Auto-generated method stub
super.init(config);
String projectName = config.getInitParameter("projectName");
System.out.println("正在访问"+projectName);
ServletContext servletContext = config.getServletContext();
String company = servletContext.getInitParameter("company");
System.out.println(company);
}
}
/**
* Servlet implementation class UploadUserServlet
*/