我无法弄清楚这个问题。
我的错误:
org.apache.jasper.JasperException:处理发生异常 第28行的JSP页面/index.jsp
Line 28: <c:forEach items="${data.visit}" var="visit">
java class:
public class DataBean implements Serializable, ServletContextListener {
private static final String nameOfLogger = DataBean.class.getName();
private static final Logger logger = Logger.getLogger(nameOfLogger);
public class Visit {
public Visit(String dateOfTheVisit, String category, String idClient, String idInsrurer, String idDoctor, String idVisit,String accepted) {
this.dateOfTheVisit = dateOfTheVisit;
this.category = category;
this.idClient = idClient;
this.idInsrurer = idInsrurer;
this.idDoctor = idDoctor;
this.idVisit = idVisit;
this.accepted = accepted;
}
public String getIdVisit() {
return idVisit;
}
public void setIdVisit(String idVisit) {
this.idVisit = idVisit;
}
public String getDateOfTheVisit() {
return dateOfTheVisit;
}
public void setDateOfTheVisit(String dateOfTheVisit) {
this.dateOfTheVisit = dateOfTheVisit;
}
public String getCategory() {
return category;
}
public void setCategory(String category) {
this.category = category;
}
public String getIdClient() {
return idClient;
}
public void setIdClient(String idClient) {
this.idClient = idClient;
}
public String getIdInsrurer() {
return idInsrurer;
}
public void setIdInsrurer(String idInsrurer) {
this.idInsrurer = idInsrurer;
}
public String getIdDoctor() {
return idDoctor;
}
public void setIdDoctor(String idDoctor) {
this.idDoctor = idDoctor;
}
String idVisit;
String dateOfTheVisit;
String category;
String idClient;
String idInsrurer;
String idDoctor;
String accepted;
public String getAccepted() {
return accepted;
}
public void setAccepted(String accepted) {
this.accepted = accepted;
}
}
public class Insurer {
public Insurer(String idInsurer, String name) {
this.idInsurer = idInsurer;
this.name = name;
}
public String getIdInsurer() {
return idInsurer;
}
public void setIdInsurer(String idInsurer) {
this.idInsurer = idInsurer;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
String idInsurer;
String name;
}
//public List<Visit> visitArray = new ArrayList<>();
public List<Insurer> insurerArray = new ArrayList<>();
private java.sql.Connection psqlCon = null;
private boolean psqlConnectionCreated = false;
synchronized public ArrayList<Visit> getVisit() throws ClassNotFoundException, SQLException {
Connection conn = DriverManager.getConnection("jdbc:postgresql://localhost:5432/postgres", "postgres", "password");
Statement stm;
stm = conn.createStatement();
String sql = "Select * From Customer";
ResultSet rst;
rst = stm.executeQuery(sql);
ArrayList<Visit> visitArray = new ArrayList<>();
while (rst.next()) {
Visit visit = new Visit(rst.getString("dateOfTheVisit"), rst.getString("category"), rst.getString("idClient"), rst.getString("idInsurer"), rst.getString("idDoctor"), rst.getString("idVisit"),rst.getString("accepted"));
visitArray.add(visit);
}
return visitArray;
}
@Override
public void contextInitialized(ServletContextEvent sce) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public void contextDestroyed(ServletContextEvent sce) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
}
JSP:
<%@page import="java.util.List"%>
<HTML>
<HEAD>
</HEAD>
<BODY>
<jsp:useBean id="data" class="sevenet.DataBean" scope="application"/>
<H1>The tableName Database Table </H1>
<TABLE BORDER="1">
<TR>
<TH>Date</TH>
<TH>Category</TH>
<TH>IdClient</TH>
<TH>IdInsurer</TH>
<TH>IdDoctor</TH>
<TH>Accepted</TH>
<TH>ID</TH>
</TR>
<tbody>
<c:forEach items="${data.visit}" var="visit">
<tr>
<td><c:out value="${visit.dateOfTheVisit}"/></td>
<td><c:out value="${visit.category}"/></td>
<td><c:out value="${visit.idClient}"/></td>
<td><c:out value="${visit.idInsrurer}"/></td>
<td><c:out value="${visit.idDoctor}"/></td>
<td><c:out value="${visit.idVisit}"/></td>
<td><c:out value="${visit.accepted}"/></td>
</tr>
</c:forEach>
</tbody>
</TABLE>
</BODY>
</HTML>
我不确定我做错了什么,但我认为这与bean的定义有关。
我感谢每一次帮助的尝试!
答案 0 :(得分:0)
你的代码非常棘手!!
1)getVisit()方法的调用者是谁? 2)为什么你创建了一个内部声明的内部类的DataBean类? 3)您正在返回一个ArrayList visitArray = new ArrayList&lt;&gt;(),而不是DataBean!
因此,首先,DataBean类是无用的 其次,在访问课程中,不要只使用Insrured id,而是使用Insurer实例。 这是代码示例:
public class Visit {
public Visit(String dateOfTheVisit, String category, String idClient, Insurer insurer, String idDoctor, String idVisit,String accepted) {
this.dateOfTheVisit = dateOfTheVisit;
this.category = category;
this.idClient = idClient;
this.insurer = insurer;
this.idDoctor = idDoctor;
this.idVisit = idVisit;
this.accepted = accepted;
}
public String getIdVisit() {
return idVisit;
}
public void setIdVisit(String idVisit) {
this.idVisit = idVisit;
}
public String getDateOfTheVisit() {
return dateOfTheVisit;
}
public void setDateOfTheVisit(String dateOfTheVisit) {
this.dateOfTheVisit = dateOfTheVisit;
}
public String getCategory() {
return category;
}
public void setCategory(String category) {
this.category = category;
}
public String getIdClient() {
return idClient;
}
public void setIdClient(String idClient) {
this.idClient = idClient;
}
public String getIdDoctor() {
return idDoctor;
}
public void setIdDoctor(String idDoctor) {
this.idDoctor = idDoctor;
}
String idVisit;
String dateOfTheVisit;
String category;
String idClient;
String idInsrurer;
String idDoctor;
String accepted;
Insurer insurer;
public String getAccepted() {
return accepted;
}
public void setAccepted(String accepted) {
this.accepted = accepted;
}
}
公共级保险人{
public Insurer(String idInsurer, String name) {
this.idInsurer = idInsurer;
this.name = name;
}
public String getIdInsurer() {
return idInsurer;
}
public void setIdInsurer(String idInsurer) {
this.idInsurer = idInsurer;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
String idInsurer;
String name;
}
现在您应该创建一个servlet来显示数据,请查看本教程:http://www.mkyong.com/servlet/a-simple-servlet-example-write-deploy-run/
注意在ServletDemo1类中,方法名必须是doGet(而不是doGe,这是一个简单的数字错误)。
所以,在你的ServletDemo1中,在doGet方法中,你必须对你的getVisit()执行相同的逻辑,然后,在返回之前,你必须添加它:
request.setAttribute("visitList",visitArray);
而是返回列表,发送到你的jsp路径,如下所示:
request.getRequestDispatcher("/index.jsp").forward(request, response);
现在,在你的jsp中,删除
<jsp:useBean id="data" class="sevenet.DataBean" scope="application"/>
并像这样纠正每个人:
<c:forEach items="${visitList}" var="visit">
而不是
<td><c:out value="${visit.idInsrurer}"/></td>
把
<td><c:out value="${visit.insrurer.idInsurer}"/></td>
我没有尝试过代码,但如果您有任何疑问,请不要犹豫!
答案 1 :(得分:0)
您需要使用servlet来获取数据并将数据发送到jsp并设置会话变量。用你的班级做你的逻辑。
为每个列表创建一个单独的getter和setter类
在新班级
public class VisitListVariables{
private String dateOfTheVisit;
private String category;
private String idClient;
private String idInsrurer;
private String idDoctor;
private String idVisit;
private String accepted;
public String getDateOfTheVisit(){
return dateOfTheVisit;
}
public void setDateOfTheVisit(String dateOfTheVisit){
this.dateOfTheVisit = dateOfTheVisit;
}
...(More Getter and Setters)...
}
访问课程
public class Visit{
public static List<VisitListVariables> BuildVisitList(String dateOfTheVisit, String category, String idClient, String idInsrurer, String idDoctor, String idVisit,String accepted) {
List<VisitListVariables> BuildVisitList = new ArrayList<>();
//database connection here
VisitListVariables buildList = new VisitListVariables();
while(rs.next()){
String visitDate = rs.getString("visitDate"); //gets variable from database
buildList.setDateOfTheVisit(visitDate);
BuildVisitList.add(buildList);
}
return BuildVisitList;
}
}
//在新Servlet中 将访问放在会话中,您可以使用JSTL获取它。
protected void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
visit = Visit.BuildVisitList(dateOfTheVisit,category,idClient, idInsrurer,idDoctor,idVisit,accepted);
request.setAttribute("visitParam", visit); // Will be available as ${visitParam} in JSP
request.getRequestDispatcher("/PageName.jsp").forward(request, response);
}
你的jsp
<c:forEach items="${visitParam}" var="visit">
<tr>
<td><c:out value="${visit.dateOfTheVisit}"/></td>
<td><c:out value="${visit.category}"/></td>
<td><c:out value="${visit.idClient}"/></td>
<td><c:out value="${visit.idInsrurer}"/></td>
<td><c:out value="${visit.idDoctor}"/></td>
<td><c:out value="${visit.idVisit}"/></td>
<td><c:out value="${visit.accepted}"/></td>
</tr>
</c:forEach>
答案 2 :(得分:0)
首先,非常感谢您的回答。 我根据第一个答案修改了代码,但是我遇到了一些麻烦。
的java:
public class DataBean{
private static final String nameOfLogger = DataBean.class.getName();
private static final Logger logger = Logger.getLogger(nameOfLogger);
public class VisitListVariables{
private String dateOfTheVisit;
private String category;
private String idClient;
private String idInsrurer;
private String idDoctor;
private String idVisit;
private String accepted;
public String getDateOfTheVisit() {
return dateOfTheVisit;
}
public void setDateOfTheVisit(String dateOfTheVisit) {
this.dateOfTheVisit = dateOfTheVisit;
}
public String getCategory() {
return category;
}
public void setCategory(String category) {
this.category = category;
}
public String getIdClient() {
return idClient;
}
public void setIdClient(String idClient) {
this.idClient = idClient;
}
public String getIdInsrurer() {
return idInsrurer;
}
public void setIdInsrurer(String idInsrurer) {
this.idInsrurer = idInsrurer;
}
public String getIdDoctor() {
return idDoctor;
}
public void setIdDoctor(String idDoctor) {
this.idDoctor = idDoctor;
}
public String getIdVisit() {
return idVisit;
}
public void setIdVisit(String idVisit) {
this.idVisit = idVisit;
}
public String getAccepted() {
return accepted;
}
public void setAccepted(String accepted) {
this.accepted = accepted;
}
}
private java.sql.Connection psqlCon = null;
private boolean psqlConnectionCreated = false;
public static class Visit{
public static List<VisitListVariables> BuildVisitList(String dateOfTheVisit, String category, String idClient, String idInsrurer, String idDoctor, String idVisit,String accepted) throws SQLException {
List<VisitListVariables> BuildVisitList = new ArrayList<>();
Connection conn = DriverManager.getConnection("jdbc:postgresql://localhost:5432/postgres", "postgres", "password");
Statement stm;
stm = conn.createStatement();
String sql = "Select * From Customer";
ResultSet rs;
rs = stm.executeQuery(sql);
ArrayList<Visit> visitArray = new ArrayList<>();
BuildSearch buildList = new BuildSearch();
while(rs.next()){
String visitDate = rs.getString("visitDate"); //gets variable from database
buildList.setDateOfTheVisit(visitDate);
BuildVisitList.add(buildList);
}
return BuildVisitList;
}
public class Insurer {
public Insurer(String idInsurer, String name) {
this.idInsurer = idInsurer;
this.name = name;
}
public String getIdInsurer() {
return idInsurer;
}
public void setIdInsurer(String idInsurer) {
this.idInsurer = idInsurer;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
String idInsurer;
String name;
}
}
}
的servlet:
public class Servlet extends HttpServlet{
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
visit = Visit.BuildVisitList(dateOfTheVisit, category, idClient, idInsrurer, idDoctor, idVisit, accepted);
request.setAttribute("visitParam", visit); // Will be available as ${visitParam} in JSP
request.getRequestDispatcher("index.jsp").forward(request, response);
}
}
JSP:
<HTML>
<HEAD>
<TITLE>The tableName Database Table </TITLE>
</HEAD>
<BODY>
<jsp:useBean id="data" class="sevenet.DataBean" scope="application"/>
<H1>The tableName Database Table </H1>
<TABLE BORDER="1">
<TR>
<TH>Date</TH>
<TH>Category</TH>
<TH>IdClient</TH>
<TH>IdInsurer</TH>
<TH>IdDoctor</TH>
<TH>Accepted</TH>
<TH>ID</TH>
</TR>
<tbody>
<c:forEach items="${visitParam}" var="visit">
<tr>
<td><c:out value="${visit.dateOfTheVisit}"/></td>
<td><c:out value="${visit.category}"/></td>
<td><c:out value="${visit.idClient}"/></td>
<td><c:out value="${visit.idInsrurer}"/></td>
<td><c:out value="${visit.idDoctor}"/></td>
<td><c:out value="${visit.idVisit}"/></td>
<td><c:out value="${visit.accepted}"/></td>
</tr>
</c:forEach>
</tbody>
</TABLE>
</BODY>
如何声明BuildSearch()? 这种方法应该是什么?
Servlet找不到符号访问,如何解决? 我知道这可能是基本问题,但它是我的第一个带数据库,Web服务的java项目..
答案 3 :(得分:0)
以下是我为了让您的应用运行所做的工作。
FolderStructure(文件夹结构图片) ApplicationExample(App运行示例)
创建2个新的Java包
(1)小服务程序 (2)列表
向列表添加2个类(1)Visit.java(2)VisitListVariables.java 将1个类添加到Servlet(1)Servlet.java
Inside Visit.java
package lists;
import java.util.ArrayList;
import java.util.List;
public class Visit{
public static List<VisitListVariables> BuildVisitList(){
List<VisitListVariables> BuildVisitList = new ArrayList<>(); //creates List to return
try{
//Database connection variables
//Connection conn = DriverManager.getConnection("jdbc:postgresql://localhost:5432/postgres", "postgres", "password");
//Statement stm;
//stm = conn.createStatement();
//String sql = "Select * From Customer";
//ResultSet rs;
//rs = stm.executeQuery(sql);
//while(rs.next()){
VisitListVariables buildList = new VisitListVariables(); //Gets variables needed for list
//String visitDate = rs.getString("visitDate");
String dateOfTheVisit = "today";
buildList.setDateOfTheVisit(dateOfTheVisit);
buildList.setCategory("Doctor Visit");
buildList.setIdClient("666");
buildList.setIdInsrurer("999");
buildList.setIdDoctor("1001");
buildList.setIdVisit("001");
buildList.setAccepted("yes");
BuildVisitList.add(buildList);
//}
//rs.close();stm.close();conn.close();
}catch(Exception e){}
return BuildVisitList;
}
}
Inside VisitListVariables.java
package lists;
public class VisitListVariables {
private String dateOfTheVisit;
private String category;
private String idClient;
private String idInsrurer;
private String idDoctor;
private String idVisit;
private String accepted;
public String getDateOfTheVisit() {
return dateOfTheVisit;
}
public void setDateOfTheVisit(String dateOfTheVisit) {
this.dateOfTheVisit = dateOfTheVisit;
}
public String getCategory() {
return category;
}
public void setCategory(String category) {
this.category = category;
}
public String getIdClient() {
return idClient;
}
public void setIdClient(String idClient) {
this.idClient = idClient;
}
public String getIdInsrurer() {
return idInsrurer;
}
public void setIdInsrurer(String idInsrurer) {
this.idInsrurer = idInsrurer;
}
public String getIdDoctor() {
return idDoctor;
}
public void setIdDoctor(String idDoctor) {
this.idDoctor = idDoctor;
}
public String getIdVisit() {
return idVisit;
}
public void setIdVisit(String idVisit) {
this.idVisit = idVisit;
}
public String getAccepted() {
return accepted;
}
public void setAccepted(String accepted) {
this.accepted = accepted;
}
}
内部Servlet.java
package Servlet;
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import lists.Visit;
import lists.VisitListVariables;
public class Servlet 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 {
List<VisitListVariables> visit = Visit.BuildVisitList();
request.setAttribute("visitParam", visit); // Will be available as ${visitParam} in JSP
request.getRequestDispatcher("/test.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>
}
在jsp中我命名为test.jsp
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<TITLE>The tableName Database Table </TITLE>
</head>
<body>
<H1>The tableName Database Table </H1>
<table BORDER="1">
<tr>
<th>Date</th>
<th>Category</th>
<th>IdClient</th>
<th>IdInsurer</th>
<th>IdDoctor</th>
<th>Accepted</th>
<th>ID</th>
</tr>
<tbody>
<c:forEach items="${visitParam}" var="visit">
<tr>
<td><c:out value="${visit.dateOfTheVisit}"/></td>
<td><c:out value="${visit.category}"/></td>
<td><c:out value="${visit.idClient}"/></td>
<td><c:out value="${visit.idInsrurer}"/></td>
<td><c:out value="${visit.idDoctor}"/></td>
<td><c:out value="${visit.idVisit}"/></td>
<td><c:out value="${visit.accepted}"/></td>
</tr>
</c:forEach>
</tbody>
</table>
</body>
</html>
Web.xml(通常你会有一个带链接的索引页面
<a href='/Servlet'>Visits</a>
这将指向您的servlet,但是为了测试,您可以将servlet键入url // localhost:8080 / ProjectName / Servlet)
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
<servlet>
<servlet-name>Servlet</servlet-name>
<servlet-class>Servlet.Servlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Servlet</servlet-name>
<url-pattern>/Servlet</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>Servlet</welcome-file>
</welcome-file-list>
</web-app>