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 Struts and the fly likes Struts Action and Java 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 » Frameworks » Struts
Bookmark "Struts Action and Java Servlet" Watch "Struts Action and Java Servlet" New topic
Author

Struts Action and Java Servlet

Abubacker Siddik
Ranch Hand

Joined: Aug 12, 2009
Posts: 90
Hi all,

I am using struts framework..all of a sudden it seems i miss the part of the servlet. because org.apache.struts.action.Action class seems to take the position of Servlet. If it is not correct, How to use Servlets in struts application. In which scenario i should go for the Servlets usage in Struts.


Thanks,
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

It's a rare occasion you'd want to use a servlet in a Struts app, and rarer still you'd *need* to.
chetan agg
Ranch Hand

Joined: Apr 11, 2009
Posts: 31
Hi,

In your web.xml, you would have defined something like this to handle all struts requests:

<servlet-mapping>
<servletName> ActionServlet </servletName>
<url-pattern>*.do</url-pattern>
</servlet-mapping>


To access a servlet, you need to define a servlet mapping with different name like this:

<servlet-mapping>
<servletName> MyServlet </servletName>
<url-pattern>/servlet/MyServlet</url-pattern>
</servlet-mapping>

Now all requests to /servlet/MyServlet/ will go to new servlet.

In scenarios where you want to send an Ajax call, you can use servlet.

Regards
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

chetan agg wrote:In scenarios where you want to send an Ajax call, you can use servlet.

NOT necessary, you can handle AJAX calls with Struts action too.

Also, think from the "Controller" prospective, you'll just miss all the functionality provided by the Struts Controller. like action mapping, dispatch action, etc, One more, when you use the Servlets, you have to edit the "web.xml" for the servelet mapping entry, which is, not considered a good practice if you're uisng struts for not-so-special actions, like AJAX. Place all the action mapping in struts-config.xml

So you rarely need servelets in Struts, and that's why Strusts1 is designed to enhance servelets use..


[LEARNING bLOG] | [Freelance Web Designer] | [and "Rohan" is part of my surname]
Abubacker Siddik
Ranch Hand

Joined: Aug 12, 2009
Posts: 90
i have to tell you that i am not good at J2EE, so you are taking about AJAX calls to servlet. I even confuse how to use servlets then how can i use AJAX in struts. If you can help your point through an example code, i might understand.
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

AJAX is used with JavaScript for the asynchronous request to the server and Servlet is one of the request processor.

For more about AJAX OR Google.
Abubacker Siddik
Ranch Hand

Joined: Aug 12, 2009
Posts: 90
 
 
subject: Struts Action and Java Servlet
 
Similar Threads
requested servlet is not available
scea - 1 experiences
try, catch,finally
JDK - Tomcat version compatibility
Struts and EJB file structure in eclipse