Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

re: how do i link my first servlet to the second and then to third etc?

 
Dipanjan Paul
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
I am new to servlets and I am trying to do a project where I am using java servlets.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>");
out.println("<form action='http://localhost:8080/servlet/ServletB' method='get'>");
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 name = 'it_name'>");
out.println("Item Name:" + rec.getString("Item_Name") + "<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();
}
I am trying to use this page to have a list of all items and this page should have a hyperlink at the Items_Name parameter so that once the user clicks on this hyperlink it gets to the second servlet which is ServletB in my case.
Please help me how do i solve this problem and be done with the navigation.
thanks in advance,
DP
catch(Exception e)
{
}
out.println("</form>");
out.println("</table> </BODY> </HTML>");

out.close();
}
}
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13074
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is really just an HTML problem, not a servlets one.
The equivilent of:
out.println("<form action='http://localhost:8080/servlet/ServletB'
method='get'>");
expressed as a clickable link is something like:
<a href="http://localhost:8080/servlet/ServletB?param=value¶m2=value2" >Item Name Goes Here </a>
Which automatically goes to your doGet method.
Bill
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic