栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

处理JSP页面,数组列表时发生异常

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

处理JSP页面,数组列表时发生异常

这是我为使您的应用程序运行所要做的。

FolderStructure(文件夹结构的图像)
ApplicationExample(应用程序运行示例)

创建2个新的Java包

(1)Servlet(2)列表

向列表添加2个类(1)Visit.java(2)VisitListVariables.java向Servlet添加1个类(1)Servlet.java

内部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;      }    }

内部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 {    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 pre.">    @Override  protected void doGet(HttpServletRequest request, HttpServletResponse response)          throws ServletException, IOException {    processRequest(request, response);  }    @Override  protected void doPost(HttpServletRequest request, HttpServletResponse response)          throws ServletException, IOException {    processRequest(request, response);  }    @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(通常,您将拥有一个带有链接的索引页,该链接

<ahref='/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>


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/399241.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号