aspose file tools*
The moose likes Servlets and the fly likes Java Functions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Java Functions" Watch "Java Functions" New topic
Author

Java Functions

Farakh khan
Ranch Hand

Joined: Mar 22, 2008
Posts: 732
I want to call this class whenever I need to show the list values of the countries but being a begginner in java unable to understand right now. Can anybody Please help me?

Thanks & best regards

<blockquote>code:
<pre name="code" class="core">import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class countries extends HttpServlet{

Connection cnn=null;
ResultSet rs=null;

public void doGet(HttpServletRequest req, HttpServletResponse res)throws IOException, ServletException{

res.setContentType("text/html");
PrintWriter out=res.getWriter();

try{

if ((cnn==null)||cnn.isClosed()){
cnn=db.getOracleConnection();
}

String country="",country1="",country2="";

String query="select * from country";
PreparedStatement ps=cnn.prepareStatement(query);
rs=ps.executeQuery();

while(rs.next()){
int id=rs.getInt(1);
String col2=rs.getString(2);

country1+="<select name=country class=khan>"+
"<option value=''>-- Select Country --</option>"+
"<optioin value=\""+id+"\">"+col2+"</option>";
}
country2="</select>";

country=country1+country2;
ps.close();


}catch(Exception e){
out.println(e.toString());
System.out.println("countries.java "+e.toString());
}

finally {
try {
if ( rs != null ) rs.close();
if ( cnn != null ) cnn.close();
} catch(SQLException ex) {
System.out.println("countries..java "+ex.toString());
}
}

}
}
</pre>
</blockquote>
[ July 15, 2008: Message edited by: Campbell Ritchie ]
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11229
    
  16

you don't call a class - you call a method. you need an instance of this class to call the method. somewhere, you would have done a

countries myCountries = new countries();

(note: traditionally, a class name starts with a capital letter)

Then, you could call myCountries.doGet(<parameters go here> .

Alternately, you could make the method static and declare the the two parameters inside the method, then call it by simply saying

countries.doGet(<parameters go here>


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19670
    
  18

Uhm, Fred? You are well aware that this is a servlet, and it should be run from a web container like Apache Tomcat?

Farakh, if you do that correctly all you need to do is open a web browser and navigate to the servlet.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11229
    
  16

then shouldn't this be in the servlet forum?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19670
    
  18

Probably.
Farakh khan
Ranch Hand

Joined: Mar 22, 2008
Posts: 732
Sorry I didn't understand. Can you Please alter my provided code

Thanks & best regards
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38340
    
  23
Originally posted by fred rosenberger:
then shouldn't this be in the servlet forum?
If you and Rob agree about it, then it shall go to the servlets forum.

Moving . . .
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

If you want to be able to reuse a piece of code, don't put it in a servlet.
Create a bean or a plain old Java object that can be instanciated and have its methods called by your servlet.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Amit Ghorpade
Bartender

Joined: Jun 06, 2007
Posts: 2716
    
    6

Hi Farakh khan although its not recommended to mix the business logic with presentation,
if you want it that way, you need to write the entire html in your servlet code so as to generate the desired output.
Or include this in the output of another JSP.


Hope this helps


SCJP, SCWCD.
|Asking Good Questions|
Amit Ghorpade
Bartender

Joined: Jun 06, 2007
Posts: 2716
    
    6

And yes UseAMeaningfulSubjectLine

Java functions is too generic to make sense.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Java Functions