AdminServlet.java
上传用户:cdpainuo
上传日期:2022-07-12
资源大小:5257k
文件大小:5k
源码类别:

Jsp/Servlet

开发平台:

Java

  1. package com.appspot.cindyblog.servlet;
  2. import java.io.IOException;
  3. import java.util.Date;
  4. import javax.persistence.EntityManager;
  5. import javax.servlet.ServletException;
  6. import javax.servlet.http.HttpServletRequest;
  7. import javax.servlet.http.HttpServletResponse;
  8. import com.appspot.cindyblog.common.EMF;
  9. import com.appspot.cindyblog.pojo.ArticleBean;
  10. import com.appspot.cindyblog.pojo.DiscussBean;
  11. import com.appspot.cindyblog.pojo.LinkBean;
  12. import com.appspot.cindyblog.pojo.MessageBean;
  13. import com.appspot.cindyblog.pojo.TypeBean;
  14. import com.google.appengine.api.datastore.Text;
  15. public class AdminServlet extends BaseServlet {
  16. public void doGet(HttpServletRequest request, HttpServletResponse response)
  17. throws ServletException, IOException {
  18. String method = request.getParameter("m");
  19. if("addJournal".equals(method)){
  20. addJournal(request,response);
  21. }else if("addMessage".equals(method)){
  22. addMessage(request,response);
  23. }else if("addType".equals(method)){
  24. addType(request,response);
  25. }else if("addDiscuss".equals(method)){
  26. addDiscuss(request,response);
  27. }else if("addLink".equals(method)){
  28. addLink(request,response);
  29. }
  30. }
  31. public void doPost(HttpServletRequest request, HttpServletResponse response)
  32. throws ServletException, IOException {
  33. this.doGet(request, response);
  34. }
  35. public void addJournal(HttpServletRequest request, HttpServletResponse response)
  36. throws ServletException, IOException {
  37. String title = request.getParameter("title");
  38. String type = request.getParameter("type");
  39. String content = request.getParameter("richEdit0");
  40. EntityManager em = null;
  41. try {
  42. ArticleBean article = new ArticleBean(title,new Date(),type,new Text(content),0);
  43. as = getAdminService();
  44. em = EMF.get().createEntityManager();
  45. as.addArticle(article, em);
  46. response.getWriter().print("add success!");
  47. } catch (RuntimeException e) {
  48. // TODO Auto-generated catch block
  49. e.printStackTrace();
  50. }finally{
  51. as = null;
  52. closeEntityManager(em);
  53. }
  54. }
  55. public void addMessage(HttpServletRequest request, HttpServletResponse response)
  56. throws ServletException, IOException {
  57. String name = request.getParameter("name");
  58. String website = request.getParameter("website");
  59. String email = request.getParameter("email");
  60. String content = request.getParameter("content");
  61. MessageBean message = new MessageBean(email,website,name,new Date(),new Text(content));
  62. EntityManager em = null;
  63. try {
  64. as = getAdminService();
  65. em = EMF.get().createEntityManager();
  66. boolean b =as.addMessage(message, em);
  67. response.setContentType("text/html;charset=utf-8");
  68. String html = "";
  69. if(b){
  70. html = "<script>alert('添加成功!');parent.document.f2.reset();</script>";
  71. }else{
  72. html = "<script>alert('添加失败!');</script>";
  73. }
  74. response.getWriter().print(html);
  75. } catch (RuntimeException e) {
  76. // TODO Auto-generated catch block
  77. e.printStackTrace();
  78. }finally{
  79. as = null;
  80. closeEntityManager(em);
  81. }
  82. }
  83. public void addType(HttpServletRequest request, HttpServletResponse response)
  84. throws ServletException, IOException {
  85. String name = request.getParameter("name");
  86. TypeBean type = new TypeBean(name,0);
  87. EntityManager em = null;
  88. try {
  89. as = getAdminService();
  90. em = EMF.get().createEntityManager();
  91. as.addObject(type, em);
  92. response.getWriter().print("add success!");
  93. } catch (RuntimeException e) {
  94. // TODO Auto-generated catch block
  95. e.printStackTrace();
  96. }finally{
  97. as = null;
  98. closeEntityManager(em);
  99. }
  100. }
  101. public void addLink(HttpServletRequest request, HttpServletResponse response)
  102. throws ServletException, IOException {
  103. String name = request.getParameter("name");
  104. String url = request.getParameter("url");
  105. LinkBean link = new LinkBean(name,url);
  106. EntityManager em = null;
  107. try {
  108. as = getAdminService();
  109. em = EMF.get().createEntityManager();
  110. as.addObject(link, em);
  111. response.getWriter().print("add success!");
  112. } catch (RuntimeException e) {
  113. // TODO Auto-generated catch block
  114. e.printStackTrace();
  115. }finally{
  116. as = null;
  117. closeEntityManager(em);
  118. }
  119. }
  120. public void addDiscuss(HttpServletRequest request, HttpServletResponse response)
  121. throws ServletException, IOException {
  122. String name = request.getParameter("name");
  123. String email = request.getParameter("email");
  124. String content = request.getParameter("richEdit0");
  125. String journal_id = request.getParameter("rid");
  126. String title = request.getParameter("title");
  127. int id = Integer.parseInt(journal_id);
  128. DiscussBean discuss = new DiscussBean(name,title,new Date(),new Text(content),id,email);
  129. EntityManager em = null;
  130. try {
  131. as = getAdminService();
  132. em = EMF.get().createEntityManager();
  133. boolean b = as.addDiscuss(discuss, em);
  134. response.setContentType("text/html;charset=utf-8");
  135. String html = "";
  136. if(b){
  137. html = "<script>alert('添加成功!');parent.document.f1.reset();parent.document.getElementById('rtoIFrame0').contentDocument.body.innerHTML='';</script>";
  138. }else{
  139. html = "<script>alert('添加失败!');</script>";
  140. }
  141. response.getWriter().print(html);
  142. } catch (RuntimeException e) {
  143. // TODO Auto-generated catch block
  144. e.printStackTrace();
  145. }finally{
  146. as = null;
  147. closeEntityManager(em);
  148. }
  149. }
  150. }