File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes coding help needed Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "coding help needed" Watch "coding help needed" New topic
Author

coding help needed

Ayub ali khan
Ranch Hand

Joined: Oct 20, 2005
Posts: 380
    
    2
Hello, I am doing a small project using servlets and JSP. My database is MS SQL 2000.

COuld you any one help me giving a sample code to implement the following:

Display drop down list which takes names from database table1.
When one name in the first drop down is selected, it displays second drop down list depending on the name selected in first drop down list. When a name is selected in second drop down list a third drop down list is displayed. When element in third drop down list is selected a weblink is displayed which is obtained from the database.

What would be the database structure and also how this can be implemented using JSP, servlets, html?

Thanks in advance.


SCEA part I,TOGAF Foundation
Ayub ali khan
Ranch Hand

Joined: Oct 20, 2005
Posts: 380
    
    2
Sorry I would like to add some more information to the previous post:

It should have a text box which if any new name is entered, it is added to the first drop down list which is automatically updated in the database.
another option which can be used to delete any entry from the first drop down list if required.

Thank you
dema rogatkin
Ranch Hand

Joined: Oct 09, 2002
Posts: 294
I'd implement everything using JavaScript. Check http://www.backbase.com.


Tough in space?, <a href="http://tjws.sf.net" target="_blank" rel="nofollow">Get J2EE servlet container under 150Kbytes here</a><br />Love your iPod and want it anywhere?<a href="http://mediachest.sf.net" target="_blank" rel="nofollow">Check it here.</a><br /><a href="http://7bee.j2ee.us/book/Generics%20in%20JDK%201.5.html" target="_blank" rel="nofollow">Curious about generic in Java?</a><br /><a href="http://7bee.j2ee.us/bee/index-bee.html" target="_blank" rel="nofollow">Hate ant? Use bee.</a><br /><a href="http://7bee.j2ee.us/addressbook/" target="_blank" rel="nofollow">Need contacts anywhere?</a><br /><a href="http://searchdir.sourceforge.net/" target="_blank" rel="nofollow">How to promote your business with a search engine</a>
Ayub ali khan
Ranch Hand

Joined: Oct 20, 2005
Posts: 380
    
    2
Thanks for fast reply. I was not able to where to search for in http://www.backbase.com./ website. However I have to use a database so that new entries can be added and removed from the drop down menu.

Any ideas how to do using a database?

Thanks
Fu Cheng
Greenhorn

Joined: Nov 21, 2005
Posts: 1
When an option is select, it fires the "onselect" event handler, you can in this event handler, post a request to the server ,and get the result back, use DOM to insert these items into the second select.
Ayub ali khan
Ranch Hand

Joined: Oct 20, 2005
Posts: 380
    
    2
Thanks FU. Could you give me an example code to understand it better?
Ayub ali khan
Ranch Hand

Joined: Oct 20, 2005
Posts: 380
    
    2
Following is my Application directory:

Project:
WEB-INF
Classes
ApplicationServlet.class
web.xml
index.jsp

index.jsp(contains all the code to query the database and display the initial drop down menu).

Below is the code for the respective files:
==============
index.jsp
==============
<%@ page import="java.sql.*" %>

<html>
<body>

<form method="GET" action="SelectTechnology"/>

<%

Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
Connection conn=DriverManager.getConnection("jdbc:microsoft:sqlserver://niting:1433;databaseName=Technology","sa","sa");

Statement stmt=conn.createStatement();

ResultSet rs=stmt.executeQuery("select techname from table1");

%>

<select name="technology">
<%
while(rs.next())
{
out.print("<option>"+rs.getString("techname")+"</option>");
}

%>

</select>

<br>
<br>

<form method="GET" action="SelectTechnology">
Enter name of Technology <input type="textbox" name="t1"><br><br>
<input type="submit" value="submit">
<br> <br>
</form>

Enter name to be deleted <input type="textbox" name="t2"><br><br>
<input type="submit" value="submit">
<br> <br>


</body>
</html>

=============
ApplicationServlet.java
==================
import javax.servlet.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import java.sql.*;

public class ApplicationServlet extends HttpServlet{

Connection conn;
public void doGet(HttpServletRequest req,HttpServletResponse res) throws

ServletException,IOException{
int flag=0;
Statement stmt;
ResultSet rs;
List li=new ArrayList();

res.setContentType("text/html");

try{

Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
Connection
conn=DriverManager.getConnection("jdbc:microsoft:sqlserver://niting:1433;databaseName=Technology"

,"sa","sa");

stmt=conn.createStatement();

rs=stmt.executeQuery("select techname from table1");

while(rs.next())
{
li.add(rs.getString("techname"));
}

Search se=new Search();

flag=se.find(li,req.getParameter("t1").toUpperCase());

if(flag==1)
{
stmt.execute("insert into table1 (techname) values('"+req.getParameter("t1").toUpperCase()+"')");
li.add(req.getParameter("t1").toUpperCase());
req.setAttribute("technology",li);
RequestDispatcher view=req.getRequestDispatcher("index.jsp");
view.forward(req,res);
}

else
{
flag=0;
RequestDispatcher errview=req.getRequestDispatcher("errview.jsp");
errview.forward(req,res);

}

conn.close();
stmt.close();
rs.close();

}//end try

catch(Exception e){System.out.println(e);}

} //end doPost

}//end class

=================
Search.java (class used to search the database for duplicate entries
=====================
class Search{

public int find(List li,String s){
int flag=0;

Collator c=Collator.getInstance();

Iterator it=li.iterator();

while(it.hasNext())

{
if(c.compare(s,it.next())==0)

{ flag=0; }

else {flag=1; }

}

return(flag);

} //end find

} //end search

Could any one help me find a better solution with code example that I can use to implement the cascaded drop down menus which use database?
Ayub ali khan
Ranch Hand

Joined: Oct 20, 2005
Posts: 380
    
    2
Any witty brians out there could solve this puzzle?

Thanks
Ayub ali khan
Ranch Hand

Joined: Oct 20, 2005
Posts: 380
    
    2
Could any one help me out?
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Ayub,
Ease Up.

I have an example app on my site that does this.
Look for "DynamicSelectLists".

http://simple.souther.us/not-so-simple.html


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Ayub ali khan
Ranch Hand

Joined: Oct 20, 2005
Posts: 380
    
    2
Hello Ben,

Thanks for a wonderful solution you have provided. My sincere apologies if I have troubled some one here. You have really made a big help to me. I am improving on my programming skills.I was stuck in a small project. I tried to search in google, however could not get what I was looking for. Your examples will surely make me little wiser now

Thanks for your Wonderful help !!!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: coding help needed