Search cannot be hyperlink to specific description:(

Discussion in 'Java JSP/Servlet' started by peggie1990, Jul 29, 2009.

  1. peggie1990

    peggie1990 New Member

    Hey guys do you know how to like click on the productId and it will go to speccific product Description.

    Above one is my search servlets:)

    Code:
    package sg.edu.nyp.feppz;
    
    import javax.servlet.*;
    import javax.servlet.http.*;
    import java.io.IOException;
    import java.io.PrintWriter;
    import java.sql.*;
    import java.util.*;
    
    import java.sql.Connection;
    import java.sql.DriverManager;
    
    /**
     * Servlet implementation class BeanieAdmin
     */
    public class BeanieAdmin extends HttpServlet {
    	public void doPost(HttpServletRequest request, HttpServletResponse response)
    	throws ServletException, IOException {
    response.setContentType("text/html");
    PrintWriter out = response.getWriter();
    
    System.out.println("MySQL Connect Example.");
    Connection conn = null;
    String url = "jdbc:mysql://localhost:3306/";
    
    String dbName = "feppz";
    String driver = "com.mysql.jdbc.Driver";
    String userName = "root";
    String password = "";
    
    Statement st;
    try {
    	Class.forName(driver).newInstance();
    	conn = DriverManager
    			.getConnection(url + dbName, userName, password);
    	System.out.println("Connected to the database");
    	String ProdColor = request.getParameter("ProductId");
    
    	ArrayList al = null;
    	ArrayList emp_list = new ArrayList();
    	String query = "select * from products where ProductId like'"
    			+ ProdColor + "' order by ProductId";
    	System.out.println("query " + query);
    	st = conn.createStatement();
    	ResultSet rs = st.executeQuery(query);
    
    	while (rs.next()) {
    		al = new ArrayList();
    
    		al.add(rs.getString(1));
    		al.add(rs.getString(2));
    		al.add(rs.getString(3));
    		al.add(rs.getString(4));
    		al.add(rs.getString(5));
    		al.add(rs.getString(6));
    		al.add(rs.getString(7));
    		al.add(rs.getString(8));
    		al.add(rs.getString(9));
    
    		System.out.println("al :: " + al);
    		emp_list.add(al);
    	}
    
    	request.setAttribute("empList", emp_list);
    
    	System.out.println("empList " + emp_list);
    
    	// out.println("emp_list " + emp_list);
    
    	String nextJSP = "/ProductDesc.jsp";
    	RequestDispatcher dispatcher = getServletContext()
    			.getRequestDispatcher(nextJSP);
    	dispatcher.forward(request, response);
    	rs.close();
    	st.close();
    	conn.close();
    	System.out.println("Disconnected from database");
    } catch (Exception e) {
    	e.printStackTrace();
    }
    }
    
    }
    This is my search box so that when I type 1101 it will lead to the page that display 1101 info.
    Code:
    <form method="post" action="/FEPPZ/BeanieAdmin">
    		<table border="0" width="300" align="center" bgcolor="#FFFFFF">
    			<tr>
    				<td colspan=2 style="font-size: 12pt; color: #00000;" align="center">
    				<h3>Search product</h3>
    				</td>
    			</tr>
    			<tr>
    				<td><b>ProductId</b></td>
    				<td>: <input type="text" name="ProductId" id="ProductId" /></td>
    			</tr>
    			<tr>
    				<td colspan=2 align="center"><input type="submit" name="submit" value="Search"></td>
    			</tr>
    		</table>
    		</form>

    This is the page that will show 1101 record if there is no 1101, it will show "No record".
    Code:
    <table width="500px" align="center" style="border:1px solid #000000;">
    
    <tr style="background-color:efefef;">
    
    <td><b>ProductId</b></td>
    <td><b>ProductName</b></td>
    
    
    
    </tr>
    <%
    int count=0;
    String color = "#F9EBB3";
    
    
    if(request.getAttribute("empList")!=null)
    {
    ArrayList al = (ArrayList)request.getAttribute("empList");
    Iterator itr = al.iterator();
    
    
    
    
    while(itr.hasNext()){
    
    if((count%2)==0){ 
    color = "#eeffee"; 
    }
    else{
    color = "#F9EBB3";
    }
    count++;
    ArrayList empList = (ArrayList)itr.next();
    %>
    <tr style="background-color:<%=color%>;">
    <td><a href="search.jsp?id=<%=empList.get(2)%>"><%=empList.get(0)%></a></td>
    <td><%=empList.get(1)%></td>
    <td>$<%=empList.get(2)%></td>
    <td>
    
    
    
    </td>
    
    
    
    </tr>
    <%
    }
    }
    %> 
    <%
    if(count==0){
    %>
    <tr>
    <td colspan=8 align="center" style="background-color:eeffee"><b>No Record</b></td>
    </tr>
    <%
    }
    %>
    
    </table>
    However the problem is how to click on the 1101 so that It can go view all the other description like the image, price,....

    Thanks:)


    I attach a zip file of image too:)


    Thanks!:D

    Attached Files:

    • pic.zip
      File size:
      10.4 KB
      Views:
      0

Share This Page