【问题标题】:JSP Page Will Not Open in EclipseJSP 页面无法在 Eclipse 中打开
【发布时间】:2012-09-19 17:23:03
【问题描述】:

好的,所以我遇到了一个奇怪的情况。我基本上创建了这个以前工作的 JSP 页面,或者至少在我运行代码时 Eclipse 会打开一个新选项卡并显示该页面的意义上。但是今天,当我回去查看我创建的表单时,它基本上会闪烁新标签一秒钟然后自动关闭。我在控制台或 tomcat 日志中没有收到任何错误,所以我不确定发生了什么。我尝试在同一个项目中创建一个新的 JSP 文件(没有相同的代码)并正确加载。有没有人遇到过类似的问题?

下面是我的代码...

<%@page import="java.sql.*"%>
<!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>Code Selector</title>
        </head>
        <body> 
            <h1>Please select the applicable codes:</h1> 
            <select name='Code' onchange="showState(this.value)">  
            <option value="none">Select a code</option>  
            <%
                //Pulls the ids and decriptions from the codes table and stores them in the first drop down
                try
                {
                    Class.forName("driverName").newInstance();  
                    Connection con = DriverManager.getConnection("serverURL","username","password");  
                    Statement stmt = con.createStatement();  
                    ResultSet rs = stmt.executeQuery("select id, descr from codes");

                    while(rs.next())
                    {
                        %>
                            <option value="<%=rs.getString(1)%>"><%=rs.getString(1)%> <%=rs.getString(2)%></option>  
                        <%
                    }

                    //Closes the database connection
                    stmt.close();
                    con.close();
                }
                catch (ClassNotFoundException e)
                {
                    System.err.println("ClassNotFoundException: " + e.getMessage());
                } 
                catch (SQLException e)
                {
                    System.err.println("SQLException: " + e.getMessage());
                }
                catch (Exception e)
                {
                    System.err.println("Generic Exception: " + e.getMessage());
                }       
            %>
            </select>  
            <br>
            <br>
            <select name='Code2' onchange="showState(this.value)">  
            <option value="none">Select a code</option>  
            <%
                //Pulls the ids and decriptions from the codes table and stores them in the second drop down
                try
                {
                    Class.forName("driverName").newInstance();  
                    Connection con = DriverManager.getConnection("serverURL","username","password");  
                    Statement stmt = con.createStatement();  
                    ResultSet rs = stmt.executeQuery("select id, descr from codes");

                    while(rs.next())
                    {
                        %>
                            <option value="<%=rs.getString(1)%>"><%=rs.getString(1)%> <%=rs.getString(2)%></option>  
                        <%
                    }

                    //Closes the database connection
                    stmt.close();
                    con.close();
                }
                catch (ClassNotFoundException e)
                {
                    System.err.println("ClassNotFoundException: " + e.getMessage());
                } 
                catch (SQLException e)
                {
                    System.err.println("SQLException: " + e.getMessage());
                }
                catch (Exception e)
                {
                    System.err.println("Generic Exception: " + e.getMessage());
                }       
             %>
            </select>
            <br>
            <br>
            <select name='otherCode' onchange="showState(this.value)">  
            <option value="none">Select a other code</option>  
            <%

                //Pulls the ids and decriptions from the other codes table and stores them in the third drop down
                try
                {
                    Class.forName("driverName").newInstance();  
                    Connection con = DriverManager.getConnection("serverURL","username","password");  
                    Statement stmt = con.createStatement();
                    ResultSet rs2 = stmt.executeQuery("select id, descr from other_codes");

                    while(rs2.next())
                    {
                        %>
                            <option value="<%=rs2.getString(1)%>"><%=rs2.getString(1)%> <%=rs2.getString(2)%></option>  
                        <%
                    }

                    //Closes the database connection
                    stmt.close();
                    con.close();
                }
                catch (ClassNotFoundException e)
                {
                    System.err.println("ClassNotFoundException: " + e.getMessage());
                } 
                catch (SQLException e)
                {
                    System.err.println("SQLException: " + e.getMessage());
                }
                catch (Exception e)
                {
                    System.err.println("Generic Exception: " + e.getMessage());
                }       
            %>
      </select>
      <br> 
      <br>
      <form method = "post">
        <input type="submit" value="Submit">
        <%
            try
            {
                String Code = request.getParameter("Code");
                String Code2 = request.getParameter("Code2");
                String otherCode = request.getParameter("otherCode");

                Class.forName("driverName").newInstance();  
                Connection con = DriverManager.getConnection("serverURL","username","password");  
                Statement stmt = con.createStatement();
                //ResultSet rs3 = stmt.executeQuery();

                System.out.println("This is the first code: " + Code);
                System.out.println("This is the second code: " + Code2);
                System.out.println("This is the other code: " + otherCode);

                con.close();
                stmt.close();

            }
            catch (ClassNotFoundException e)
            {
                System.err.println("ClassNotFoundException: " + e.getMessage());
            } 
            catch (SQLException e)
            {
                System.err.println("SQLException: " + e.getMessage());
            }
            catch (Exception e)
            {
                System.err.println("Generic Exception: " + e.getMessage());
            } 
        %>
        <script>
            window.close();
        </script>
      </form>
      </body> 
</html>

【问题讨论】:

  • 嗯,这听起来可能很傻,但我在末尾看到一个脚本标签,上面写着“window.close()”。你会不小心绊倒吗?我认为脚本应该在标题而不是正文中声明?
  • 我想也可能是这样,但我尝试创建一个新项目,取出它,以及代码的整个&lt;form&gt; 部分,但我仍然遇到同样的问题。此外,它在今天之前与 window.close() 合作过。
  • 我明白了,你知道发生了什么变化吗?我所知道的是,我很确定脚本应该放在标题中。我只能假设您以某种方式提交表单。
  • 我想知道您的 onChange 方法是否是通过添加新选项来触发的?您应该能够在 Eclipse 中使用断点来调试您的页面。我建议在事件处理程序中放置一个断点并提交方法以查看页面关闭的位置。

标签: java html database eclipse jsp


【解决方案1】:

嗯,这很愚蠢。所以我用相同的代码创建了另一个新的jsp文件,除了&lt;script&gt;标签中的代码,一切正常。然后我重新添加了&lt;script&gt; 标签,它第一次运行良好。然而,下次我去尝试时,原来的问题又出现了。然后当我再次删除&lt;script&gt; 标签时,问题仍然存在。所以不管是什么原因,似乎一旦你添加了window.close(),它就会永久留在页面上?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-04-11
    • 1970-01-01
    • 2012-08-28
    • 1970-01-01
    • 1970-01-01
    • 2022-07-24
    • 2011-04-04
    • 2023-03-19
    相关资源
    最近更新 更多