java web Restful HTTP 404 Not Found

时间:2018-04-20 04:15:54

标签: java web-services jsp restful-architecture

java web客户端。

这是index.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Calcular factorial</title>
</head>
<body>
    <div align="center">
        <form action="calcular.do" style="font-family:arial">
        Numero :<br>
        <input type="text" name="base" style="text-align:right"/><br><br>
        <input type="submit" value="calcular" name="Calcular" />
        <br><br>
        Resultado:<br>
        <input type="text" name="resultado" value="${result}"
               style="text-align:right"/><br><br>
        </form>
    </div>           
 </body>
</html>

这是Calcular.java

 package controlador;

 import Webservicce.ClientRest;
 import java.io.IOException;
 import java.io.PrintWriter;
 import javax.servlet.ServletException;
 import javax.servlet.annotation.WebServlet;
 import javax.servlet.http.HttpServlet;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;

/**
*
* @author x2010s
*/
@WebServlet(name = "Calcular", urlPatterns = {"/calcular.do"})
public class Calcular extends HttpServlet {

/**
 * Processes requests for both HTTP <code>GET</code> and   <code>POST</code>
 * methods.
 *
 * @param request servlet request
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    ClientRest rest = new ClientRest();
    String base = request.getParameter("base");
    String factorial = rest.factorial(base);
    request.setAttribute("factorial",factorial);
    request.getRequestDispatcher("index.jsp").forward(request, response);

 }

 // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
 * Handles the HTTP <code>GET</code> method.
 *
 * @param request servlet request
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    processRequest(request, response);
}

/**
 * Handles the HTTP <code>POST</code> method.
 *
 * @param request servlet request
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    processRequest(request, response);
}

/**
 * Returns a short description of the servlet.
 *
 * @return a String containing servlet description
 */
@Override
public String getServletInfo() {
    return "Short description";
}// </editor-fold>

}
在servlet中,必须从restful中调用factorial方法,用输入的数字计算factorial

这是导入的RESTful Java客户端

package Webservicce;

import javax.ws.rs.ClientErrorException;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.WebTarget;

public class ClientRest {

private WebTarget webTarget;
private Client client;
private static final String BASE_URI = "http://localhost:8080/SimpleRESTweb/webresources";

public ClientRest() {
    client = javax.ws.rs.client.ClientBuilder.newClient();
    webTarget = client.target(BASE_URI).path("factorial");
}

public String factorial(String base) throws ClientErrorException {
    WebTarget resource = webTarget;
    if (base != null) {
        resource = resource.queryParam("base", base);
    }
    return resource.request().get(String.class);
}

public void close() {
    client.close();
}

}

当我输入数字并点击计算时,它会出现

HTTP状态500 - 内部服务器错误

输入例外报告

消息内部服务器错误

说明服务器遇到内部错误,导致无法完成此请求。

例外 javax.ws.rs.NotFoundException:找不到HTTP 404

错误出现在java Web客户端中。我找不到错误,因为它不计算因子

0 个答案:

没有答案