• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt on calling java class in Servlet

 
dhwani mathur
Ranch Hand
Posts: 621
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 212
Eclipse IDE Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
dhwani mathur
Ranch Hand
Posts: 621
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 621
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64959
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
dhwani mathur
Ranch Hand
Posts: 621
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi


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

Dhwani:>Swimmers never weep.
 
Raghavan Muthu
Ranch Hand
Posts: 3381
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Rahul Shilpakar
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic