GAE 程式作品 -- TxtdBase

GAE 雲端程式

簡介

環境安裝

管理介面

資料庫

中文編碼

程式範例

Hello

Guestbook

WikiBox

TxtDBase

訊息

相關網站

參考文獻

最新修改

簡體版

English

專案下載:txtdbase.zip, txtdbase_2.zip

package ccc.gae;
 
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Date;
import java.util.logging.Logger;
import javax.jdo.PersistenceManager;
import javax.servlet.http.*;
import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;
 
import ccc.gae.TxtRecord;
import ccc.gae.PMF;
 
public class TxtOpServlet extends HttpServlet {
    private static final Logger log = Logger.getLogger(TxtOpServlet.class.getName());
 
    public String getParam(HttpServletRequest req, String name) {
        String value = req.getParameter(name);
        if (value == null) value = "";
        return value;
    }
 
    public void doGet(HttpServletRequest req, HttpServletResponse resp)
                throws IOException {
        doPost(req, resp);
    }
 
    public void doPost(HttpServletRequest req, HttpServletResponse resp)
                throws IOException {
        UserService userService = UserServiceFactory.getUserService();
        User user = userService.getCurrentUser();
        PrintWriter writer = resp.getWriter();
 
        PersistenceManager pm = PMF.get().getPersistenceManager();
        String op = getParam(req, "op");
        String id = getParam(req, "id");
        String tag1 = getParam(req, "tag1");
        String path = getParam(req, "path");
        String text = getParam(req, "text");
        String query = getParam(req, "query");
        writer.println("<query>"+req.getQueryString()+"</query>");
        TxtRecord txtRecord = null;
        Date now = new Date();
        try {
        if (id.length() > 0) {
            long rid = Long.parseLong(id); 
            txtRecord = (TxtRecord) pm.getObjectById(rid);
        }
        if (op.equals("insert")) {
            txtRecord = new TxtRecord(user, path, text, now);
               pm.makePersistent(txtRecord);
        } else if (op.equals("delete")) {
            pm.deletePersistent(txtRecord);
        } else if (op.equals("update")) {
               txtRecord.user = user;
               txtRecord.tag1 = tag1;
               txtRecord.path = path;
               txtRecord.setText(text);
               pm.makePersistent(txtRecord);
        }
        else if (op.equals("select")) {
            List<TxtRecord> records = (List<TxtRecord>) pm.newQuery(query).execute();
            writer.println("<list>");
            for (TxtRecord r : records) {
                writer.println("<text id=\""+r.id+"\" path=\""+r.path+"\" tag1=\""+r.tag1+"\" date=\""+r.date+"\">\n");
                writer.println(r.getText()+"\n</text>");
            }
            writer.println("</list>");
        }
        } finally {
            pm.close();
        }
//        resp.sendRedirect("/wikibox.jsp?path="+path);
    }
}

Facebook

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 License