This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Servlets and the fly likes Calling another servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Calling another servlet" Watch "Calling another servlet" New topic
Author

Calling another servlet

meena latha
Ranch Hand

Joined: Jan 24, 2005
Posts: 219
Hi all..........
I have written a program to search and get the details of the employees and display the details.
In the displayed screen i have given two option ,like to Delete or Update.
So to execute the delete or update i have written a deleteServlet and UpdateServlet.

Now my problem is how do i call the deleteServlet or UpdateServlet from my SerachServlet.

I am attaching my code also ...


I also need to know is there any better way to do this application.
I know only a little of jsp.
Instead of using out.println to output the html i know i can use jsp but dont know how to do it.

It will be great if somebody gives me suggestion how to call my deleteServlet and how should i use jsp to make my code appear simple.

Thanks a lot
Ramya.A
[ May 19, 2005: Message edited by: ramya jp ]
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

For each value, you can add a link with querystring variables or a small form that points to the update and delete servlets.


I also need to know is there any better way to do this application.
I know only a little of jsp.
Instead of using out.println to output the html i know i can use jsp but dont know how to do it.

It will be great if somebody gives me suggestion how to call my deleteServlet and how should i use jsp to make my code appear simple.


The most popular design pattern for cleaning up server side code and moving all the look and feel code into JSPs is the "Model, View, Controller" (MVC) pattern. There are frameworks out there that implement this pattern for you. Struts is the most well known. Many people (myself included) use this pattern without anyone else's framework.

For a real simple example, go to http://simple.souther.us
and download SimpleMVC.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Amarender Reddy
Ranch Hand

Joined: May 12, 2005
Posts: 54
hi ramya!

I kindly request you to post any code within the 'code' tags of the UBB

Code, so that your query would be readable. Coming to your question, you

have no where included a 'Delete' or 'Update' button in your SearchServlet.

Probably you might have coded a seperate HTML page for that. So there is no

question of calling the 'updateServlet' or 'deleteServlet' from your

'searchServlet'. One of this should be invoked through the concerned HTML

page. Please clarify if I am wrong. However, RequestDispatcher.forward()

method would do it for you.

cheers,
amar
meena latha
Ranch Hand

Joined: Jan 24, 2005
Posts: 219
Sorry amar for not posting my code properly.
I came to know that i can pass the value to another servlet by using
URLRewritting,cookies,HttpSession.

In my application SearchServlet i want to use URLRewriting method to call my DeleteServlet and UpDateServlet.

I am posting a part of my code where i have done this.



Now my out put screen will applear like this.



So when i click the delete or update link it should take me to the appropriate servlet and do the action written there.

I am able to get the output screen but when i click the delete or Update link i am getting an error saying DeleteServlet not found

So how should i solve this error.

Thanks in advance.
Ramya.A

[ May 20, 2005: Message edited by: ramya jp ]
[ May 20, 2005: Message edited by: ramya jp ]
Nikhilesh Fonseca
Ranch Hand

Joined: Aug 23, 2004
Posts: 57
Have u mapped your servlets to their names in the Web.xml file
there is the class name and the servlet name as well as the url which you can map to call the particular servlet.
The web.xml file will be in the Web-Inf folder.Check it out.
You can give the path where you call the form.
Read up on the web.xml file and you'll have your answer.
What ever form action you give will be mapped to the servlet.
Cheers
Nikhilesh
meena latha
Ranch Hand

Joined: Jan 24, 2005
Posts: 219
Thanks Nikhilesh...

My doubt is not yet cleared....
I have written an html page where it call a servlet called SearchServlet.
I have dont the web.xml mapping for this.

Here is my web.xml mapping details



Now i was try to call a servlet called DeleteServlet from SearchServlet and was trying to pass value.For this is added the below code in my SearchServlet



And in my DeleteServlet i used



This is what i have done to call from my SearchServlet to DeleteServlet..
I want to know apart from the above web.xml mapping am i need to make anything more to call my DeleteServlet from my SearchServlet.

Please reply in detail.....
Thanks in advance
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

If you were to pull out your business logic to update and delete to Plain Old Java Objects (POJO), then you wouldn't need to call another Servlet. They could all share the same POJOs to do their work. This is a cleaner design.

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
avaya sahu
Greenhorn

Joined: Oct 16, 2001
Posts: 14
Hi,
I think you have not registered the Delete servlet, u have to to call sendredirect by passing the path of servlet. once you do it by using urlrewriting you can invoke the delete servlet and after performing the task can redirect the request to search page again.

I will suggest you to go for JSp soln instead of servlet.
in case of jsp you have to drop you out.print* with simple text

e.g if you have written a statement like out.print("<Html>");


just write <html>


for any logic where u need java code just write it inside
<%
//........java code
%>

html code

<%
...java code
%>
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Calling another servlet
 
Similar Threads
derby problem
Error with jsp and JavaBeans
Displaying a ResultSet Problem
reading data from jsp page to another jsp page using request.getParameter
A href command not working