1

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.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Syy
  • 11
  • 1
  • 2
  • Check your error logs and let us know what the error was. – Asaph Dec 13 '10 at 15:55
  • "out" is the pageContext's output, the initializations concerning the page are done at the beginning of the program. – Syy Dec 13 '10 at 15:56
  • could be that rs.getString(1) returns characters that mess up the html (e.g. > or something) – JohnSmith Dec 13 '10 at 15:57
  • it should return proper strings and integers. – Syy Dec 13 '10 at 15:58
  • Wrinting HTML this way is very prone to errors. It's hard to read and the IDE cannot check if html elements match up. Read more on MVC and try to isolate your HTML from your logic. – Frankie Dec 13 '10 at 16:11

5 Answers5

4

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.

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
4

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

Community
  • 1
  • 1
jmj
  • 237,923
  • 42
  • 401
  • 438
2

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.

LaGrandMere
  • 10,265
  • 1
  • 33
  • 41
  • @Syy : if you know some of your columns are Integer and not String, for example imagine that the 2nd column in your ResultSet is Integer, you should have rs.GetInteger(2).toString() instead of rs.GetString(2) – LaGrandMere Dec 13 '10 at 16:43
0

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.

Parith
  • 85
  • 2
  • 6
  • 17
-1

Why dont you put the variables in request

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

then call it in your jsp

<td><%=request.getParameter("string1")%>;
124697
  • 22,097
  • 68
  • 188
  • 315
  • 1
    This will not work: request attributes and parameters are not the same. jstl expressions should be preferred to scriptlets. – svachon Dec 13 '10 at 18:43
  • scriptlets is a good point to start since this OP sounds new to this. plus its better than writing html in out.print – 124697 Dec 13 '10 at 19:14
  • 1
    It's even better to start with good habits and ignore scriptlets from the start. – Steven Benitez Dec 13 '10 at 22:56