Java,JSP,Eclipse-服务器遇到意外状况,阻止其满足请求

时间:2019-09-19 11:15:06

标签: java eclipse jsp

我正在使用Eclipse创建“动态Web项目”。而且我通过MVC结构编写了一些简单的代码。

我的 LoaiBean 类:

    private void RetrieveFromDatabase(int index)
    {
        Debug.Log($"{index} started...");
        FirebaseDatabase.DefaultInstance.GetReference("/Teams/" + TeamsSelection.teamSelected + "/").Child(index.ToString()).Child("userPosition").GetValueAsync()
           .ContinueWith(task => 
           {
               if (task.IsFaulted)
               {
                   //handle error
               }
               else if(task.IsCompleted)
               {
                   DataSnapshot snapshot = task.Result;
                   OnResponseGot(snapshot, index);
                   return;
               }
           });
    }

    private void OnResponseGot(DataSnapshot dataSnapshot, int index)
    {
        if (dataSnapshot == null || int.Parse(dataSnapshot.Value.ToString()) < 10)
        {
            Debug.Log("Trying again to retrieve " + index);
            RetrieveFromDatabase(index);
        }
        else
        {
            int userPos = int.Parse(dataSnapshot.Value.ToString());
            Debug.Log("retrieved userPos " + userPos);
            StartCoroutine(FetchWithUWR(userPos, index));
            //FetchWithResourceLoad(userPos, index);
        }
    }

    IEnumerator FetchWithUWR(int userPos, int index)
    {
        Debug.Log("Starting uwr for audioFiles");
        using (UnityWebRequest uwr = UnityWebRequestMultimedia.GetAudioClip($"C:/Users/Domas/Desktop/Projects/Wild_West/Assets/Resources/Audio/WAV_ENG/D{userPos}a", AudioType.WAV))
        {
            yield return uwr.SendWebRequest();
            Debug.Log("yielded a file!");
            if (uwr.isNetworkError)
            {
                Debug.Log(uwr.error);
            }
            else
            {
                AudioClip clip = DownloadHandlerAudioClip.GetContent(uwr);
                Debug.Log("Starting Coroutine " + index);
                StartCoroutine(PlayAudioClipAndStartRetrievingFromDatabase(index, clip));
            }
        }
    }

我的 LoaiDao 班:

package bean;
public class LoaiBean {
    private String maLoai;
    private String tenLoai;
    public String getMaLoai() {
        return maLoai;
    }
    public void setMaLoai(String maLoai) {
        this.maLoai = maLoai;
    }
    public String getTenLoai() {
        return tenLoai;
    }
    public void setTenLoai(String tenLoai) {
        this.tenLoai = tenLoai;
    }
    public LoaiBean(String maLoai, String tenLoai) {
        super();
        this.maLoai = maLoai;
        this.tenLoai = tenLoai;
    }
}

我的 DBConnection 类:

package dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import bean.LoaiBean;
public class LoaiDao {
    private DBConnection dbConnection = null;
    public LoaiDao() {
        super();
        this.dbConnection = new DBConnection("bookdb");
    }
    public LoaiDao(DBConnection dbConnection) {
        super();
        this.dbConnection = dbConnection;
    }
    /**
     * get list of categories
     * @return {@link ArrayList}
     */
    public ArrayList<LoaiBean> getLoai(){
        Connection connection = this.dbConnection.getConnection(true);
        ArrayList<LoaiBean> loais = new ArrayList<LoaiBean>();
        try {
            Statement stmt = connection.createStatement();
            ResultSet rs =  stmt.executeQuery("select * from loai");
            while(rs.next()) {
                loais.add(new LoaiBean(
                    rs.getString("maloai"),
                    rs.getString("tenloai")
                ));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return loais;
    }
}

我的 LoaiBo 课:

package dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class DBConnection {
    private String server = "localhost";
    private String port = "1433";
    private String databaseName = null;
    private String user = "sa";
    private String password = "123";
    private Connection connection = null;
    public String getServer() {
        return server;
    }
    public void setServer(String server) {
        this.server = server;
    }
    public String getPort() {
        return port;
    }
    public void setPort(String port) {
        this.port = port;
    }
    public String getDatabaseName() {
        return databaseName;
    }
    public void setDatabaseName(String databaseName) {
        this.databaseName = databaseName;
    }
    public String getUser() {
        return user;
    }
    public void setUser(String user) {
        this.user = user;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    public Connection getConnection(boolean autoConnect) {
        if(autoConnect && this.connection == null) {
            this.connect();
        }
        return connection;
    }
    public void setConnection(Connection connection) {
        this.connection = connection;
    }
    public DBConnection(String databaseName) {
        super();
        this.databaseName = databaseName;
    }
    public void connect() {
        if(this.databaseName == null) {
            return;
        }
        try {
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            String st = "jdbc:sqlserver://"+this.server+":"+this.port+";databaseName="+this.databaseName+";user="+this.user+";password="+this.password;
            try {
                this.connection = DriverManager.getConnection(st);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
    public void reconnect() {
        this.disconnect();
        this.connect();
    }
    public void disconnect() {
        if(this.connection != null) {
            try {
                this.connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}

我的JSP文件 tc.jsp

package bo;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import bean.LoaiBean;
import dao.LoaiDao;
public class LoaiBo {
    private LoaiDao dao = null;
    public LoaiDao getDao() {
        return dao;
    }
    public void setDao(LoaiDao dao) {
        this.dao = dao;
    }
    public LoaiBo() {
        super();
        this.dao = new LoaiDao();
    }
    public LoaiBo(LoaiDao dao) {
        super();
        this.dao = dao;
    }
    public ArrayList<LoaiBean> getLoai(){
        return this.dao.getLoai();
    }
}

当我在上面运行文件tc.jsp时,出现错误: enter image description here

<%@page import="bean.LoaiBean"%>
<%@page import="java.util.ArrayList"%>
<%@page import="bo.LoaiBo"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
</head>
<body>
    <%
    LoaiBo bo = new LoaiBo();
    ArrayList<LoaiBean> loais = bo.getLoai(); // error is showed here
    %>
    <div class="row justify-content-center">
        <div class="col-md-4 col-sm-6 col-xs-12">
            <ul class="list-group">
                <% for (LoaiBean loaiBean : loais) { %>
                <li class="list-group-item"><%= loaiBean.getMaLoai()  %></li>
                <% } %>
            </ul>       
        </div>
    </div>
</body>
</html>

当我通过具有主要功能的java核心类进行测试时,这些功能可以正常运行,但不能在jsp文件中运行。

我正在使用Eclipse,tomcat 9,java 8。

我不知道如何解决此问题。希望大家能帮助我。谢谢。

1 个答案:

答案 0 :(得分:0)

您可以尝试如下所示从LoaiDao.class获取列表对象

<%
    LoaiDao loaiDao = new LoaiDao();
    ArrayList<LoaiBean> loais = loaiDao.getLoai(); 
%>