简体   繁体   中英

Using google app engine with java gives “url was not found on this server” but works with localhost

I made my first Java Servlet application with JSPs to create a record about books and display them. The index.html page loads well but the other pages in the project don't work when deployed on GoogleAppEngine. The same project works well when run locally on App Engine through eclipse. I have also enabled sessions in the "appengine-web.xml" but the problem persists. The application works perfectly fine when run locally. Running through the app engine I get the error -

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

Directory Structure

Servlet code :

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>

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>

As you're using Java8 ( <runtime>java8</runtime> ) and as per Google App engine specification try implementing this way :

// 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");
  }
}

Reference : how-requests-are-handled-app-engine

One more advice : do not use camelCase while naming your url pattern.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM