Syy
Syy

Reputation: 11

JSP java code + html table

I have encountered a problem that I don't really understand. I'm using java code to access a database and then using the jsp part, display in html format the result. The code looks like this:

out.println("<html><body>");

out.println("<form method=\"GET\" action=\"http://localhost:1234/WebLabJSP/delete\">");

out.println("<table>");

out.println("<tr><th>Name</th><th>Quantity</th><th>Category</th></tr>");

ResultSet rs = s.executeQuery("select * from Products");


while(rs.next()){


out.println("<tr><td>" + rs.getString(2) + "</td><td>" + rs.getString(3) + "</td><td>" + rs.getString(4) + "</td><td><input type=\"submit\" value=\"Delete\" name=\"delete" + rs.getString(1) + "\"/></td><td><input type=\"submit\" value=\"Update\" name=\"update" + rs.getString(1) + "\"/></td></tr>");

   } 

out.println("</table></form>");

out.println("</body></html>");

The problem is, that if I have only the delete button in the form, it works perfectly, but when I added the second button, the page didn't display anything at all. If anyone knows what's going on, please help.

Upvotes: 1

Views: 6806

Answers (5)

Jigar Joshi
Jigar Joshi

Reputation: 240898

First of all this is not how you should use jsp. On jsp use jstl on servlet use java code.

and about your issue try checking generated HTML. It should have

<form ...>
.
.
.
.<input type="submit" name="something" value="delete"/>
.<input type="submit" name="something" value="update"/>
</form>

Or check for server's log,

See also

Upvotes: 4

Parith
Parith

Reputation: 85

Don't use connection process and other updating process in jsp page, try to use servlets or other javabeans. tag to use that servlets... if u want to update means simply cal that function, declared inside that servlet. servlet s act as a controller of ur jsp page.its more powerful.

Upvotes: 0

124697
124697

Reputation: 21893

Why dont you put the variables in request

request.setAttribute("string1",rs.getString(1));

then call it in your jsp

<td><%=request.getParameter("string1")%>;

Upvotes: -1

BalusC
BalusC

Reputation: 1108742

If you get a blank page, then this is often caused because an exception is been thrown in midst of rendering the response and the servletcontainer is unable to display the standard error page with exception details, because the response is already committed.

The exception will be logged to server log file. Lookup it, read the stacktrace and fix the problem accordingly.


Unrelated to the actual problem, the code practice you're showing is actually a poor practice. Java code should go in a Java class (controlled by a servlet) and HTML code should go in JSP without utilizing out.println(). JSTL should be used to control the page flow. See also this answer for an example. This way exceptions will/can be caught timely and end up in an error page.

Upvotes: 4

LaGrandMere
LaGrandMere

Reputation: 10359

Surround your :

ResultSet rs = s.executeQuery("select * from Products");
while(rs.next()){
out.println("<tr><td>" + rs.getString(2) + "</td><td>" + rs.getString(3) + "</td><td>" + rs.getString(4) + "</td><td><input type=\"submit\" value=\"Delete\" name=\"delete" + rs.getString(1) + "\"/></td><td><input type=\"submit\" value=\"Update\" name=\"update" + rs.getString(1) + "\"/></td></tr>");
   } 

with a try/catch block to see if there is an Exception.

Upvotes: 2

Related Questions