3

when ever I press the submit action button to send the information to a controller i always get "HTTP 404 Status - not found: The requested resource is not available" I was just wondering if any could help me sort out this frustrating problem. Here is my Code:

LibraryInfo.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Library Information</title>
</head>
<body>
<form action="/bookServlet" method="POST">
    <fieldset style="width:300px">
    <legend>Find Book</legend>
        <table>
            <tr>
                <td>ISBN Number:</td>
                <td><input type="text" name="isbn" required="required"/></td>
            </tr>
        </table>
                <input type="submit" value="Find Book"/>
    </fieldset>
</form>
</body>
</html>

Book.java:

package com.farmani.model;

public class Book {
    private int isbn;
    private String author; 
    private String title; 
    private String status;
    public int getIsbn() {
        return isbn;
    }
    public void setIsbn(int isbn) {
        this.isbn = isbn;
    }
    public String getAuthor() {
        return author;
    }
    public void setAuthor(String author) {
        this.author = author;
    }
    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    public String getStatus() {
        return status;
    }
    public void setStatus(String status) {
        this.status = status;
    } 

}

ConnectionClass.java

package com.farmani.dao;

import java.sql.Connection;
import java.sql.SQLException;

import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

public class ConnectionClass {
    private DataSource dataSource; 
    public ConnectionClass(){
        try{
            InitialContext context = new InitialContext(); 
            dataSource = (DataSource)context.lookup("jdbc/employee"); 
        }catch(NamingException ex){
            ex.printStackTrace(); 
        }
    }
    public Connection getConnection(){
        Connection conn = null; 
        try{
            conn=dataSource.getConnection(); 
        }catch(SQLException ex){
            ex.printStackTrace(); 
        }
        return conn; 
    }
}

LibraryDAO.java

package com.farmani.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.farmani.model.Book;

public class LibraryDAO {
    private Connection connection; 
    public LibraryDAO(){
        ConnectionClass conn = new ConnectionClass(); 
        connection = conn.getConnection(); 
    }
    public Book getBookByISBN(int isbn){
        Book bk = new Book(); 
        try{
            PreparedStatement preparedStatement = connection.prepareStatement("select * from book where ISBN=?");
            preparedStatement.setInt(1,isbn);
            ResultSet rs = preparedStatement.executeQuery(); 
            if(rs.next()){
                bk.setIsbn(rs.getInt("ISBN"));
                bk.setAuthor(rs.getString("author"));
                bk.setTitle(rs.getString("title")); 
                bk.setStatus(rs.getString("status")); 
            }
        }catch(SQLException e){
            e.printStackTrace();
        }
        return bk;
    }

}

bookServlet.java:

package com.farmani.controllers;

import java.io.IOException;

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 com.farmani.dao.LibraryDAO;
import com.farmani.model.Book;


@WebServlet("/bookServlet")
public class bookServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
    private static String BOOK_DETAILS="/LibraryDetails"; 
    private LibraryDAO dao; 


    public bookServlet() {
        super();
        dao = new LibraryDAO(); 
    }


    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int isbn = Integer.parseInt(request.getParameter("isbn")); 
        Book bk = dao.getBookByISBN(isbn); 
        request.setAttribute("Book",bk); 
        RequestDispatcher view = request.getRequestDispatcher(BOOK_DETAILS); 
        view.forward(request,response); 
    }

}

Web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>LibraryWeb</display-name>
  <welcome-file-list>
    <welcome-file>LibraryInfo.jsp</welcome-file>
  </welcome-file-list>
</web-app>

LibraryDetails.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Book Details</title>
</head>
<body>
<CENTER><h1><b>Book Details</b></h1></CENTER>
<ul>
<li>ISBN: ${Book.isbn}</li>
<li>Author: ${Book.author}</li>
<li>Title: ${Book.title }</li>
<li>Status: ${Book.status}</li> 
</ul> 
</body>
</html>
Rez88
  • 57
  • 1
  • 10
  • Can you try this form tag: `
    ` ? Also, please try adding a `doGet()` method to the `bookServlet` class and access the URL directly from your web browser.
    – Tim Biegeleisen Mar 10 '15 at 06:08
  • I have tried different paths in the action form including the one you have suggested. I also tried by adding doGet(....){ doPost(request,response)} ... no luck – Rez88 Mar 10 '15 at 06:10
  • What version of Java and Glassfish are you using? – Tim Biegeleisen Mar 10 '15 at 06:13
  • Glassfish 4.0 JDK1.8.0_25 – Rez88 Mar 10 '15 at 06:17
  • Change the access modifier of `doPost` from `protected` to `public` &/or replace `action="/bookServlet"` with `action="bookServlet"`. – OO7 Mar 10 '15 at 06:24
  • `protected` should not be a problem in the method signature. – Tim Biegeleisen Mar 10 '15 at 06:35
  • Can you put the old school mappings into your `web.xml` just to see if that works? If it does work, then we know for certain the problem lies with the annotation. If it doesn't work, then the problem is something else. – Tim Biegeleisen Mar 10 '15 at 06:38
  • bookServlet com.farmani.controllers ... No Luck – Rez88 Mar 10 '15 at 06:40
  • Try replacing `@WebServlet("/bookServlet")` with `@WebServlet(name = "bookServlet" , urlPatterns = { "/bookServlet" })` or `@WebServlet(value = "/bookServlet")` For more info on this look at [@WebServlet - Java Doc](http://docs.oracle.com/javaee/7/api/javax/servlet/annotation/WebServlet.html) – OO7 Mar 10 '15 at 06:48
  • *Have u tried to run ur app on any other server ?* Bcoz everything is working fine in ur configuration. I have tried to run this configuration on JBoss & it's working. May be it's compatibility issue with Glassfish. – OO7 Mar 10 '15 at 07:17
  • Only glassfish ..as it was part of the requirement. – Rez88 Mar 10 '15 at 13:29
  • 1
    Hey, I have similar problem, but I can't find the answers anywhere. Were you able to solve it? Would you mind telling how too? Thank you very much. – Trash Can Mar 31 '15 at 18:45

2 Answers2

2

Give a period just before bookServlet in the form action. Like below.
action="./bookServlet"

Narendra Jaggi
  • 1,297
  • 11
  • 33
1

give your servlet url path exactly in the <form> tag

greenhorn
  • 594
  • 6
  • 19