在单击提交按钮之前阻止jsp运行

时间:2012-11-30 03:56:38

标签: jsp

我有一个带有表单的jsp页面。

<form action="search.jsp" method="get">
<input type="submit" value="submit" />
</form>

在表单下面,我将建立一个基于数据库查询的表。问题是,页面加载时输出标题。是否可以等到按下提交按钮以运行查询/ jsp?

<%
try{
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("connection");
Statement st=con.createStatement();
ResultSet rs=st.executeQuery("query here);
%>

<h2>Something <% out.print(something); %> with something <% out.print(something); %> </h2>

<%
while (rs.next()) {

        output =rs.getString(1) + "  "  + rs.getString(2)  + " something "  + rs.getString(3) ;
        %>
        <tr>
        <td>  
        <%

        out.println(output);

       %>
       </td>
       </tr>           
       <%
}

con.close();

}
catch(Exception e) {
    System.out.println(e);
}
%>

1 个答案:

答案 0 :(得分:1)

假设您要提交到同一页面

为提交按钮添加名称

    <input type="submit" value="submit" name="submit />

然后仅在参数存在时显示标题

<%if(request.getParameter("submit") != null){%>
<h2>Something <% out.print(something); %> with something <% out.print(something); %> </h2>
<%}%>