如何将数据库数据从JAVA servlet发送到JSP?

时间:2016-04-17 08:35:00

标签: java mysql jsp servlets model-view-controller

我正在开发一个基于MVC的应用程序,用于从MYSQL数据库中获取数据并将其显示在JSP上。我使用servlet作为控制器,JSP作为视图。但是,在请求对象中添加ArrayList作为属性后,我无法在JSP上显示它。请帮忙。

包含获取数据按钮的JSP,index.jsp:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Student Management System</title>
</head>
<body>
<center>
<form name="mainForm" action="localhost:8080/Student_Management_System/StudentController" method="post">
<h1>Student Management System</h1>
<table cellpadding="5" border="1">
<tr>
<td><input type="submit" name="viewAll" value="View all records" style="width:145px;height:30px"></td>
</tr>
</table>
</form>
</center>
</body>
</html>

控制器servlet,StudentController.java:

package Controller;

import java.io.IOException;
import java.util.ArrayList;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import Service.StudentDAO;
import Service.StudentDAOImpl;

/**
 * Servlet implementation class StudentController
 */
@WebServlet("/StudentController")
public class StudentController extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public StudentController() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        String viewAll=request.getParameter("viewAll");
        RequestDispatcher dispatcher=null;
        StudentDAO obj=new StudentDAOImpl();
        if(viewAll!=null)
        {
            ArrayList<String> rs=obj.viewAll();
            request.setAttribute("result", rs);
        }
        dispatcher=request.getRequestDispatcher("/WebContent/table.jsp");
        dispatcher.forward(request, response);
    }
}

界面,StudentDAO.java:

package Service;

import java.util.ArrayList;

public interface StudentDAO {
    public ArrayList<String> viewAll();
}

实现类StudentDAOImpl.java:

package Service;

import java.sql.*;
import java.util.ArrayList;

public class StudentDAOImpl implements StudentDAO {

    static Connection con=null;
    static Statement stmt=null;
    static ResultSet rs=null;
    static ArrayList<String> result=new ArrayList<String>();
    static int n=0;
    @Override
    public ArrayList<String> viewAll() {
        // TODO Auto-generated method stub
        try{
            con=DriverManager.getConnection("jdbc:mysql://localhost:3306/demo?useSSL=false", "root", "password");
            stmt=con.createStatement();
            rs=stmt.executeQuery("select * from demotable");
            n=rs.getMetaData().getColumnCount();
            while(rs.next()){
                for(int i=1;i<=n;i++){
                    result.add(rs.getString(i));
                }
            }
        }
        catch(Exception e){
            result.add("Couldn't connect to database!");
        }
        return result;
    }
}

要显示数据的JSP,table.jsp:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
    <%@ page import="java.util.ArrayList" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Table displaying the data</title>
</head>
<body>
<table>
    <%
        ArrayList<String> rs=(ArrayList<String>)request.getAttribute("result"); 
        %> <tr><%for(String s: rs){ %>
        <td><%=s%></td>
        </tr>
        <%
        }
    %>
</table>
</body>
</html>

P.S。:数据库名称为“demo”,表名为“demotable”。该表由两列组成,“Name”和“RollNo”。用户名是“root”,密码是“password”。我也在项目中导入了JAVA连接JAR。

0 个答案:

没有答案