基于javaweb+mysql的jsp+servlet个人博客系统(java+jsp+servlet+mysql)
私信源码获取及调试交流
运行环境
Java≥8、MySQL≥5.7、Tomcat≥8
开发工具
eclipse/idea/myeclipse/sts等均可配置运行
适用
课程设计,大作业,毕业设计,项目练习,学习演示等
功能说明
技术框架
JSP Servlet MySQL
基于javaweb的JSP+Servlet个人博客系统(java+jsp+servlet+mysql)
String q = request.getParameter("query");//取得查询标志
boolean query = false;
if(q!=null){
query = Boolean.parseBoolean(q);//转换字符串为布尔类型
}
HttpSession session = request.getSession();
User user = (User)session.getAttribute("USER");//从session中取出登录成功后的用户对象
List<Article> articles = null;
if(user!=null){
articles = articleDao.listArticle(user.getBlog(),query);
}
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");
}
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");
int articleId = 0;
if(aid!=null){
}
if("showPage".equals(method)){
listUserPageArticle(request,response);
}
if("get".equals(method)){
getArticle(request,response);
}
if("update".equals(method)){
updateArticle(request,response);
}
if("del".equals(method)){
delArticle(request,response);
}
if("categroy".equals(method)){
queryCategroyArticle(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);
}
public void listUserArticle(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
String q = request.getParameter("query");//取得查询标志
boolean query = false;
if(q!=null){
query = Boolean.parseBoolean(q);//转换字符串为布尔类型
}
HttpSession session = request.getSession();
User user = (User)session.getAttribute("USER");//从session中取出登录成功后的用户对象
List<Article> articles = null;
if(user!=null){
articles = articleDao.listArticle(user.getBlog(),query);
}
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)
int result = userDao.updateBlogId(user);
if(result>0){
response.sendRedirect("BlogServlet?method=query");
}else{
response.sendRedirect("login.jsp");
}
}
}
/**
* 游客浏览博客
* @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);
}
}
response.sendRedirect("CategroyServlet?method=showPage&pageNo=1");
} else {
response.sendRedirect("categroy_add.jsp");
}
}
/**
* 调用分页方法
* @param request
* @param response
* @throws IOException
* @throws ServletException
*/
public void listUserPageCategroy(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 = categroyDao.listPageCategroy(user.getBlog(), pageNo, 3);
page.setPageNo(pageNo);
request.setAttribute("page", page);
}
request.getRequestDispatcher("categroy_manager.jsp").forward(request, response);
}
public void delCategroy(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
String cid = request.getParameter("cid");
int categroyId = 0;
if(cid!=null){
categroyId = Integer.parseInt(cid);
}
int result = categroyDao.delCategroy(categroyId);
if(result>0){
searchCategroy(request,response);
request.getRequestDispatcher("CategroyServlet?method=showPage&pageNo=1").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");
int articleId = 0;
if(aid!=null){
articleId = Integer.parseInt(aid);
/**
* 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)
*/
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");
/**
* 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#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)
*/
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);
*/
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("调用了销毁方法!");
}
protected void addBlog(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("/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");
}
}
}
// PrintWriter out = response.getWriter();
// if(result>0){
// out.println("注册成功!");
// }else{
// out.println("注册失败!");
// }
}
public void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//request.setCharacterEncoding("UTF-8");
String username = request.getParameter("uname");
String pwd = request.getParameter("pwd");
User user = new User();
user.setUserName(username);
user.setUserpass(pwd);
User userInfo = userDao.searchUser(user);
//看当前session是否存在,如果存在返回已存在的session,如果不存在返回新的session对象
HttpSession session = request.getSession();
// session.setMaxInactiveInterval(60*60);
//看当前session是否存在,如果存在返回已存在的session,如果不存在返回null
// HttpSession session = request.getSession(false);
Integer lineNum = (Integer)this.getServletContext().getAttribute("LINE_NUM");
if(lineNum==null){
lineNum = 0;
}
if(userInfo!=null){
lineNum++;
this.getServletContext().setAttribute("LINE_NUM", lineNum);
//将用户信息存放到session中
session.setAttribute("USER", userInfo);
if(userInfo.getBlog()!=null&&userInfo.getBlog().getBlogId()>0){
response.sendRedirect("user/UserArticleServlet?method=list&query=true");
}else{
response.sendRedirect("user/blog.jsp");
}
}else{
response.sendRedirect("loginMessage.jsp");
}
}
public void logout(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
HttpSession session = request.getSession();
// session.invalidate();//销毁掉session
//清除session对象中的某个属性值
session.removeAttribute("USER");
response.sendRedirect("login.jsp");
}
}else{
response.sendRedirect("loginMessage.jsp");
}
}
public void logout(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
HttpSession session = request.getSession();
// session.invalidate();//销毁掉session
//清除session对象中的某个属性值
session.removeAttribute("USER");
response.sendRedirect("login.jsp");
}
public void updateBlogId(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
HttpSession session = request.getSession();
User user = (User)session.getAttribute("USER");
String blogid = request.getParameter("blogId");
int blogId = 0;
if(blogid!=null){
blogId = Integer.parseInt(blogid);
Blog blog = new Blog();
blog.setBlogId(blogId);
user.setBlog(blog);
int result = userDao.updateBlogId(user);
if(result>0){
response.sendRedirect("BlogServlet?method=query");
}else{
response.sendRedirect("login.jsp");
}
}
}
/**
* 游客浏览博客
* @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){
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String method = request.getParameter("method");
if ("visit".equals(method)) {
listVisitorArticle(request, response);
}
if("categroy".equals(method)){
queryCategroyArticle(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);
}
public void listVisitorArticle(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);
String q = request.getParameter("query");// 取得查询标志
boolean query = false;
if (q != null) {
query = Boolean.parseBoolean(q);// 转换字符串为布尔类型
}
if (user != null) {
HttpSession session = request.getSession();
}
}
/**
* 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"};
throws IOException, ServletException {
String userId = request.getParameter("userid");
int userid = 0;
if (userId != null) {
userid = Integer.parseInt(userId);
}
User user = userDao.getUser(userid);
String q = request.getParameter("query");// 取得查询标志
boolean query = false;
if (q != null) {
query = Boolean.parseBoolean(q);// 转换字符串为布尔类型
}
if (user != null) {
HttpSession session = request.getSession();
session.setAttribute("VISITOR",user);// 从session中取出登录成功后的用户对象
List<Article> articles = null;
List categroys = null;
if (user != null) {
articles = articleDao.listArticle(user.getBlog(), query);
categroys = categroyDao.searchCategroy(user.getBlog().getBlogId());
}
session.removeAttribute("ARTICLES");// 移除上一次查询的博文列表
if (articles != null && articles.size() > 0) {
session.setAttribute("ARTICLES", articles);
}
session.removeAttribute("CATEGROYS");
if (categroys != null && categroys.size() > 0) {
session.setAttribute("CATEGROYS", categroys);
}
}
request.getRequestDispatcher("index.jsp").forward(request, response);
}
/**
* 分类查询
* @param request
* @param response
* @throws IOException
* @throws ServletException
*/
}
/**
* Servlet implementation class BlogServlet
*/
@MultipartConfig
public class BlogServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private BlogDao blogDao = new BlogDao();
/**
* @see HttpServlet#HttpServlet()
*/
public BlogServlet() {
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 ("query".equals(method)) {
queryAllBlog(request, response);
}
if ("add".equals(method)) {
addBlog(request, response);
}
}
// TODO Auto-generated method stub
doGet(request, response);
}
public void listUserArticle(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
String q = request.getParameter("query");//取得查询标志
boolean query = false;
if(q!=null){
query = Boolean.parseBoolean(q);//转换字符串为布尔类型
}
HttpSession session = request.getSession();
User user = (User)session.getAttribute("USER");//从session中取出登录成功后的用户对象
List<Article> articles = null;
if(user!=null){
articles = articleDao.listArticle(user.getBlog(),query);
}
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);
}
}