joshft91
joshft91

Reputation: 1895

Prevent jsp from running before submit button is clicked

I have a jsp page with a form in it.

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

Below the form I'm going to have a table built on a database query. The problem is, the heading is output when the page is loaded. Is it possible to wait until the submit button is pressed to run the query/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);
}
%>

Upvotes: 0

Views: 1745

Answers (1)

Sree
Sree

Reputation: 635

Assuming that you are submitting to the same page

add a name for the submit button

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

then show heading only if the parameter is present

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

Upvotes: 1

Related Questions