Java servlet无法与AWS Java SDK一起使用

问题描述 投票:0回答:1

我有一个用JS编写的网页,其中包含几个文本字段和一些按钮。该页面是使用JQuery的Javascript,并且我也有一个Java Servlet。我已经将按钮连接到servlet,并且当通过Eclipse部署到Elastic Beanstalk时,一切工作正常……直到我在servlet中实例化AmazonS3对象。然后,除了调用用于实例化S3对象的函数的按钮之外,其他所有东西仍然可以正常工作。我单击它,它只是不执行任何操作,包括在实例化之前运行代码。当我评论亚马逊产品线时,它可以正常工作。

我想念什么?

这是我的文件:

index.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Program 4</title>
<script type="text/javascript" src ="js/jquery.js"></script>
<script type="text/javascript">
    $(document).ready(function(){
        $('#bttLoad').click(function(){
            $.ajax({
                type:'POST',
                data: {
                    action: 'load'
                },
                url:'AjaxController',
                success: function(result){
                    $('#loadResult').html(result);
                }
            });
        });

        $('#bttClear').click(function(){
            $.ajax({
                type:'POST',
                data: {
                    action: 'clear'
                },
                url:'AjaxController',
                success: function(result){
                    $('#clearResult').html(result);
                }
            });
        });

        $('#bttQuery').click(function(){
            var firstName = $('#firstName').val();
            var lastName = $('#lastName').val();
            $.ajax({
                type:'POST',
                data: {
                    firstName: firstName,
                    lastName: lastName,
                    action: 'query'
                },
                url:'AjaxController',
                success: function(result){
                    $('#queryResult').html(result);
                }
            });
        });
    });
</script>
</head>
<body>

    <fieldset>
        <form>
            <input type="button" value="Load Data" id ="bttLoad">
            <br>
            <span id="loadResult"></span>
        </form>
    </fieldset>

    <fieldset>
        <form>
            <input type="button" value="Clear Data" id ="bttClear">
            <br>
            <span id="clearResult"></span>
        </form>
    </fieldset>

    <fieldset>
        <form>
            First Name <input type="text" id="firstName">
            Last Name <input type="text" id="lastName">
            <input type="button" value="Query" id ="bttQuery">
            <br>
            <span id="queryResult"></span>
        </form>
    </fieldset>

</body>
</html>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>program4</display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  <servlet>
    <description></description>
    <display-name>AjaxController</display-name>
    <servlet-name>AjaxController</servlet-name>
    <servlet-class>controller.AjaxController</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>AjaxController</servlet-name>
    <url-pattern>/AjaxController</url-pattern>
  </servlet-mapping>
</web-app>

最后是AjaxController.java(servlet)

package controller;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;

/**
 * Servlet implementation class AjaxController
 */
public class AjaxController extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public AjaxController() {
        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 {
        response.setContentType("text/plain");
        PrintWriter out = response.getWriter();
        String action = request.getParameter("action");

        if (action.contentEquals("load")) {
            //String fullname = request.getParameter("fullname");
            out.println("load");
        }
        else if (action.contentEquals("clear")) {
            out.println("clear");
            myfunc(response);
            /*
            int number1 = Integer.parseInt(request.getParameter("number1"));
            int number2 = Integer.parseInt(request.getParameter("number2"));
            out.println(number1 + number2);
            myfunc(response);
            */
        }
        else if (action.contentEquals("query")) {
            //perform the query
            out.println("we gonna do da query, mon");
            String firstName = request.getParameter("firstName");
            String lastName = request.getParameter("lastName");
            out.println(firstName + " " + lastName);
        }
        //doGet(request, response);
    }

    public static void myfunc(HttpServletResponse response) throws IOException {
        PrintWriter out = response.getWriter();
        for (int i = 0; i < 100; i++) {
            out.println(i);
        }
        AmazonS3 s3 = AmazonS3ClientBuilder.standard().build();
    }

}
javascript amazon-web-services servlets
1个回答
0
投票

找到了解决方案。

结果是,即使我将jar添加到我的项目中,我也没有将AWS开发工具包上传到Elastic Beanstalk(我不使用Maven)。

我要做的是...

首先,将jar拖放到我在Eclipse中的项目中的WebContent文件夹WEB-INF中,lib。

其次,右键单击该项目,然后转到“构建路径”,“配置构建路径”。在打开的窗口中,选择顶部的“库”选项卡。在右侧,有一些按钮。其中之一是“添加外部JAR”,我之前已经完成过将AWS开发工具包添加到我的项目中的工作。但是我还必须单击“添加JAR”。在弹出的窗口中,导航到前面提到的lib文件夹,然后从此处添加jar。

一旦我这样做并重新部署到Elastic Beanstalk,调用包含AWS代码的函数的按钮现在就可以使用!

© www.soinside.com 2019 - 2024. All rights reserved.