• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Pl help me with this href portion in this code

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the following code I am trying to pass the parameter Item_Name to ServletB (in the href= ...). If I dont use the parameter (i.e ?Item_Name = ...) the code works fine. Can anyone please tell me why this is happenning.
Tx

import java.sql.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
public class ServletA extends HttpServlet
{
Connection dbCon;
public void init() throws ServletException
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
dbCon = DriverManager.getConnection("jdbc dbc:MyDB");
}
catch(ClassNotFoundException e)
{
System.out.println("ItemsQuery atabase driver could not be found.");
System.out.println(e.toString());
throw new UnavailableException(this,"Database driver class not found");
}
catch(SQLException e)
{
System.out.println("ItemsQuery:Error Connecting to the database");
System.out.println(e.toString());
throw new UnavailableException(this,"Cannot connect to the database");
}
}
public void doGet(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException
{
response.setContentType("text/html");
PrintWriter out= response.getWriter();
out.println("<HTML>");
out.println("<HEAD>");
out.println("<TITLE>Search Results</TITLE>");
out.println("</HEAD>");
out.println("<BODY>");
out.println("<table width='100%' border='0' cellspacing='5' cellpadding='2'>");
out.println("<TR> <TH align='center'>Items Chosen from Gallery </TH> </TR>");
try
{
Statement st=dbCon.createStatement();
ResultSet rec=st.executeQuery(
"Select Item_Name,Current_Bid, End_Date "+
"from Items");
while(rec.next())
{
out.println("<TR> <TD>");
out.println("<a href='http://dpaul004:9090/servlet/ServletB?Item_Name=" + rec.getString("Item_Name") + "'>" + rec.getString("Item_Name") + "</a> <br>");
out.println("Current Bid:" + rec.getString("Current_Bid") + "<br>");
out.println("Ends On:" + rec.getString("End_Date") + "<br>");
out.println("</TD> </TR>");
}
st.close();
}
catch(Exception e)
{
}
out.println("</table>");
out.println("</BODY> </HTML>");

out.close();
}
}
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"In the following code I am trying to pass the parameter Item_Name to ServletB (in the href= ...). If I dont use the parameter (i.e ?Item_Name = ...) the code works fine. Can anyone please tell me why this is happenning."
What exactly goes wrong? Does the page get written with bad data or what? You can't expect us to read your mind..... : )
Why are you not doing something with the exception? That is a great source of debuging information!
}catch(Exception e)
{
e.printStackTrace( out ); // one possibility
}
Bill
 
Dipanjan Paul
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry to have not explained the problem fully. Its been damn frustating from yesterday.
This is what the problem is. When the following code gets executed the output HTML is like the following.
<HTML>
<HEAD>
<TITLE>Search Results</TITLE>
</HEAD>
<BODY>
<table width='100%' border='0' cellspacing='5' cellpadding='2'>
<TR> <TH align='center'>Items Chosen from Gallery </TH> </TR>
<TR> <TD>
</table>
</BODY> </HTML>
The stuff from the database that I am trying to display is not getting formatted into the HTML.
But if the following line in the code
out.println("<a href='http://dpaul004:9090/servlet/ServletB?Item_Name=" + rec.getString("Item_Name") + "'>" + rec.getString("Item_Name") + "</a> <br>");
is coded as
out.println("<a href='http://dpaul004:9090/servlet/ServletB'>" + rec.getString("Item_Name") + "</a> <br>");
the output is perfect like the following
<HTML>
<HEAD>
<TITLE>Search Results</TITLE>
</HEAD>
<BODY>
<table width='100%' border='0' cellspacing='5' cellpadding='2'>
<TR> <TH align='center'>Items Chosen from Gallery </TH> </TR>
<TR> <TD>
<a href='http://dpaul004:9090/servlet/ServletB?Item_Name=a123'>Book1</a> <br>
Current Bid:10.0000<br>
Ends On:2000-12-01 00:00:00<br>
</TD> </TR>
<TR> <TD>
<a href='http://dpaul004:9090/servlet/ServletB?Item_Name=a123'>Book2</a> <br>
Current Bid:20.0000<br>
Ends On:2000-12-01 00:00:00<br>
</TD> </TR>
</table>
</BODY> </HTML>
Please help Thanx Dipanjan
public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException
{
response.setContentType("text/html");
PrintWriter out= response.getWriter();
out.println("<HTML>");
out.println("<HEAD>");
out.println("<TITLE>Search Results</TITLE>");
out.println("</HEAD>");
out.println("<BODY>");
out.println("<table width='100%' border='0' cellspacing='5' cellpadding='2'>");
out.println("<TR> <TH align='center'>Items Chosen from Gallery </TH> </TR>");
try
{
Statement st=dbCon.createStatement();
ResultSet rec=st.executeQuery(
"Select Item_Name,Current_Bid, End_Date "+
"from Items");
while(rec.next())
{
out.println("<TR> <TD>");
out.println("<a href='http://dpaul004:9090/servlet/ServletB?Item_Name=" + rec.getString("Item_Name") + "'>" + rec.getString("Item_Name") + "</a> <br>");
out.println("Current Bid:" + rec.getString("Current_Bid") + "<br>");
out.println("Ends On:" + rec.getString("End_Date") + "<br>");
out.println("</TD> </TR>");
}
st.close();
}
catch(Exception e)
{
}
out.println("</table>");
out.println("</BODY> </HTML>");

out.close();
}
}
 
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
I worked on you're code and it worked fine.
In the following explanation you r saying that if you change the following code line with the next line you get the same out put. Well that is never possible because in the second statement you r not writing query string for you're ServletB. So you will not get query string in second line of code.
[code]
But if the following line in the code
out.println("<a href='http://dpaul004:9090/servlet/ServletB?Item_Name=" + rec.getString("Item_Name") + "'>" + rec.getString("Item_Name") + "</a> <br>");
is coded as
out.println("<a href='http://dpaul004:9090/servlet/ServletB'>" + rec.getString("Item_Name") + "</a> <br>");
the output is perfect like the following
<HTML>
<HEAD>
<TITLE>Search Results</TITLE>
</HEAD>
<BODY>
<table width='100%' border='0' cellspacing='5' cellpadding='2'>
<TR> <TH align='center'>Items Chosen from Gallery </TH> </TR>
<TR> <TD>
<a href='http://dpaul004:9090/servlet/ServletB?Item_Name=a123'>Book1</a> <br>
Current Bid:10.0000<br>
Ends On:2000-12-01 00:00:00<br>
</TD> </TR>
<TR> <TD>
<a href='http://dpaul004:9090/servlet/ServletB?Item_Name=a123'>Book2</a> <br>
Current Bid:20.0000<br>
Ends On:2000-12-01 00:00:00<br>
</TD> </TR>
</table>
</BODY> </HTML>
[code]
=====================================================
What i found out that an exception is beging thrown and since you have not used any statement you r not able to see what is wrong or what exception is being thrown.
That is here:

st.close();
}
catch(Exception e)
{
}
==================================
second you change you're init method.
use following:
public void init(ServletConfig config) throws ServletException
and it will work fine.
With the earlier version of init method you were using you're code was working fine till the line
out.println("<TR> <TH align='center'>Items Chosen from Gallery </TH> </TR>");
try
{
===================
after this when it encounter you're this
line it gives the NullPointerException.
=============
So changer you're init method and it will work fine for both the condition.
Any pb still then you can write me directly at [email protected].
Since i am doing lot of work on servlets ....i would like to help you and any one out .....
============
Now the question comes why and where we can use this init() method?
Well i don't know this answer and will see if JGURUS can help us out?

Regards,
Raj.
 
Rajpal Kandhari
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Paul,
I was waiting for a reply from you. Have you been able to solve the pb. Did you get it right.
Contact me if you have more pb on servlets/Jdbc
Regards,
Raj.
 
Ranch Hand
Posts: 688
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's a tip I got from my friend:
Always use URLEncoder to encode all the links. It will solve pretty much most of the linf problems.
 
A "dutch baby" is not a baby. But this tiny ad is baby sized:
New web page for Paul's Rocket Mass Heaters movies
https://coderanch.com/t/785239/web-page-Paul-Rocket-Mass
reply
    Bookmark Topic Watch Topic
  • New Topic