aspose file tools*
The moose likes Struts and the fly likes Connecting an applet to my struts data Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Connecting an applet to my struts data" Watch "Connecting an applet to my struts data" New topic
Author

Connecting an applet to my struts data

Matthew Plant
Greenhorn

Joined: May 07, 2008
Posts: 18
I have a functioning struts web application.
I want to display some graphics based on data from my web application and my current thinking is to have an applet on a page which gets it's data from a servlet (or an struts action) and renders the appropriate images.

I have written the applet and have all my data in one object but connecting from the applet to a struts action just seems wrong and I can't seem to add another servlet into web.xml without the rest of the application breaking either (are you even really meant to use other servlets when you are using struts?).

Am I going about this all wrong? Is there an easier way?
Any help would be appreciated.
Amit M Tank
Ranch Hand

Joined: Mar 28, 2004
Posts: 257
What is wrong in connecting the applet to your struts?


Amit Tank
Linked In
Matthew Plant
Greenhorn

Joined: May 07, 2008
Posts: 18
I'm not sure how to do it.

Do I create a new servlet which sits alongside the struts action servlet and define this in web.xml and use this to serve my applet the required data?

Or can I connect the applet to a struts action directly?

Or is there another more elegant solution?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42929
    
  68
Hi Matt-

Welcome to JavaRanch.

On your way in you may have missed that we have a policy on screen names here at JavaRanch. Basically, it must consist of a first name, a space, and a last name, and not be obviously fictitious. Since yours does not conform with it, please take a moment to change it, which you can do right here.

As to your question, using a Struts action would seem weird - not nearly as natural as using a servlet directly. That is definitely possible, though, in addition to using the Struts servlet. What problems are you facing in particular setting that up?
Matthew Plant
Greenhorn

Joined: May 07, 2008
Posts: 18
I wrote a httpservlet (would genericservlet be better) and added in the definitions into web.xml but then I could not load jsp pages from my struts application and so I wondered if adding additional servlets interrupted the normal flow of the actionservlet and requests to it and should be avoided.

Also I didn't know if my servlet was actually running or not.

It made me sad... :-(
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42929
    
  68
Extending HttpServlet is the way to go. How are you mapping the servlet in the web.xml file? It sounds as if there may be an overlap between the servlet mapping and the Struts mapping.
Matthew Plant
Greenhorn

Joined: May 07, 2008
Posts: 18
Erm something like:

<servlet>
<servlet-name>custom</servlet-name>
<servlet-class>test.servlet.CustomServlet</servlet-class>
</servlet>

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

not quite sure how the url pattern should look for what I am trying to do...

Also I noticed the struts action servlet defination has this extra bit within the servlet tags. <load-on-startup>2</load-on-startup>. Maybe I need one of those in my servlet definition?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42929
    
  68
load-on-startup has nothing to do with this.

What happens if you try to access the servlet? Have you tried it from within a browser?

Assuming that the Struts servlet is not mapped to "/custom" it should work.
Matthew Plant
Greenhorn

Joined: May 07, 2008
Posts: 18
I finally got it to work and I haven't broken struts!!!
Thanks for your help with this.

Think I was mostly doing things right but a couple of things tripped me up...

1. I think I was initially missing a slash in the servlet mapping which was breaking struts.

2. Then when I got the networking code sussed and the applet worked standalone but not embedded in a JSP I realised struts was helpfully sticking ".do" onto the end of my request to the servlet.

Changed the servlet mapping to the following and it ALL worked.

<servlet-mapping>
<servlet-name>custom</servlet-name>
<url-pattern>/Custom.servlet</url-pattern>
</servlet-mapping>

Now if I can only remember what I was originally trying to achieve... ;-)

Thanks again, great forum.
Andr� Cedik
Greenhorn

Joined: Mar 30, 2008
Posts: 8
Can you specify what you'd like to draw? If you're using Struts 2 maybe something like the JFreeChart Plugin or Connext Graph Plugin might be helpful for you.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Connecting an applet to my struts data