This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Java Functions

 
Farakh khan
Ranch Hand
Posts: 833
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12086
29
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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>
 
Rob Spoor
Sheriff
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12086
29
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
then shouldn't this be in the servlet forum?
 
Rob Spoor
Sheriff
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Probably.
 
Farakh khan
Ranch Hand
Posts: 833
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry I didn't understand. Can you Please alter my provided code

Thanks & best regards
 
Campbell Ritchie
Sheriff
Posts: 48424
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Amit Ghorpade
Bartender
Posts: 2854
10
Fedora Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Amit Ghorpade
Bartender
Posts: 2854
10
Fedora Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And yes UseAMeaningfulSubjectLine

Java functions is too generic to make sense.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic