ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

Java-HTTP错误404请求的资源不可用,Glassfish

2019-11-20 17:03:31  阅读:228  来源: 互联网

标签:glassfish java


这个问题已经在这里有了答案:            >            Servlet returns “HTTP Status 404 The requested resource (/servlet) is not available”                                    7个
每当我按下“提交操作”按钮将信息发送到控制器时,我总是收到“ HTTP 404状态-找不到:所请求的资源不可用”,我只是想知道是否有什么可以帮助我解决这个令人沮丧的问题.这是我的代码:

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>

解决方法:

在formServlet的bookServlet之前给一个句点.像下面.

    action =“ ./ bookServlet”

标签:glassfish,java
来源: https://codeday.me/bug/20191120/2045444.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有