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

与java一起使用google app engine会给出“未在此服务器上找到URL”,但与localhost一起使用

孟翰海
2023-03-14
Error: Not Found
The requested URL /BookApp was not found on this server.
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);

    }

}
<!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>

共有1个答案

沙星波
2023-03-14

当您正在使用Java8( Java8 )并按照Google App engine规范,尝试以以下方式实现:

// 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

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

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

  • 我有一个烧瓶服务器运行在http://127.0.0.1:5000和一个vuejs前端运行http://localhost:8080我已经做了api,并用postman测试了它,一切都如预期的那样工作:( > 将请求发布到/登录- (将请求发送至/登录)- 烧瓶API代码: 登录。vue: 指数vue 当我使用邮递员登录时,我得到的响应为;当我使用邮递员获取url/索引时,我得到响应。数据但当我使

  • 问题内容: 如果我说Postgres不接受连接,但是如果我说它可以工作 我的 如果我添加以下行,则Postgres服务将启动: 那里怎么了? 更新 我的档案: 问题答案: 在pg_hba.conf中,第 一个匹配 计数。每个文档: 具有匹配的连接类型,客户端地址,请求的数据库和用户名的第一条记录用于执行身份验证。没有“失败”或“备份”:如果选择了一条记录并且验证失败,则不会考虑后续记录。如果没有记

  • 问题内容: 我对WireMock完全陌生。 到目前为止,我一直在使用通过SOAPUI进行模拟响应。我的用例很简单: 只需将SOAP XML请求发送到不同的端点(http:// localhost:9001 / endpoint1 ),然后返回固定的XML响应。但是MockWrire必须作为独立服务部署到专用服务器上,该服务器将充当从中提供模拟响应的中心位置。 只是想要一些开始的建议。如我所见,Wi

  • 问题内容: 我已经使用Selenium和最初的PhantomJS开发了一些Python脚本。在走向自动下载时,我改用了(带头的)Firefox(运行了),然后选择了无头选项的Chrome,这样我就不会打开浏览器了。 我的第一个脚本访问一个页面和几个HTML元素,与无头Chrome完美搭配。 但是第二个 仅适用于带头的Chrome 。如果添加“无头”选项,它将不再起作用。当我尝试以无头模式打印HTM

  • 我想通过STS在Spring Boot中使用JPA和SQL Server。这是我的表: 马文 application.properties 模型.Account.class 接口账户 ServiceAccount.class 服务帐户类 我在控制器中调用了方法add帐户(),这是我得到的异常 "java.lang.NoClassDefFoundError: org/spingFramework/o