In the last lesson, we talked about how to configure the development environment, including the room-side flex development environment, and the server-side Java development environment, and wrote a sample program for the guest house HelloWorld, but unfortunately, this one helloworld is not dynamic at the moment. If the client cannot interact with the server, then I feel that my flex is not really getting started.
Directory structure and configuration description of Google app Project
I took a look at the Gapp_flexblog project that eclipse created for me, which includes the following directory structure:
Guestbook/
src/
…Java source code…
META-INF/
…other configuration…
war/
…JSPs, images, data files…
WEB-INF/
…app configuration…
lib/
…JARs for libraries…
classes/
…compiled classes…
1,src under my Sban.flexblog namespace directory, there are some *.java files, it seems that this is the source file. From the name can also be seen.
2,src/meta-inf/jdoconfig.xml is the JDO configuration file and is not available for the time being.
3,war is a standard packaging format for Java Web applications, and Google app engine uses this common format to place applications in containers.
Place the jar file in the 3.1,war/lib directory. The jar is the abbreviation of Java Archive file, a Java document, and a collection of class libraries after compilation. This is similar to the SWC file produced by Flex Library project after it was compiled.
3.2,war/web-inf is used to place some configuration files. Web.xml is a Web application configuration file that defines the mapping of servlet to URL, home page list, filter and security constraints, and so on.
In Web.xml, the following fragment defines the mapping of a servlet name to a servlet class:
<servlet>
<servlet-name>helloWorld</servlet-name>
<servlet-class>sban.flexblog.server.HelloWorldServlet</servlet-class>
</servlet>
At this point, if we define a URL to the servlet name Mapping, access the URL, the control is referred to the Sban.flexblog.server.HelloWorldServlet processing:
<servlet>
<servlet-name>helloWorld</servlet-name>
<url-pattern>/gapp_flexblog/hello</url-pattern>
</servlet>
Question 1: Is it OK if we directly define a URL to the Servlet class mapping?
Second, the dynamic Hello World application
Under Sban.flexblog.server, create a new Helloworldservlet class with the following code:
package sban.flexblog.server;
import java.io.IOException;
import javax.servlet.http.*;
public class HelloWorldServlet extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws IOException
{
String[] name = req.getParameterValues("name");
resp.setContentType("text/plain");
resp.getWriter().println("Hi " + name[0] + ",Hello, world.");
}
}
This class inherits from HttpServlet and overrides the Doget method for processing HTTP GET requests. Let it process the URL from/gapp_flexblog/hello and add the following configuration fragment to Web.xml:
<servlet>
<servlet-name>helloWorld</servlet-name>
<servlet-class>sban.flexblog.server.HelloWorldServlet</servlet-class>
</servlet>
…
<servlet-mapping>
<servlet-name>helloWorld</servlet-name>
<url-pattern>/gapp_flexblog/hello</url-pattern>
</servlet-mapping>
Starting the Gapp_flexblog project, our Web server is running normally:
If you have curl installed, you can now use it to test whether the Helloworldservlet class is working properly, open cmd, and enter:
Curl Http://localhost:8080/gapp_flexblog/hello?name=sban
The results returned are as follows: