BlogServlet.java
资源名称:cindyblog.rar [点击查看]
上传用户:cdpainuo
上传日期:2022-07-12
资源大小:5257k
文件大小:8k
源码类别:
Jsp/Servlet
开发平台:
Java
- package com.appspot.cindyblog.servlet;
- import java.io.IOException;
- import java.util.HashMap;
- import java.util.Map;
- import javax.persistence.EntityManager;
- import javax.servlet.ServletException;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import com.appspot.cindyblog.common.EMF;
- import com.appspot.cindyblog.common.User;
- import com.appspot.cindyblog.pojo.ArticleBean;
- import com.appspot.cindyblog.pojo.DiscussBean;
- import com.appspot.cindyblog.pojo.LinkBean;
- import com.appspot.cindyblog.pojo.MessageBean;
- import com.appspot.cindyblog.pojo.TypeBean;
- import com.appspot.cindyblog.util.Counter;
- public class BlogServlet extends BaseServlet {
- private static final long serialVersionUID = 5709330878821727568L;
- public void doGet(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- String m = request.getParameter("m");
- if("getIndex".equals(m)){
- this.getIndex(request, response);
- }else if("getSingleArticle".equals(m)){
- getSingleArticle(request, response);
- }else if("getMessageList".equals(m)){
- getMessageList(request, response);
- }else if("getArticleList".equals(m)){
- getArticleList(request, response);
- }else if("getArticle".equals(m)){
- getArticle(request, response);
- }else if("getDiscussList".equals(m)){
- getDiscussList(request, response);
- }else if("login".equals(m)){
- login(request, response);
- }
- }
- public void getIndex(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- response.setContentType("text/html;charset=utf-8");
- EntityManager em = null;
- try{
- bs = getBlogService();
- em = EMF.get().createEntityManager();
- String rz = bs.getObjectList(ArticleBean.class, em, "time", 0, 5);
- String ly = bs.getObjectList(MessageBean.class, em, "time", 0, 5);
- String fl = bs.getType(em);
- String pl = bs.getObjectList(DiscussBean.class, em, "time", 0, 5);
- String link = bs.getObjectList(LinkBean.class, em);
- String str = "var elements = {nodes:{ly:"+ly+",fl:"+fl+",rz:"+rz+",pl:"+pl+",link:"+link+"}}";
- response.getWriter().print(str);
- }catch(Exception e){
- e.printStackTrace();
- }finally{
- bs = null;
- closeEntityManager(em);
- }
- }
- public void getSingleArticle(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- String id = request.getParameter("nowPage");
- int articleId = -1;
- response.setContentType("text/html;charset=utf-8");
- EntityManager em = null;
- try {
- if(id != null && !"".equals(id)){
- articleId = Integer.parseInt(id);
- bs = getBlogService();
- em = EMF.get().createEntityManager();
- String str = bs.getObjectById(ArticleBean.class,new Long(articleId), em);
- String html = "var elements = {nodes:{rz_single:["+str+"]}}";
- response.getWriter().print(html);
- }else{
- response.getWriter().print("false");
- }
- } catch (Exception e) {
- e.printStackTrace();
- }finally{
- bs = null;
- closeEntityManager(em);
- }
- }
- public void getMessageList(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- String page = request.getParameter("nowPage");
- int nowPage = 0;
- EntityManager em = null;
- response.setContentType("text/html;charset=utf-8");
- try {
- if(page != null || !"".equals(page)){
- nowPage = Integer.parseInt(page);
- }
- bs = getBlogService();
- em = EMF.get().createEntityManager();
- String str = bs.getObjectList(MessageBean.class, em, "time", nowPage*20, 20);
- String html = "var elements = {page:{nowPage:"+(nowPage+1)+",allPage:"+((Counter.getMessageCount()-1)/10+1)+"},nodes:{ly_list:"+str+"}}";
- response.getWriter().print(html);
- } catch (NumberFormatException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }finally{
- bs = null;
- closeEntityManager(em);
- }
- }
- public void getArticleList(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- String page = request.getParameter("nowPage");
- String type = request.getParameter("type");
- response.setContentType("text/html;charset=utf-8");
- EntityManager em = null;
- int nowPage = 0;
- try {
- if(page != null || !"".equals(page)){
- nowPage = Integer.parseInt(page);
- }
- em = EMF.get().createEntityManager();
- int allCount = Counter.getArticleCount(type);
- Map<String,String> map = new HashMap<String,String>();
- if(!"All".equals(type)){
- map.put("type", type);
- }
- bs = getBlogService();
- String str = bs.getObjectList(ArticleBean.class, em, "time", map, nowPage*20, 20);
- String html = "var elements = {page:{nowPage:"+(nowPage+1)+",allPage:"+((allCount-1)/20+1)+"},nodes:{rz_list:"+str+"}}";
- response.getWriter().print(html);
- } catch (NumberFormatException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }finally{
- bs = null;
- closeEntityManager(em);
- }
- }
- public void getDiscussList(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- String page = request.getParameter("nowPage");
- String type = request.getParameter("type");
- int nowPage = 0;
- int allPage = 0;
- int article_id = -1;
- EntityManager em = EMF.get().createEntityManager();
- bs = getBlogService();
- try {
- Map<String,String> map = new HashMap<String,String>();
- if(page != null || !"".equals(page)){
- nowPage = Integer.parseInt(page);
- }
- if(type != null && !"".equals(type) && !"All".equals(type)){
- article_id = Integer.parseInt(type);
- allPage = counter.getSingleDisscussCount(article_id);
- map.put("article_id", article_id+"");
- }else if("All".equals("type")){
- allPage = counter.getDisscussCount();
- }
- String str = bs.getObjectList(DiscussBean.class, em, "time", map, nowPage*20, 20);
- String html = "var elements = {page:{nowPage:"+(nowPage+1)+",allPage:"+((allPage-1)/10+1)+"},nodes:{pl_list:"+str+"}}";
- response.setContentType("text/html;charset=utf-8");
- response.getWriter().print(html);
- } catch (NumberFormatException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }finally{
- bs = null;
- closeEntityManager(em);
- }
- }
- public void getArticle(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- bs = getBlogService();
- EntityManager em = EMF.get().createEntityManager();
- try {
- String rz = bs.getObjectList(ArticleBean.class, em, "time", 0, 5);
- String html = "var elements = {nodes:{rz:"+rz+"}}";
- response.setContentType("text/html;charset=utf-8");
- response.getWriter().print(html);
- } catch (RuntimeException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }finally{
- bs = null;
- closeEntityManager(em);
- }
- }
- public void login(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- String username = request.getParameter("username");
- String password = request.getParameter("pwd");
- User user = User.getUser();
- response.setContentType("text/html;charset=utf-8");
- String html = "";
- if(user.getUsername().equals(username) && user.getPassword().equals(password)){
- request.getSession().setAttribute("username", username);
- html = "<script>alert('登录成功!');parent.window.location='/admins/admin.jsp'</script>";
- }else{
- html = "<script>alert('登录失败!');</script>";
- }
- response.getWriter().print(html);
- }
- }