当前位置: 首页 > 知识库问答 >
问题:

使用谷歌应用程序引擎与java给出“在此服务器上找不到网址”,但与localhost一起使用

曾光誉
2023-03-14

我用 JSP 制作了我的第一个 Java Servlet 应用程序,用于创建关于书籍的记录并显示它们。索引.html页面加载良好,但项目中的其他页面在GoogleAppEngine上部署时不起作用。当通过 eclipse 在 App Engine 上本地运行时,同一项目运行良好。我还在“appengine-web.xml”中启用了会话,但问题仍然存在。该应用程序在本地运行时工作正常。通过应用程序引擎运行,我得到错误 -

Error: Not Found
The requested URL /BookApp was not found on this server.

目录结构

Servlet代码:

package com.nh;

import com.books.Book;

import java.util.*;
import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet implementation class BookApp
 */

@WebServlet("/BookApp")
public class BookApp extends HttpServlet {

    private static final long serialVersionUID = 1L;
    ArrayList<Book>Books = new ArrayList<Book>();
    static int id = 0;

    /*List<String> BookNames = new ArrayList<String>();
    List<String> Authors = new ArrayList<String>();
    List<String> Costs = new ArrayList<String>();*/

    /**
     * @see HttpServlet#HttpServlet()
     */
    public BookApp() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        //response.getWriter().append("Served at: ").append(request.getContextPath());

    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
        String bookName = request.getParameter("bookname");
        String author = request.getParameter("authorname");
        String bookCost = request.getParameter("cost");
        String url = ("");
        HttpSession session=request.getSession(true);
        Book newBook = new Book();

        if(bookName.length()!=0&&author.length()!=0&&bookCost.length()!=0)
        {
            newBook.setAuthorName(author);
            newBook.setName(bookName);
            newBook.setCost(Float.parseFloat(bookCost));
            newBook.setId(id++);
            Books.add(newBook);
        }
        session.setAttribute("Books", Books);
        request.setAttribute("Books", Books);
        System.out.println(Books.get(0).getName());
        url = ("/listBooks.jsp");
        RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(url);
        dispatcher.forward(request, response);

    }

}

Index.html

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Create a book entry</title>
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
  <h2>Create A Book</h2>
  <form action="BookApp" method="post">
    <div class="form-group">
      <label for="Title">Title:</label>
      <input type="text" class="form-control" id="tbBook" placeholder="Enter Title" name="bookname" required>
    </div>
    <div class="form-group">
      <label for="author">Author:</label>
      <input type="text" class="form-control" id="authorname" placeholder="Enter Author" name="authorname" required>
    </div>
    <div class="form-group">
      <label for="cost">Cost:</label>
      <input type="text" class="form-control" id="tbCost" placeholder="Enter Cost" name="cost" required>
    </div>

    <button type="submit" class="btn btn-default">Create</button>
  </form>
</div>
</body>
</html>

清单。jsp:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ page import="com.books.Book" %>
<%@ page import="java.util.*" %>


<!DOCTYPE html>
<html>

<head>
<meta charset="ISO-8859-1">
  <title>List of books</title>

  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js"></script>
</head>

<body>
<div class="container">
  <h2>Book List</h2>  
      <table class="table table-hover">
      <thead>
      <tr>
        <th>Sr No.</th>
        <th>Title</th>
      </tr>
    </thead>
    <tbody>
      <%
      ArrayList<Book> posts=(ArrayList<Book>) request.getAttribute("Books"); 

    for (Book book: posts) {   
    %>
    <%session.setAttribute("Books", posts); %>
      <tr>
        <td><a href="result.jsp?Id=<%=book.getId() %>"><%=book.getId()+1 %></a></td>
        <td><%=book.getName()%></td>
      </tr>
      <%}%>

    </tbody>
  </table>
</div>

</body>
</html>

appengine-web.xml

<?xml version="1.0" encoding="utf-8"?>
<appengine-web-app xmlns="http://appengine.google.com/ns/1.0">

  <threadsafe>true</threadsafe>
  <sessions-enabled>true</sessions-enabled>
  <runtime>java8</runtime>

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


</appengine-web-app>

共有1个答案

郎星汉
2023-03-14

当您使用Java8时(

// With @WebServlet annotation the webapp/WEB-INF/web.xml is no longer required.
@WebServlet(name = "requests", description = "Requests: Trivial request", urlPatterns = "/requests")
public class RequestsServlet extends HttpServlet {

  @Override
  public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
    resp.setContentType("text/plain");
    resp.getWriter().println("Hello, world");
  }
}

参考:如何处理请求应用程序引擎

还有一个建议:在命名url模式时不要使用camelCase。

 类似资料:
  • 问题内容: 我正在按照http://googcloudlabs.appspot.com/教程创建新的Google App Engine项目。当我尝试从本地主机运行时,却按如下所述抛出错误,但是当我将其部署正常时。(http://mynewcloudcom.appspot.com/)。请帮忙。 Eclipse控制台 问题答案: 找到了答案。我需要在JDK1.6中运行。一旦我更改了Java编译器(右键

  • 它以GAE标准运行。更改日志权限后更新日志: 权限已更改:triage@appspot.gserviceaccount.com 应用程序引擎默认服务帐户 编辑器日志编写器所有者 有趣的是,到上周为止,它一直运行得很好。 当我试图访问该网站时,显示503个错误。2018-07-11 11:16:26.296 CDT GET 302 0 B 1 ms Chrome 67/144.188.128.2-[

  • 当我尝试访问我的struts2 tiles应用程序(在google app engine中)时,出现以下错误 与上下文路径[]关联的命名空间和操作名称[]没有映射的操作[未知位置] 下面是堆栈跟踪 堆栈痕迹 我的文件内容是: 我不知道为什么它不加载默认情况下。 请告知。 你好,穆吉尔

  • 我是谷歌应用引擎服务的新手。我有一个JavaMaven项目,其中一个模块运行在应用引擎flex上,另一个模块运行在应用引擎标准上。我正在为应用引擎Flex API使用JWT身份验证。我想从应用引擎标准向应用引擎Flex发出发布请求。验证服务的最佳方式应该是什么? 此外,我还有一个cron服务,它可以访问我用于某些后端内容的特定URL。如何验证请求是否仅来自Cron服务?

  • 我是谷歌应用引擎的新手。要使用Java App Engine,Google提供了两个选项:创建一个Maven项目,或者由Eclipse Google插件支持的非Maven项目。根据文档,Eclipse版本更容易。那么我应该继续日食吗?但是我看到了很多关于maven的帖子。有人能评论一下使用Maven的利弊吗。 谢谢