Develop a JSP website on the Gae Platform

Source: Internet
Author: User

Preparations:

Download APACHE-ant, googleappengine-SDK, JDK, and other tools

Write a simple environment setting script

Setenv. bat

@echo offcd %~dp0 && echo CWD=%cd%set GAE_HOME=""for /d %%i in (appengine*) do (set GAE_HOME=%CD%\%%i)echo GAE_HOME=%GAE_HOME%set JAVA_HOME=""for /d %%i in (jdk*) do (set JAVA_HOME=%CD%\%%i)echo JAVA_HOME=%JAVA_HOME%set ANT_HOME=""for /d %%i in (apache-ant-*) do (set ANT_HOME=%CD%\%%i)echo ANT_HOME=%ANT_HOME%SET PATH=%JAVA_HOME%\BIN;%GAE_HOME%\BIN;%ANT_HOME%\BIN;%PATH%;@echo on%comspec%

For JSP compilation failure problems, refer to this,

Http://stackoverflow.com/questions/5622726/unable-to-update-app-failed-to-compile-jsp-files

Region path.

1. Copy the library file to the SRC subdirectory under the appid directory.

cp -rf $GAE_SDK_HOME/{shared, user} src

According to the description of the README file in the Lib of the SDK directory, shared and user are related to the execution and compilation of the user app.

2. Scan all jar packages.

Run the following command in the src directory of your app project:

for /r %i in (*.jar) do @echo %i 

Get:

D:\webdev\gae\appengine-java-sdk-1.7.7\demos\bagebit\src\user\orm\jdo2-api-2.3-eb.jar D:\webdev\gae\appengine-java-sdk-1.7.7\demos\bagebit\src\shared\appengine-local-runtime-shared.jar D:\webdev\gae\appengine-java-sdk-1.7.7\demos\bagebit\src\shared\el-api.jar D:\webdev\gae\appengine-java-sdk-1.7.7\demos\bagebit\src\shared\jsp-api.jar D:\webdev\gae\appengine-java-sdk-1.7.7\demos\bagebit\src\shared\servlet-api.jar D:\webdev\gae\appengine-java-sdk-1.7.7\demos\bagebit\src\shared\jsp\repackaged-appengine-ant-1.7.1.jar D:\webdev\gae\appengine-java-sdk-1.7.7\demos\bagebit\src\shared\jsp\repackaged-appengine-ant-launcher-1.7.1.jar D:\webdev\gae\appengine-java-sdk-1.7.7\demos\bagebit\src\shared\jsp\repackaged-appengine-jasper-6.0.29.jar D:\webdev\gae\appengine-java-sdk-1.7.7\demos\bagebit\src\shared\jsp\repackaged-appengine-jasper-el-6.0.29.jar D:\webdev\gae\appengine-java-sdk-1.7.7\demos\bagebit\src\shared\jsp\repackaged-appengine-tomcat-juli-6.0.29.jar D:\webdev\gae\appengine-java-sdk-1.7.7\demos\bagebit\src\user\appengine-api-1.0-sdk-1.7.7.jar D:\webdev\gae\appengine-java-sdk-1.7.7\demos\bagebit\src\user\appengine-api-labs-1.7.7.jar D:\webdev\gae\appengine-java-sdk-1.7.7\demos\bagebit\src\user\appengine-jsr107cache-1.7.7.jar D:\webdev\gae\appengine-java-sdk-1.7.7\demos\bagebit\src\user\jsr107cache-1.1.jar D:\webdev\gae\appengine-java-sdk-1.7.7\demos\bagebit\src\user\orm\datanucleus-appengine-1.0.10.final.jar D:\webdev\gae\appengine-java-sdk-1.7.7\demos\bagebit\src\user\orm\datanucleus-core-1.1.5.jar D:\webdev\gae\appengine-java-sdk-1.7.7\demos\bagebit\src\user\orm\datanucleus-jpa-1.1.5.jar D:\webdev\gae\appengine-java-sdk-1.7.7\demos\bagebit\src\user\orm\geronimo-jpa_3.0_spec-1.1.1.jar D:\webdev\gae\appengine-java-sdk-1.7.7\demos\bagebit\src\user\orm\geronimo-jta_1.1_spec-1.1.1.jar D:\webdev\gae\appengine-java-sdk-1.7.7\demos\bagebit\src\user\orm\jdo2-api-2.3-eb.jar 

Write two simple Java classes

Res. Java

package bagebit;import com.google.appengine.api.datastore.DatastoreService;import com.google.appengine.api.datastore.DatastoreServiceFactory;import com.google.appengine.api.datastore.Entity;import com.google.appengine.api.datastore.Key;import com.google.appengine.api.datastore.KeyFactory;import java.io.IOException;import java.util.logging.Logger;import javax.servlet.http.*;public class Res extends HttpServlet {    private static final Logger log = Logger.getLogger(Res.class.getName());    public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {log.info("Res::doGet called at: " + (new java.util.Date()).toString());            resp.setContentType("text/html;charset=UTF-8");            resp.getWriter().println("<b>Hello, " + req.getRequestURI() +"</b>");    }}

User. Java

package bagebit;import com.google.appengine.api.datastore.DatastoreService;import com.google.appengine.api.datastore.DatastoreServiceFactory;import com.google.appengine.api.datastore.Entity;import com.google.appengine.api.datastore.Key;import com.google.appengine.api.datastore.KeyFactory;import java.io.IOException;import java.util.logging.Logger;import javax.servlet.http.*;public class User extends HttpServlet {    private static final Logger log = Logger.getLogger(User.class.getName());    public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {log.info("User::doGet called at: " + (new java.util.Date()).toString());            resp.setContentType("text/html;charset=UTF-8");            resp.getWriter().println("<b>Hello, " + req.getRequestURI() +"</b>");    }}

In the app directory, runBatbuild. batCompile the Java file using a script:

set APPID=bagebit
set CLASSPATH="D:\webdev\gae\appengine-java-sdk-1.7.7\demos\bagebit\src\user\orm\jdo2-api-2.3-eb.jar;D:\webdev\gae\appengine-java-sdk-1.7.7\demos\bagebit\src\shared\appengine-local-runtime-shared.jar;D:\webdev\gae\appengine-java-sdk-1.7.7\demos\bagebit\src\shared\el-api.jar;D:\webdev\gae\appengine-java-sdk-1.7.7\demos\bagebit\src\shared\jsp-api.jar;D:\webdev\gae\appengine-java-sdk-1.7.7\demos\bagebit\src\shared\servlet-api.jar;D:\webdev\gae\appengine-java-sdk-1.7.7\demos\bagebit\src\shared\jsp\repackaged-appengine-ant-1.7.1.jar;D:\webdev\gae\appengine-java-sdk-1.7.7\demos\bagebit\src\shared\jsp\repackaged-appengine-ant-launcher-1.7.1.jar;D:\webdev\gae\appengine-java-sdk-1.7.7\demos\bagebit\src\shared\jsp\repackaged-appengine-jasper-6.0.29.jar;D:\webdev\gae\appengine-java-sdk-1.7.7\demos\bagebit\src\shared\jsp\repackaged-appengine-jasper-el-6.0.29.jar;D:\webdev\gae\appengine-java-sdk-1.7.7\demos\bagebit\src\shared\jsp\repackaged-appengine-tomcat-juli-6.0.29.jar;D:\webdev\gae\appengine-java-sdk-1.7.7\demos\bagebit\src\user\appengine-api-1.0-sdk-1.7.7.jar;D:\webdev\gae\appengine-java-sdk-1.7.7\demos\bagebit\src\user\appengine-api-labs-1.7.7.jar;D:\webdev\gae\appengine-java-sdk-1.7.7\demos\bagebit\src\user\appengine-jsr107cache-1.7.7.jar;D:\webdev\gae\appengine-java-sdk-1.7.7\demos\bagebit\src\user\jsr107cache-1.1.jar;D:\webdev\gae\appengine-java-sdk-1.7.7\demos\bagebit\src\user\orm\datanucleus-appengine-1.0.10.final.jar;D:\webdev\gae\appengine-java-sdk-1.7.7\demos\bagebit\src\user\orm\datanucleus-core-1.1.5.jar;D:\webdev\gae\appengine-java-sdk-1.7.7\demos\bagebit\src\user\orm\datanucleus-jpa-1.1.5.jar;D:\webdev\gae\appengine-java-sdk-1.7.7\demos\bagebit\src\user\orm\geronimo-jpa_3.0_spec-1.1.1.jar;D:\webdev\gae\appengine-java-sdk-1.7.7\demos\bagebit\src\user\orm\geronimo-jta_1.1_spec-1.1.1.jar"
javac -encoding UTF-8 src\%APPID%\*.java -d war\WEB-INF\classes

At this time, you can see the generated class file in $ appid \ war \ WEB-INF \ Classes \ $ app_package_name.

User.java -> User.classRes.java -> Res.class

Configure servlet ing:

Start $ appid/war/WEB-INF/Web. xml

<?xml version="1.0" encoding="utf-8"?><!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd"><web-app xmlns="http://java.sun.com/xml/ns/javaee" version="2.5">    <servlet>        <servlet-name>user</servlet-name>        <servlet-class>bagebit.User</servlet-class>    </servlet>    <servlet-mapping>        <servlet-name>user</servlet-name>        <url-pattern>/user</url-pattern>    </servlet-mapping>        <servlet>        <servlet-name>res</servlet-name>        <servlet-class>bagebit.Res</servlet-class>    </servlet>    <servlet-mapping>        <servlet-name>res</servlet-name>        <url-pattern>/res</url-pattern>    </servlet-mapping>    <welcome-file-list>        <welcome-file>index.jsp</welcome-file>    </welcome-file-list></web-app>

Jetty server for testing

D: \ webdev \ Gae \ appengine-java-sdk-1.7.7 \ demos \ bagebit> dev_appserver-P 8888 war2013-4-22 17:30:41 COM. google. apphosting. utils. config. appenginewebxmlreader readappenginewebxml information: successfully processed D: \ webdev \ Gae \ appengine-java-sdk-1.7.7 \ demos \ bagebit \ war \ WEB-INF/appengine-web.xml2013-04-22 17:30:41. 379: info: logging to stderr via Org. mortbay. log. stdErrLog2013-4-22 17:30:41 COM. google. apphosting. utils. config. abstractconfigxmlreader readconfigxml information: successfully processed D: \ webdev \ Gae \ appengine-java-sdk-1.7.7 \ demos \ bagebit \ war \ WEB-INF/web. xml2013-4-22 17:30:41 COM. google. appengine. tools. development. systempropertiesmanager setsystemproperties information: overwriting system property key'java. util. logging. config. file ', value 'd: \ webdev \ Gae \ appengine-java-sdk-1.7.7 \ config \ SDK \ Logging. properties 'with value' web-INF/logging. properties 'from 'd: \ webdev \ Gae \ appengine-java-sdk-1.7.7 \ demos \ bagebit \ war \ WEB-INF \ appengine-web.xml '2017-04-22 17:30:41. 695: info: jetty-6.1.x2013-04-22 17:30:42. 671: info: started SelectChannelConnector@127.0.0.1: 88882013-4-22 9:30:42 COM. google. appengine. tools. development. abstractserver startup: Server default is running at http: // localhost: 8888/2013-4-22 9:30:42 COM. google. appengine. tools. development. abstractserver startup information: the Admin console is running at http: // localhost: 8888/_ Ah/admin2013-4-22 9:30:42 COM. google. appengine. tools. development. devappserverimpl start information: Dev app server is now running

Test in a browser

HTTP ERROR 500Problem accessing /. Reason:    Unable to compile class for JSP: An error occurred at line: 7 in the generated java fileOnly a type can be imported. com.google.appengine.api.users.User resolves to a packageAn error occurred at line: 8 in the generated java fileOnly a type can be imported. com.google.appengine.api.users.UserService resolves to a packageAn error occurred at line: 9 in the generated java fileOnly a type can be imported. com.google.appengine.api.users.UserServiceFactory resolves to a package

An error occurred. It seems that the index. jsp compilation on the homepage failed.

The reason is that the corresponding JSP support library is not copied to the lib directory of the WEB-INF.

Copy from the user sub-directory under the src directory just copied, or from the user sub-directory under the SDK lib directory to maintain the folder structure.

D: \ webdev \ Gae \ appengine-java-sdk-1.7.7 \ demos \ bagebit> xcopy/y/e/s SRC \ User war \ WEB-INF \ libsrc \ User \ appengine-api-1.0-sdk-1.7.7.jarsrc \ User \ appengine-api-labs-1.7.7.jarsrc \ User \ appengine-jsr107cache-1.7.7.jarsrc \ User \ jsr107cache-1.1.jarsrc \ User \ ORM \ datanucleus-appengine-1.0.10.final.jarsrc \ User \ ORM \ datanucleus-core-1.1.5.jarsrc \ User \ ORM \ datanucleus-jpa-1.1.5.jarsrc \ User \ ORM \ geronimo-jpa_3.0_spec-1.1.1.jarsrc \ User \ ORM \ geronimo-jta_1.1_spec-1.1.1.jarsrc \ User \ ORM \ jdo2-api-2.3-eb.jar copied 10 files

The shared directory is no longer required. Read the Lib readme document.

The specific copy of those jar packages is necessary can refer to the guestbook example in the WEB-INF/lib below.

Note:: When updating to the server, remember to delete the jar package that comes with Gae In your project.

For example, do not upload the jar packages copied in the user directory. You can upload the 3rd jar packages that you have added together. The server has prepared these jar packages for you.

Otherwise, the upload of many huge jar Packages may fail, and the prompt -- enable-jar-splitting appears.

Note:: The examples in demos provided by Gae also add auxiliary jar packages to guestbook. Therefore, dev_appserver-P 8888 war can be used for testing. Other examples do not seem to be able to run, at least a few of my tests won't work.

OK. The page is displayed.

Change a simple JSP page

<%@ page contentType="text/html;charset=UTF-8" language="java" %><%@ page import="java.util.List" %><%@ page import="com.google.appengine.api.users.User" %><%@ page import="com.google.appengine.api.users.UserService" %><%@ page import="com.google.appengine.api.users.UserServiceFactory" %><%@ page import="com.google.appengine.api.datastore.DatastoreServiceFactory" %><%@ page import="com.google.appengine.api.datastore.DatastoreService" %><%@ page import="com.google.appengine.api.datastore.Query" %><%@ page import="com.google.appengine.api.datastore.Entity" %><%@ page import="com.google.appengine.api.datastore.FetchOptions" %><%@ page import="com.google.appengine.api.datastore.Key" %><%@ page import="com.google.appengine.api.datastore.KeyFactory" %><%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>

You do not need to restart the server. The JSP file takes effect immediately.

This is the configurable option of jetty.

Submit it to Servlet and let Java handle it.

Error reported, http405,

The reason is that index. jsp is requested in post mode, but res. Java does not implement the dopost () method.

More useful. Let's get started...

Back-end storage, that is, "Database Operations". Please first review Google's data storage policies. Google's big data itself is amazing. If Google is Our backend, you can rest assured.

Https://developers.google.com/appengine/docs/java/gettingstarted/usingdatastore

Create a broker. Java

package bagebit;import com.google.appengine.api.datastore.DatastoreService;import com.google.appengine.api.datastore.DatastoreServiceFactory;import com.google.appengine.api.datastore.Entity;import com.google.appengine.api.datastore.Key;import com.google.appengine.api.datastore.KeyFactory;import java.io.IOException;import java.util.logging.Logger;import javax.servlet.http.*;public class Broker extends HttpServlet {    private static final Logger log = Logger.getLogger(Broker.class.getName());    public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {log.info("Broker::doGet called at: " + (new java.util.Date()).toString());            resp.setContentType("text/html;charset=UTF-8");            resp.getWriter().println("<b>Hello, " + req.getRequestURI() +"</b>");    }public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {log.info("Res::doPost called at: " + (new java.util.Date()).toString());            resp.setContentType("text/html;charset=UTF-8");            resp.getWriter().println("<b>Hello, " + req.getRequestURI() +"</b>");    }}

Modify the Web ing of Web. xml

<?xml version="1.0" encoding="utf-8"?><!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd"><web-app xmlns="http://java.sun.com/xml/ns/javaee" version="2.5">    <servlet>        <servlet-name>SERVLET_KEY_do</servlet-name>        <servlet-class>bagebit.Broker</servlet-class>    </servlet>    <servlet-mapping>        <servlet-name>SERVLET_KEY_do</servlet-name>        <url-pattern>/do</url-pattern>    </servlet-mapping>    <welcome-file-list>        <welcome-file>index.jsp</welcome-file>    </welcome-file-list></web-app>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.