JDO 新增網頁:gContactAddServlet.java

package com.emprogria; import java.io.IOException; 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; @SuppressWarnings("serial") public class gContactAddServlet extends HttpServlet { public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException { resp.setContentType("text/html"); resp.setCharacterEncoding("Big5"); UserService userService = UserServiceFactory.getUserService(); User currentUser = userService.getCurrentUser(); if (currentUser != null) { resp.getWriter().println(currentUser.getEmail()); } String msn = req.getParameter("MSN"); String realName = req.getParameter("realName"); String tel = req.getParameter("TEL"); javax.jdo.PersistenceManager pm = com.emprogria.PersistenceManagerFactory .get().getPersistenceManager(); try { Contact myContact = new Contact(msn, realName, tel); pm.makePersistent(myContact); resp.getWriter().println(String.valueOf(myContact.getId())); resp.sendRedirect("/gContactQuery?id=" + String.valueOf(myContact.getId())); } catch (Exception ex) { resp.getWriter().println(ex.getMessage()); } finally { pm.close(); } } }