Saturday, December 4, 2010
ACM Apply on Google App Engine
Thursday, June 24, 2010
Local Datastore Dashboard for Google App Engine
Tuesday, June 22, 2010
Using Spring Framework and Google App Engine for a Web Application.
Step by Step Procedure for the Hello World Application:
- Install the Google Plugin
- Create a new Web application and untick GWT (Google Web Toolkit)
- Download the latest Spring Release
- Add the Jars to the folder WEB-INF/lib
The following JAR files should be present in the folder:
Make the Following Changes to the web.xml file
<?xml version="1.0" encoding="utf-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" version="2.5">
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet><servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/a/*</url-pattern>
</servlet-mapping>
</web-app>
Create a file dispatcher-servler.xml in the WEB-INF Directory
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="ipathshala.controllers" />
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/views/" p:suffix=".jsp" >
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
</bean>
</beans>
HelloController.java
package ipathshala.controllers;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;
@Controller
@RequestMapping("/hello")
public class HelloController {
private UserService userService = UserServiceFactory.getUserService();
@RequestMapping(method = RequestMethod.GET)
public String hello(HttpServletRequest request, ModelMap model)
{
String url = request.getRequestURI();
String message;
if(request.getUserPrincipal() != null)
{
message = new StringBuilder()
.append("Hello, ")
.append(request.getUserPrincipal().getName())
.append("! You can <a href=\"")
.append(userService.createLogoutURL(url))
.append("\">Sign Out</a>.").toString();
}else{
message = new StringBuilder()
.append("Please ")
.append("<a href=\"")
.append(userService.createLoginURL(url))
.append("\">Sign In</a>.").toString();
}
model.addAttribute("message", message);
return "hello/hello";
}
}
This line above @RequestMapping("/hello") maps all the requests /a/hello to this controller after being examined by the DispatcherServlet.Create a Folder views under WEB-INF and two Sub folders hello and common.
hello/hello.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page isELIgnored="false" %>
<%@ include file="/WEB-INF/views/common/includes.jsp" %>
<p>${message}</p>
<%@ page session="false"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
You should now be able to click “Debug” and run the application by visiting “localhost:8888/a/hello”.
References:
- Adent Lord’s Spring 3.0 GAE article: http://www.ardentlord.com/apps/blog/show/829881-spring-3-0-on-google-app-engine
- GAE Documentation: http://code.google.com/appengine/docs/java/gettingstarted/
- Spring 3.0 Documentation: http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/
- Spring MVC 2.5 Tutorial: http://static.springsource.org/docs/Spring-MVC-step-by-step/
Friday, June 11, 2010
Session Management using Google User on Google App Engine
HttpSession s = request.getSession();
if (s.getAttribute("logged")==null){
s.setAttribute("from",request.getRequestURI());
response.sendRedirect("/login");
}
else {
s.setAttribute("from",request.getRequestURI());
%>Logout<%
//UserService userSer = (UserService)s.getAttribute("userSer");
}
Login.java
public class Login extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException {
UserService userSer=UserServiceFactory.getUserService();
User u=userSer.getCurrentUser();
HttpSession s=request.getSession();
String from=(String)s.getAttribute("from");
s.setAttribute("logged","yes");
s.setAttribute("userSer", userSer);
response.sendRedirect(userSer.createLoginURL(from));
}
}
Logout.java
public class Logout extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException {
HttpSession s = request.getSession();
String from=(String)s.getAttribute("from");
UserService us=(UserService)s.getAttribute("userSer");
s.invalidate();
response.sendRedirect(us.createLogoutURL("/index.html"));
}
}
Monday, May 31, 2010
Deleting All entries in the Google Datastore locally
WEB-INF/appengine-generated/ directory. (It is not uploaded with your application.)http://code.google.com/intl/fr/appengine/docs/java/tools/devserver.html#Using_the_Datastore
To delete all entries, stop the server, delete this file and then Start the server again.
Friday, May 28, 2010
Searching the Student Objects stored in the Datastore by their Name
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.jdo.PersistenceManager;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.*;
@SuppressWarnings("serial")
public class NameSearch extends HttpServlet {
public void doPost(HttpServletRequest req, HttpServletResponse resp)
throws IOException {
String token = req.getParameter("search");
PersistenceManager pm=PMF.get().getPersistenceManager();
String query= "select from " + Student.class.getName();
List
ArrayList results = new ArrayList();
for (Student a:students)
{
// Checking whether token appears in the name whether lower or upper
if (a!=null && token!=null)
if(a.getName().toLowerCase().indexOf(token.toLowerCase()) != -1)
{
// Adding the reference to the object into the ArrayList
System.out.println(a.getName());
results.add(a);
}
}
req.setAttribute("result", results);
//Sending the results data via RequestDispatcher to your JSP Page.
RequestDispatcher r=req.getRequestDispatcher("SearchAS.jsp");
try
{
r.forward(req, resp);
} catch (ServletException e)
{
e.printStackTrace();
}
}
}
Tuesday, May 25, 2010
Importing data from Google Spreadsheets from Particular columns
import com.google.gdata.client.spreadsheet.*;
import com.google.gdata.data.spreadsheet.*;
import com.google.gdata.util.*;
import java.io.IOException;
import java.net.URL;
import java.util.List;
public class Interact
{
public static void main(String args[]) throws IOException, ServiceException
{
SpreadsheetService myService = new SpreadsheetService("exampleCo-exampleApp-1");
myService.setUserCredentials("username", "password");
// SpreadSheets Feed URL
URL metafeedUrl = new URL("https://spreadsheets.google.com/feeds/spreadsheets/private/full");
// Obtaining the SpreadSheet Feed
SpreadsheetFeed feed = myService.getFeed(metafeedUrl, SpreadsheetFeed.class);
List
// Obtaining that particular SpreadSheet...
SpreadsheetEntry entry = spreadsheets.get(10);
/* First obtain the list feed URL from a WorksheetEntry and request
this feed from our authenticated SpreadsheetService object.
The SpreadsheetServicereturns a ListFeed, which contains a list
of all the rows in this worksheet. Each row is represented as
a ListEntry object.*/
// Create a list of all the Worksheets
List
// Retreive the first Worksheet
WorksheetEntry worksheet = worksheets.get(0);
// Retrieve List Feed URL
URL listFeedUrl = worksheet.getListFeedUrl();
// Get Feed from the ListFeedURL
ListFeed listfeed = myService.getFeed(listFeedUrl, ListFeed.class);
// Examine Each List Entry from the ListFeed obtained
for (ListEntry listEntry : listfeed.getEntries()) {
System.out.println(listEntry.getCustomElements().getValue("mobile"));
}
}
}
Importing data from Google Spreadsheets as List based Feed
Classes to be imported from gdata library
java/lib/gdata-spreadsheet-1.0.jar and java/lib/gdataclient-1.0.jar jar files. And don't forget to include the jar file from the deps folder.http://code.google.com/apis/gdata/javadoc/com/google/gdata/data/spreadsheet/Column.html
Additional Link: http://wiki.developerforce.com/index.php/Google_Spreadsheets_API
import com.google.gdata.client.spreadsheet.*;
import com.google.gdata.data.spreadsheet.*;
import com.google.gdata.util.*;
import java.io.IOException;
import java.net.URL;
import java.util.List;
public class Interact
{
public static void main(String args[]) throws IOException, ServiceException
{
SpreadsheetService myService = new SpreadsheetService("exampleCo-exampleApp-1");
myService.setUserCredentials("username", "password");
URL metafeedUrl = new URL("https://spreadsheets.google.com/feeds/spreadsheets/private/full");
// Obtaining the SpreadSheet Feede
SpreadsheetFeed feed = myService.getFeed(metafeedUrl, SpreadsheetFeed.class);
List
// Obtaining that particular SpreadSheet
SpreadsheetEntry entry = spreadsheets.get(10);
/* First obtain the list feed URL from a WorksheetEntry and request
this feed from our authenticated SpreadsheetService object.
The SpreadsheetServicereturns a ListFeed, which contains a list
of all the rows in this worksheet. Each row is represented as
a ListEntry object.*/
// Create a list of all the Worksheets
List
// Retreive the first Worksheet
WorksheetEntry worksheet = worksheets.get(0);
// Retrieve List Feed URL
URL listFeedUrl = worksheet.getListFeedUrl();
// Get Feed from the ListFeedURL
ListFeed listfeed = myService.getFeed(listFeedUrl, ListFeed.class);
// Examine Each List Entry from the ListFeed obtained
for (ListEntry listEntry : listfeed.getEntries()) {
System.out.println(listEntry.getTitle().getPlainText());
// Looping the no. of times, no. of columns present
for (String tag : listEntry.getCustomElements().getTags()) {
System.out.println("
}
}
}
}