AdminServlet.java
资源名称:cindyblog.rar [点击查看]
上传用户:cdpainuo
上传日期:2022-07-12
资源大小:5257k
文件大小:5k
源码类别:
Jsp/Servlet
开发平台:
Java
- package com.appspot.cindyblog.servlet;
- import java.io.IOException;
- import java.util.Date;
- 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.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.google.appengine.api.datastore.Text;
- public class AdminServlet extends BaseServlet {
- public void doGet(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- String method = request.getParameter("m");
- if("addJournal".equals(method)){
- addJournal(request,response);
- }else if("addMessage".equals(method)){
- addMessage(request,response);
- }else if("addType".equals(method)){
- addType(request,response);
- }else if("addDiscuss".equals(method)){
- addDiscuss(request,response);
- }else if("addLink".equals(method)){
- addLink(request,response);
- }
- }
- public void doPost(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- this.doGet(request, response);
- }
- public void addJournal(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- String title = request.getParameter("title");
- String type = request.getParameter("type");
- String content = request.getParameter("richEdit0");
- EntityManager em = null;
- try {
- ArticleBean article = new ArticleBean(title,new Date(),type,new Text(content),0);
- as = getAdminService();
- em = EMF.get().createEntityManager();
- as.addArticle(article, em);
- response.getWriter().print("add success!");
- } catch (RuntimeException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }finally{
- as = null;
- closeEntityManager(em);
- }
- }
- public void addMessage(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- String name = request.getParameter("name");
- String website = request.getParameter("website");
- String email = request.getParameter("email");
- String content = request.getParameter("content");
- MessageBean message = new MessageBean(email,website,name,new Date(),new Text(content));
- EntityManager em = null;
- try {
- as = getAdminService();
- em = EMF.get().createEntityManager();
- boolean b =as.addMessage(message, em);
- response.setContentType("text/html;charset=utf-8");
- String html = "";
- if(b){
- html = "<script>alert('添加成功!');parent.document.f2.reset();</script>";
- }else{
- html = "<script>alert('添加失败!');</script>";
- }
- response.getWriter().print(html);
- } catch (RuntimeException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }finally{
- as = null;
- closeEntityManager(em);
- }
- }
- public void addType(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- String name = request.getParameter("name");
- TypeBean type = new TypeBean(name,0);
- EntityManager em = null;
- try {
- as = getAdminService();
- em = EMF.get().createEntityManager();
- as.addObject(type, em);
- response.getWriter().print("add success!");
- } catch (RuntimeException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }finally{
- as = null;
- closeEntityManager(em);
- }
- }
- public void addLink(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- String name = request.getParameter("name");
- String url = request.getParameter("url");
- LinkBean link = new LinkBean(name,url);
- EntityManager em = null;
- try {
- as = getAdminService();
- em = EMF.get().createEntityManager();
- as.addObject(link, em);
- response.getWriter().print("add success!");
- } catch (RuntimeException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }finally{
- as = null;
- closeEntityManager(em);
- }
- }
- public void addDiscuss(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- String name = request.getParameter("name");
- String email = request.getParameter("email");
- String content = request.getParameter("richEdit0");
- String journal_id = request.getParameter("rid");
- String title = request.getParameter("title");
- int id = Integer.parseInt(journal_id);
- DiscussBean discuss = new DiscussBean(name,title,new Date(),new Text(content),id,email);
- EntityManager em = null;
- try {
- as = getAdminService();
- em = EMF.get().createEntityManager();
- boolean b = as.addDiscuss(discuss, em);
- response.setContentType("text/html;charset=utf-8");
- String html = "";
- if(b){
- html = "<script>alert('添加成功!');parent.document.f1.reset();parent.document.getElementById('rtoIFrame0').contentDocument.body.innerHTML='';</script>";
- }else{
- html = "<script>alert('添加失败!');</script>";
- }
- response.getWriter().print(html);
- } catch (RuntimeException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }finally{
- as = null;
- closeEntityManager(em);
- }
- }
- }