Using JSPs

Using JSPs

While we could output the HTML for our user interface directly from the Java servlet code, this would be difficult to maintain as the HTML gets complicated. It's better to use a template system, with the user interface designed and implemented in separate files with placeholders and logic to insert data provided by the application. There are many template systems available for Java, any of which would work with App Engine.

大意:我们可以在servlet中,输出html代码,但这使得这些html代码难于维护。它最好使用模版系统,这些模版有些设计了用户界面,实现了通过包含占位符的单独文件和逻辑插入数据的应用。有许多java可用的模版系统,任何个模版都可应用于App Engine

For this tutorial, we'll use JavaServer Pages (JSPs) to implement the user interface for the guest book. JSPs are part of the servlet standard. App Engine compiles JSP files in the application's WAR automatically, and maps them to URL paths.

大意:在这个向导中,我们将使用JSPs 来实现guestbook的用户界面,jspsservlet标准的一部分,App Engin自动编译WAR目录下的JSP文件,并且映射到URL路径上。

Hello, JSP!

Our guest book app writes strings to an output stream, but this could also be written as a JSP. Let's begin by porting the latest version of the example to a JSP.

In the directory war/, create a file named guestbook.jsp with the following contents:

大意:我们的guset book应用通过一个输出流将字符串输出,这些同样能够以jsp方式来写,我们开始把最后一个版本的guest book应用移植到jsp上。在目录war/下,创建一个名为guestbook.jsp,其有如下内容:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>

<%@ page import="com.google.appengine.api.users.User" %>

<%@ page import="com.google.appengine.api.users.UserService" %>

<%@ page import="com.google.appengine.api.users.UserServiceFactory" %>

<html>

  <body>

<%

    UserService userService = UserServiceFactory.getUserService();

    User user = userService.getCurrentUser();

    if (user != null) {

%>

Hello, <%= user.getNickname() %>! (You can

<a href="<%= userService.createLogoutURL(request.getRequestURI()) %>">sign out.)

<%

    } else {

%>

Hello!

<a href="<%= userService.createLoginURL(request.getRequestURI()) %>">Sign in

to include your name with greetings you post.

<%

    }

%>

  </body>

</html>

By default, any file in war/ or a subdirectory (other than WEB-INF/) whose name ends in .jsp is automatically mapped to a URL path. The URL path is the path to the .jsp file, including the filename. This JSP will be mapped automatically to the URL /guestbook.jsp.

大意:默认情况下,任何在war目录下或其子目录下并以.jsp结尾的文件都会被自动映射到一个URL路径。这个URL路径是这个.jsp文件的路径,包括文件名。此jsp文件将会自动映射到 /guestbook.jsp

For the guest book app, we want this to be the application's home page, displayed when someone accesses the URL /. An easy way to do this is to declare in web.xml that guestbook.jsp is the "welcome" servlet for that path.

大意:对于这个guest book应用,我想使用这个页面作为整个应用的主页面,即当用户输入/时,显示的就是这个页面。一个简单的方式就是在web.xml中通过“welcome”标签来配置guestbook.jsp

Edit war/WEB-INF/web.xml and replace the current <welcome-file> element in the <welcome-file-list>. Be sure to remove index.html from the list, as static files take precedence over JSP and servlets.

大意:编辑war/WEB-INF/web.xml,替换在<welcome-file-list>中的<welcome-file>的配置,将index.htm移除,换上你想使用的jsp,如下:

    <welcome-file-list>

        <welcome-file>guestbook.jsp</welcome-file>

    </welcome-file-list>

Tip: If you are using Eclipse, the editor may open this file in "Design" mode. To edit this file as XML, select the "Source" tab at the bottom of the frame.

Stop then start the development server. Visit the following URL:

提示:如果你使用Eclipse,编辑器可能以“设计(Design)”模式打开。为了编辑此XML代码,请选择在版面底部的”source”

停止然后启动服务,使用如下URL访问:

The app displays the contents of guestbook.jsp, including the user nickname if the user is signed in.

大意:这个应用显示了guestbook.jsp的内容,如果用户登录了,将显示其昵称

When you load a JSP for the first time, the development server converts the JSP into Java source code, then compiles the Java source into Java bytecode. The Java source and the compiled class are saved to a temporary directory. The development server regenerates and compiles JSPs automatically if the original JSP files change.

大意:当你第一次载入JSP,应用服务器将会将jsp转化为java源代码,然后将这些java源代码转化为java字节码,这些源代码和这些编译好的类被存放在一个临时的目录里。如果这些jsp文件改变了,应用服务器将更新和编译jsp文件。

When you upload your application to App Engine, the SDK compiles all JSPs to bytecode, and only uploads the bytecode. When your app is running on App Engine, it uses the compiled JSP classes.

大意:当你上传你的应用到App Engine时,SDK把所有的jsp文件编译成字节码文件,并只上传这些字节码文件。当你的应用跑在App Engine上时,它使用的是这些编译好的JSP类。

The Guestbook Form

Our guest book application will need a web form so the user can post a new greeting, and a way to process that form. The HTML of the form will go into the JSP. The destination of the form will be a new URL, /sign, to be handled by a new servlet class, SignGuestbookServlet. SignGuestbookServlet will process the form, then redirect the user's browser back to /guestbook.jsp. For now, the new servlet will just write the posted message to the log.

Edit guestbook.jsp, and put the following lines just above the closing </body> tag:

大意:guestbook应用将需要一个表单,使得用户能够提交一个新的问候,并且需要一个处理表单的方法。在JSP中可以使用HTML表单,这个表单将被提交到/sign,一个新的处理servlet类,SignGuestbookServlet,这个类将处理这个表单,然后转到/guestbook.jsp页面,目前,这个新的servlet仅仅只是把请求的消息写到日志中。编辑guestbook.jsp,把下面的几行放到<body></body>标签中。

  ...

 

  <form action="/sign" method="post">

    <div><textarea name="content" rows="3" cols="60"></textarea></div>

    <div><input type="submit" value="Post Greeting" /></div>

  </form>

  </body>

</html>

Create a new class named SignGuestbookServlet in the package guestbook. (Non-Eclipse users, create the file SignGuestbookServlet.java in the directory src/guestbook/.) Give the source file the following contents:

大意:在src/guestbook/目录下创建一个新的Servlet:SignGuestbookServlet:

package guestbook;

import java.io.IOException;
import java.util.logging.Logger;
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;

public class SignGuestbookServlet extends HttpServlet {
    private static final Logger log = Logger.getLogger(SignGuestbookServlet.class.getName());

    public void doPost(HttpServletRequest req, HttpServletResponse resp)
                throws IOException {
        UserService userService = UserServiceFactory.getUserService();
        User user = userService.getCurrentUser();

        String content = req.getParameter("content");
        if (content == null) {
            content = "(No greeting)";
        }
        if (user != null) {
            log.info("Greeting posted by user " + user.getNickname() + ": " + content);
        } else {
            log.info("Greeting posted anonymously: " + content);
        }
        resp.sendRedirect("/guestbook.jsp");
    }
}

Edit war/WEB-INF/web.xml and add the following lines to declare the SignGuestbookServlet servlet and map it to the the /sign URL:

大意:修改war/WEB-INF/web.xml ,添加如下行声明SignGuestbookServlet的映射路径为 /sign

<web-app xmlns="http://java.sun.com/xml/ns/javaee" version="2.5">

    ...

 

    <servlet>

        <servlet-name>sign</servlet-name>

        <servlet-class>guestbook.SignGuestbookServlet</servlet-class>

    </servlet>

    <servlet-mapping>

        <servlet-name>sign</servlet-name>

        <url-pattern>/sign</url-pattern>

    </servlet-mapping>

 

    ...

</web-app>

This new servlet uses the java.util.logging.Logger class to write messages to the log. You can control the behavior of this class using a logging.properties file, and a system property set in the app's appengine-web.xml file. If you are using Eclipse, your app was created with a default version of this file in your app's src/ and the appropriate system property.

大意:这个sevlet使用了java.util.loggin.Logger类将信息写出到日志。通过使用logging.properties文件改变其行为,如果你正使用Eclipse,你的应用将在src目录下创建一个默认的文件版本及适当的系统属性。

If you are not using Eclipse, you must set up the Logger configuration file manually. Copy the example file from the SDK appengine-java-sdk/config/user/logging.properties to your app's war/WEB-INF/ directory. Then edit the app's war/WEB-INF/appengine-web.xml file as indicated:

大意:如果你没使用Eclipse,你必须手动配置logger配置,将appengine-java-sdk/config/user/logging.properties复制至你的应用war/WEB-INF/directory下。然后编辑这个应用下的:

War/WEB-INF/appengine-web.xml文件如下:

<appengine-web-app xmlns="http://appengine.google.com/ns/1.0">

    ...

    <system-properties>

        <property name="java.util.logging.config.file" value="WEB-INF/logging.properties"/>

    </system-properties>

</appengine-web-app>

The servlet logs messages using the INFO log level (using log.info()). The default log level is WARNING, which suppresses INFO messages from the output. To change the log level for all classes in the guestbook package, edit the logging.properties file and add an entry for guestbook.level, as follows:

Servlet 日志消息使用INFO日志标准(使用log.info())。默认的日志标准为Warning,如果要改变在guestbook包下,所有类文件的日志标准,需要修改logging.properties文件,并添加一个条目,如下:

.level = WARNING

guestbook.level = INFO

...

Tip: When your app logs messages using the java.util.logging.Logger API while running on App Engine, App Engine records the messages and makes them available for browsing in the Admin Console, and available for downloading using the AppCfg tool. The Admin Console lets you browse messages by log level.

Rebuild and restart, then test http://localhost:8080/. The form displays. Enter some text in the form, and submit. The browser sends the form to the app, then redirects back to the empty form. The greeting data you entered is logged to the console by the server.

提示:当你的应用日志使用java.util.logging.Logger API,当运行在App Engine时,App Engine记录这个消息,并且使他们显示在管理员控治台上,并且可使用AppCfg工具下载。管理员控制台可以通过日志标准来显示消息。

重新部署,然后重启,通过http://localhost:8080/,显示表单。输入一些文本,提交,浏览器将提交表单,然后返回一个空表单。你输入的数据被服务器记录到了控制台。

Using JSPs

While we could output the HTML for our user interface directly from the Java servlet code, this would be difficult to maintain as the HTML gets complicated. It's better to use a template system, with the user interface designed and implemented in separate files with placeholders and logic to insert data provided by the application. There are many template systems available for Java, any of which would work with App Engine.

大意:我们可以在servlet中,输出html代码,但这使得这些html代码难于维护。它最好使用模版系统,这些模版有些设计了用户界面,实现了通过包含占位符的单独文件和逻辑插入数据的应用。有许多java可用的模版系统,任何个模版都可应用于App Engine

For this tutorial, we'll use JavaServer Pages (JSPs) to implement the user interface for the guest book. JSPs are part of the servlet standard. App Engine compiles JSP files in the application's WAR automatically, and maps them to URL paths.

大意:在这个向导中,我们将使用JSPs 来实现guestbook的用户界面,jspsservlet标准的一部分,App Engin自动编译WAR目录下的JSP文件,并且映射到URL路径上。

Hello, JSP!

Our guest book app writes strings to an output stream, but this could also be written as a JSP. Let's begin by porting the latest version of the example to a JSP.

In the directory war/, create a file named guestbook.jsp with the following contents:

大意:我们的guset book应用通过一个输出流将字符串输出,这些同样能够以jsp方式来写,我们开始把最后一个版本的guest book应用移植到jsp上。在目录war/下,创建一个名为guestbook.jsp,其有如下内容:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>

<%@ page import="com.google.appengine.api.users.User" %>

<%@ page import="com.google.appengine.api.users.UserService" %>

<%@ page import="com.google.appengine.api.users.UserServiceFactory" %>

 

<html>

  <body> 

<%

    UserService userService = UserServiceFactory.getUserService();

    User user = userService.getCurrentUser();

    if (user != null) {

%>

Hello, <%= user.getNickname() %>! (You can

<a href="<%= userService.createLogoutURL(request.getRequestURI()) %>">sign out.)

 

<%

    } else {

%>

Hello!

<a href="<%= userService.createLoginURL(request.getRequestURI()) %>">Sign in

to include your name with greetings you post.

 

<%

    }

%>

 

  </body>

</html>

By default, any file in war/ or a subdirectory (other than WEB-INF/) whose name ends in .jsp is automatically mapped to a URL path. The URL path is the path to the .jsp file, including the filename. This JSP will be mapped automatically to the URL /guestbook.jsp.

大意:默认情况下,任何在war目录下或其子目录下并以.jsp结尾的文件都会被自动映射到一个URL路径。这个URL路径是这个.jsp文件的路径,包括文件名。此jsp文件将会自动映射到 /guestbook.jsp

For the guest book app, we want this to be the application's home page, displayed when someone accesses the URL /. An easy way to do this is to declare in web.xml that guestbook.jsp is the "welcome" servlet for that path.

大意:对于这个guest book应用,我想使用这个页面作为整个应用的主页面,即当用户输入/时,显示的就是这个页面。一个简单的方式就是在web.xml中通过“welcome”标签来配置guestbook.jsp

Edit war/WEB-INF/web.xml and replace the current <welcome-file> element in the <welcome-file-list>. Be sure to remove index.html from the list, as static files take precedence over JSP and servlets.

大意:编辑war/WEB-INF/web.xml,替换在<welcome-file-list>中的<welcome-file>的配置,将index.htm移除,换上你想使用的jsp,如下:

    <welcome-file-list>

        <welcome-file>guestbook.jsp</welcome-file>

    </welcome-file-list>

Tip: If you are using Eclipse, the editor may open this file in "Design" mode. To edit this file as XML, select the "Source" tab at the bottom of the frame.

Stop then start the development server. Visit the following URL:

提示:如果你使用Eclipse,编辑器可能以“设计(Design)”模式打开。为了编辑此XML代码,请选择在版面底部的”source”

停止然后启动服务,使用如下URL访问:

The app displays the contents of guestbook.jsp, including the user nickname if the user is signed in.

大意:这个应用显示了guestbook.jsp的内容,如果用户登录了,将显示其昵称

When you load a JSP for the first time, the development server converts the JSP into Java source code, then compiles the Java source into Java bytecode. The Java source and the compiled class are saved to a temporary directory. The development server regenerates and compiles JSPs automatically if the original JSP files change.

大意:当你第一次载入JSP,应用服务器将会将jsp转化为java源代码,然后将这些java源代码转化为java字节码,这些源代码和这些编译好的类被存放在一个临时的目录里。如果这些jsp文件改变了,应用服务器将更新和编译jsp文件。

When you upload your application to App Engine, the SDK compiles all JSPs to bytecode, and only uploads the bytecode. When your app is running on App Engine, it uses the compiled JSP classes.

大意:当你上传你的应用到App Engine时,SDK把所有的jsp文件编译成字节码文件,并只上传这些字节码文件。当你的应用跑在App Engine上时,它使用的是这些编译好的JSP类。

The Guestbook Form

Our guest book application will need a web form so the user can post a new greeting, and a way to process that form. The HTML of the form will go into the JSP. The destination of the form will be a new URL, /sign, to be handled by a new servlet class, SignGuestbookServlet. SignGuestbookServlet will process the form, then redirect the user's browser back to /guestbook.jsp. For now, the new servlet will just write the posted message to the log.

Edit guestbook.jsp, and put the following lines just above the closing </body> tag:

大意:guestbook应用将需要一个表单,使得用户能够提交一个新的问候,并且需要一个处理表单的方法。在JSP中可以使用HTML表单,这个表单将被提交到/sign,一个新的处理servlet类,SignGuestbookServlet,这个类将处理这个表单,然后转到/guestbook.jsp页面,目前,这个新的servlet仅仅只是把请求的消息写到日志中。编辑guestbook.jsp,把下面的几行放到<body></body>标签中。

  ...

 

  <form action="/sign" method="post">

    <div><textarea name="content" rows="3" cols="60"></textarea></div>

    <div><input type="submit" value="Post Greeting" /></div>

  </form>

  </body>

</html>

Create a new class named SignGuestbookServlet in the package guestbook. (Non-Eclipse users, create the file SignGuestbookServlet.java in the directory src/guestbook/.) Give the source file the following contents:

大意:在src/guestbook/目录下创建一个新的Servlet:SignGuestbookServlet:

package guestbook;

import java.io.IOException;
import java.util.logging.Logger;
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;

public class SignGuestbookServlet extends HttpServlet {
    private static final Logger log = Logger.getLogger(SignGuestbookServlet.class.getName());

    public void doPost(HttpServletRequest req, HttpServletResponse resp)
                throws IOException {
        UserService userService = UserServiceFactory.getUserService();
        User user = userService.getCurrentUser();

        String content = req.getParameter("content");
        if (content == null) {
            content = "(No greeting)";
        }
        if (user != null) {
            log.info("Greeting posted by user " + user.getNickname() + ": " + content);
        } else {
            log.info("Greeting posted anonymously: " + content);
        }
        resp.sendRedirect("/guestbook.jsp");
    }
}

Edit war/WEB-INF/web.xml and add the following lines to declare the SignGuestbookServlet servlet and map it to the the /sign URL:

大意:修改war/WEB-INF/web.xml ,添加如下行声明SignGuestbookServlet的映射路径为 /sign

<web-app xmlns="http://java.sun.com/xml/ns/javaee" version="2.5">

    ...

 

    <servlet>

        <servlet-name>sign</servlet-name>

        <servlet-class>guestbook.SignGuestbookServlet</servlet-class>

    </servlet>

    <servlet-mapping>

        <servlet-name>sign</servlet-name>

        <url-pattern>/sign</url-pattern>

    </servlet-mapping>

 

    ...

</web-app>

This new servlet uses the java.util.logging.Logger class to write messages to the log. You can control the behavior of this class using a logging.properties file, and a system property set in the app's appengine-web.xml file. If you are using Eclipse, your app was created with a default version of this file in your app's src/ and the appropriate system property.

大意:这个sevlet使用了java.util.loggin.Logger类将信息写出到日志。通过使用logging.properties文件改变其行为,如果你正使用Eclipse,你的应用将在src目录下创建一个默认的文件版本及适当的系统属性。

If you are not using Eclipse, you must set up the Logger configuration file manually. Copy the example file from the SDK appengine-java-sdk/config/user/logging.properties to your app's war/WEB-INF/ directory. Then edit the app's war/WEB-INF/appengine-web.xml file as indicated:

大意:如果你没使用Eclipse,你必须手动配置logger配置,将appengine-java-sdk/config/user/logging.properties复制至你的应用war/WEB-INF/directory下。然后编辑这个应用下的:

War/WEB-INF/appengine-web.xml文件如下:

<appengine-web-app xmlns="http://appengine.google.com/ns/1.0">

    ...

 

    <system-properties>

        <property name="java.util.logging.config.file" value="WEB-INF/logging.properties"/>

    </system-properties>

 

</appengine-web-app>

The servlet logs messages using the INFO log level (using log.info()). The default log level is WARNING, which suppresses INFO messages from the output. To change the log level for all classes in the guestbook package, edit the logging.properties file and add an entry for guestbook.level, as follows:

Servlet 日志消息使用INFO日志标准(使用log.info())。默认的日志标准为Warning,如果要改变在guestbook包下,所有类文件的日志标准,需要修改logging.properties文件,并添加一个条目,如下:

.level = WARNING

guestbook.level = INFO 

...

Tip: When your app logs messages using the java.util.logging.Logger API while running on App Engine, App Engine records the messages and makes them available for browsing in the Admin Console, and available for downloading using the AppCfg tool. The Admin Console lets you browse messages by log level.

Rebuild and restart, then test http://localhost:8080/. The form displays. Enter some text in the form, and submit. The browser sends the form to the app, then redirects back to the empty form. The greeting data you entered is logged to the console by the server.

提示:当你的应用日志使用java.util.logging.Logger API,当运行在App Engine时,App Engine记录这个消息,并且使他们显示在管理员控治台上,并且可使用AppCfg工具下载。管理员控制台可以通过日志标准来显示消息。

重新部署,然后重启,通过http://localhost:8080/,显示表单。输入一些文本,提交,浏览器将提交表单,然后返回一个空表单。你输入的数据被服务器记录到了控制台。

 

辽B代驾管理系统对代驾订单管理、用户咨询管理、代驾订单评价管理、代驾订单投诉管理、字典管理、论坛管理、公告管理、新闻信息管理、司机管理、用户管理、管理员管理等进行集中化处理。经过前面自己查阅的网络知识,加上自己在学校课堂上学习的知识,决定开发系统选择小程序模式这种高效率的模式完成系统功能开发。这种模式让操作员基于浏览器的方式进行网站访问,采用的主流的Java语言这种面向对象的语言进行辽B代驾管理系统程序的开发,在数据库的选择上面,选择功能强大的Mysql数据库进行数据的存放操作。辽B代驾管理系统的开发让用户查看代驾订单信息变得容易,让管理员高效管理代驾订单信息。 辽B代驾管理系统具有管理员角色,用户角色,这几个操作权限。 辽B代驾管理系统针对管理员设置的功能有:添加并管理各种类型信息,管理用户账户信息,管理代驾订单信息,管理公告信息等内容。 辽B代驾管理系统针对用户设置的功能有:查看并修改个人信息,查看代驾订单信息,查看公告信息等内容。 辽B代驾管理系统针对管理员设置的功能有:添加并管理各种类型信息,管理用户账户信息,管理代驾订单信息,管理公告信息等内容。 辽B代驾管理系统针对用户设置的功能有:查看并修改个人信息,查看代驾订单信息,查看公告信息等内容。 系统登录功能是程序必不可少的功能,在登录页面必填的数据有两项,一项就是账号,另一项数据就是密码,当管理员正确填写并提交这二者数据之后,管理员就可以进入系统后台功能操作区。项目管理页面提供的功能操作有:查看代驾订单,删除代驾订单操作,新增代驾订单操作,修改代驾订单操作。公告信息管理页面提供的功能操作有:新增公告,修改公告,删除公告操作。公告类型管理页面显示所有公告类型,在此页面既可以让管理员添加新的公告信息类型,也能对已有的公告类型信息执行编辑更新,失效的公告类型信息也能让管理员快速删除。新闻管理页面,此页面提供给管理员的功能有:新增新闻,修改新闻,删除新闻。新闻类型管理页面,此页面提供给管理员的功能有:新增新闻类型,修改新闻类型,删除新闻类型。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值