首页 > 解决方案 > jsp没有返回任何东西,只是一个空白屏幕

问题描述

下面的 jsp 代码在 Web 浏览器上只返回一个空白屏幕……怎么办?


<%@page import="java.sql.*"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>GAURAV GOSWAMI</title>
        <%
            try
            {
                Class.forName("java.sql.DriverManager");
                Connection con =(Connection)DriverManager.getConnection("jdbc:mysql://localhost:3306/quiz","root","");
                Statement stmt = con.createStatement();
                String query = "select * from qa";
                ResultSet rs = stmt.executeQuery(query);
                while(rs.next())
                {
        %>

        <p><br><%rs.getString(0);%></p>
        <p><br><%rs.getString(1);%></p>
        <p><br><%rs.getString(2);%></p>
        <p><br><%rs.getString(3);%></p>
        <p><br><%rs.getString(4);%></p>

        <%
                }
            }
            catch(Exception e)
            {
        %>

                <br><%e.getMessage();%>
        <%
            }
        %>

    </head>
</html>

标签: javahtmljsp

解决方案


你在头部写正文

将您的代码移动到正文部分,如下所示:

<%@page import="java.sql.*"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>GAURAV GOSWAMI</title>
    </head> <!-- Add this line -->
    <body>  <!-- Add this line -->
    <%
        try
        {
            Class.forName("java.sql.DriverManager");
            Connection con =(Connection)DriverManager.getConnection("jdbc:mysql://localhost:3306/quiz","root","");
            Statement stmt = con.createStatement();
            String query = "select * from qa";
            ResultSet rs = stmt.executeQuery(query);
            while(rs.next())
            {
    %>

    <p><br><%rs.getString(0);%></p>
    <p><br><%rs.getString(1);%></p>
    <p><br><%rs.getString(2);%></p>
    <p><br><%rs.getString(3);%></p>
    <p><br><%rs.getString(4);%></p>

    <%
            }
        }
        catch(Exception e)
        {
    %>

            <br><%e.getMessage();%>
    <%
        }
    %>
    </body> <!-- Add this line -->
    <!-- **** </head> *** Remove this line -->
</html>

希望对你有帮助,再见。

PS:我已经标记了<!-- Add this line -->要更改的行之类的注释。


推荐阅读