wood burning stoves 2.0*
The moose likes JSP and the fly likes Getting data from a form to query Access using JSP Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Getting data from a form to query Access using JSP" Watch "Getting data from a form to query Access using JSP" New topic
Author

Getting data from a form to query Access using JSP

angela ruberto
Greenhorn

Joined: Apr 03, 2003
Posts: 2
I have a form that has textboxes called title2 and surname2 I'm using them to get data from the user to enter it into an SQL search for my shopping cart. I do not think the way I have called the JSP into the SQL to get the data from the form is correct (see after LIKE in SQL code) as it brings up the following error:
Message Internal Server Error
description The server encountered an internal error (Internal Server Error) that prevented it from fulfilling this request.
exception
java.lang.NullPointerException
If I put an error page in the only error I get is it states "Null".
If I input the values for the search manually i.e. LIKE '%queen%' or get all records in the Ebook table from the database without any conditions the records are displayed properly in the shopping cart, so it must be the JSP elements that are not right.
Let me know if you need anymore information/code like the Product and shoppingbasket classes. Any help would be gratelfully appreciated.
The SQL code for the shopping cart page is as follows:
<%@ page language="java" contentType="text/html"
import="ShoppingBasket,Product"
%>
<html>
<head> <title>Welcome to the Shop</title></head>
<body>
<table width="385" border="0" cellspacing="0" cellpadding="2">
<tr>
<td colspan="4"><b><u>Search Results</u></b></td>
</tr>
<tr>
<td colspan="4" align="right">
<a href="<%= response.encodeURL("shop-basket.jsp") %>">
<img src="images\viewbasket.gif" border="0" alt="View Basket"></a>
</td>
</tr>
<tr>
<td><b>Book ID</b></td>
<td><b>Title</b></td>
<td><b>Price</b></td>
<td></td>
</tr>
<%
String title2 = request.getParameter("title");
String surname2 = request.getParameter("surname");

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
java.sql.Connection connection =
java.sql.DriverManager.getConnection("jdbc dbc:Novbase","","");
java.sql.Statement statement = connection.createStatement();
java.sql.ResultSet RS = statement.executeQuery("SELECT * FROM Author RIGHT JOIN Ebook ON Author.author_id = Ebook.author_id WHERE Ebook.title LIKE '%" + title2 + "%' AND surname LIKE '%" + surname2 + "%'");

int rowCounter = 0;
while(RS.next())
{
String item_id = RS.getString("item_id");
String title = RS.getString("title");
String description = RS.getString("description");
String price = RS.getString("price");
String surname = RS.getString("surname");
rowCounter++;
String bg = (rowCounter %2 !=0) ? "#C0C0C0" : "#FFFFFF";
%>
<tr bgcolor="<%= bg %>">
<td><%= item_id %><br>
<%= surname %></td>
<td><b><%= title %></b><br/><%= description %></td>
<td>� <%= price %></td>
<td><a href="<%= response.encodeURL("shop-products.jsp?title="+title+"&item_id="+item_id+"&price="+price) %> ">
<img src="images\addtobasket.gif" border="0" alt="Add To Basket"></A></td>
</tr>
<%
}
RS.close();
connection.close();
%>
</table>
<jsp:useBean id="basket" class="ShoppingBasket" scope="session"/>
<% String title = request.getParameter("title");
if(title!=null)
{
String item_id = request.getParameter("item_id");
double price = Double.parseDouble(request.getParameter("price"));
Product item = new Product(item_id, title, price);
basket.addProduct( item );
}
%>
</body>
</html>
Chris Baron
Ranch Hand

Joined: Mar 21, 2003
Posts: 1049
Hi angela,
i think this happens because title2 + surname2 are null the first time you call this page.
Mark Bensing
Ranch Hand

Joined: Mar 09, 2003
Posts: 40
I dont think the problem is that title2 and surname2 are null. If this JSP is always invoked as a result of a form submission with textboxes named "title" and "surname", then these parameters should always be present in the request and getParameter() should not return null for them. Even if they were null, then it would just make the SQL statement look like "...WHERE Ebook.title LIKE '%null%' and surname LIKE '%null%'", it would not generate a NPE.
I think the more likely cause of the NPE is that the price parameter is not present in the request causing request.getParameter("price") to return null and thus causing Double.parseDouble() to thrown the NPE.
It would also have been helpful to see a stack trace from the NPE.
I hope this helps.
Mark
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Getting data from a form to query Access using JSP
 
Similar Threads
Servlet Initialization Parameters
Displaying prices
Online Shopping
Using checkbox on Spring 2.5 MVC ?
Multiple forms in a single JSP page