wood burning stoves 2.0*
The moose likes Servlets and the fly likes Doubt on calling java class in Servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Doubt on calling java class in Servlet" Watch "Doubt on calling java class in Servlet" New topic
Author

Doubt on calling java class in Servlet

dhwani mathur
Ranch Hand

Joined: May 08, 2007
Posts: 621
hi all


i am in a doubt of
is it possible to call
a java class in servlet?
,actualy this is what i am
doing but i dotn have any idea onto is this possible or not?

i have created 4 things
first html page which calls
jsp page in which i am calling my Servlet
and in my servlet i have placed code which calls
one java class and forwards to another Jsp
i am getting another JSp but my java class is not called


may be all this sounds a bit confusing all
in all what i am trying to achieve is
calling Servlet from JSP passing parameters
to Servlet and
from Servlet passing parameters
to my Java class which connects to the databse
and saves the user input in table
below is my code can anyone help me
onto what i am doing does it go in right direction?
i dont know bcoz i am not gettng data passed by user
in the table


below is my servlet code as well as java class

code for test servlet



code for Connect.java class



please if anyone can help me on above
it will be great thanks in advance......


Dhwani:Its always too soon to quit.
[ January 11, 2008: Message edited by: Bear Bibeault ]
sudhir nim
Ranch Hand

Joined: Aug 29, 2007
Posts: 212

Yes you can instantiate and use other classes in a servlet

are you hoping that main() method will be called automatically? it will not.
move that code to some other method and call that method from your servlet.

some thing like this


[Servlet tutorial] [Servlet 3.0 Cook Book]
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42292
    
  64
I don't understand what exactly is happening.

Is the doGet method called? Is the if-condition true or false? If it is true, what happens then? Does the code in the constructor run? As it is, there's no way to check whether that's the case or not, so how have you established that it's not running?


Ping & DNS - my free Android networking tools app
dhwani mathur
Ranch Hand

Joined: May 08, 2007
Posts: 621
Hi thr

i am sorry Ulf Dittmer for such a confusing code


actualy i have created a JSP from which i am calling
this Servlet and also doGet method is called bcoz i
have used it in my JSP .

i think Sudhir is right bcoz how will the main
method be called ?i was wrong but now i have made
those changes but still i am not getting my data
in the database table
is there any changes i must make
in web.xml so as to include java class in that servlet


please any help on above will be great

thanks in advance.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42292
    
  64
actualy i have created a JSP from which i am calling this Servlet and also doGet method is called bcoz i have used it in my JSP.

I don't understand what you mean by this. You don't "call" a servlet from a JSP - do you mean forward, or include, or redirect?

Furthermore, the doGet method is not used in a JSP. (To be more precise, one is being created behind the scenes by the servlet container, but there is none in the JSP, nor would your code use it.)

i think Sudhir is right bcoz how will the main method be called?
Indeed, main methods have no meaning in servlet containers, and won't be called.

is there any changes i must make in web.xml so as to include java class in that servlet
No. Your class will be used if the code is being called properly. That's why I asked the various questions above. Answering those will help us pinpoint the problem.

Also, how is the servlet being called - with which URL, and which parameters?
dhwani mathur
Ranch Hand

Joined: May 08, 2007
Posts: 621
hey! Ulf Dittmer


you are absolutly right i have used redirect() in My JSP below is the code for it
Testing.jsp gets the values from html page using request.getParameter method
in my html i pass three values firstname ,email and password



test is the name of my Servlet
below is code for it





below is my code for Connect.java which connects to database and inserts value in table





Now i dont know where i am going wrong ?

here is code for web.xml


<servlet>
<servlet-name>test</servlet-name>
<servlet-class>test</servlet-class>
</servlet>
<servlet>
<servlet-name>ValidUser</servlet-name>
<servlet-class>ValidUser</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>test</servlet-name>
<url-pattern>/test</url-pattern>
</servlet-mapping>

<servlet-mapping>
<servlet-name>ValidUser</servlet-name>
<url-pattern>/ValidUser</url-pattern>
</servlet-mapping>



any help on above will be realy great
thanks in advance

Dhwani:>Swimmers never weep.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Rather than post your code over again, go back and answer each of Ulf's questions. People will be much more likely to stick with you until your problem is solved if you're careful not to ignore questions that they've asked when trying to help you.

Answer them all to the best of your ability.
If you don't understand one of his questions, let him know so he can re-phrase it.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61437
    
  67

Originally posted by dhwani mathur:
... method is called bcoz i have used it ...

Please use real words when posting to the forums. Abbreviations such as "bcoz" in place of "because" only serve to make your posts more difficult to read and less likely to generate useful responses.

Please read this for more information.

thanks,
bear
JavaRanch sheriff


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
dhwani mathur
Ranch Hand

Joined: May 08, 2007
Posts: 621
Hi


i sincerely apologize for the mistakes .
thanks a lot for all the help till now.

Dhwani:>Swimmers never weep.
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

Dhwani,

I do have a suggestion to you. I know you are into the beginning level of Servlets and JSP. And i have been seeing a couple of posts from you in the very basic stuff which certainly shows that you lack in the *fundamental concepts* of the technologies.

Better spend some time and read the concepts clearly. Once you understand the basic working model of the technologies, you yourself will get rid of the small issues.

Good luck


Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
Rahul Shilpakar
Ranch Hand

Joined: Aug 29, 2006
Posts: 132
yes you can do it. Servlet is also a java program. you may call any function of any java class from it. and can also pass the parameter


Perform for today. Adapt for tomorrow.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Doubt on calling java class in Servlet