使用带有 java 的谷歌应用程序引擎会给出“在此服务器上找不到 url”但适用于 localhost
Posted
技术标签:
【中文标题】使用带有 java 的谷歌应用程序引擎会给出“在此服务器上找不到 url”但适用于 localhost【英文标题】:Using google app engine with java gives "url was not found on this server" but works with localhost 【发布时间】:2019-07-30 14:51:19 【问题描述】:我使用 JSP 创建了我的第一个 Java Servlet 应用程序,以创建有关书籍的记录并显示它们。 index.html 页面加载良好,但项目中的其他页面在部署到 GoogleAppEngine 时无法正常工作。通过 eclipse 在 App Engine 上本地运行时,相同的项目运行良好。我还在“appengine-web.xml”中启用了会话,但问题仍然存在。该应用程序在本地运行时运行良好。 通过应用程序引擎运行我得到错误 -
Error: Not Found
The requested URL /BookApp was not found on this server.
Directory Structure
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);
索引.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>
listBook.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>
【问题讨论】:
我不确定,但是如果您将WebContent
文件夹放在包旁边的 src
文件夹中,就像在 sample's 结构中一样?
【参考方案1】:
当您使用 Java8 (<runtime>java8</runtime>
) 并按照 Google App 引擎规范尝试以这种方式实现时:
// 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");
参考:how-requests-are-handled-app-engine
还有一个建议:do not usecamelCase,同时命名您的 url 模式。
【讨论】:
以上是关于使用带有 java 的谷歌应用程序引擎会给出“在此服务器上找不到 url”但适用于 localhost的主要内容,如果未能解决你的问题,请参考以下文章
如何将带有秘密管理器的谷歌应用引擎连接到 Postgres?