• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How do I run a java file inside JSP?

 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi everyone. I'm really confused as to how can I run my java program inside JSP.

My JSP project is a subscription form. Upon completion of the subscription form, when the user click on the submit button. It will initiate a file download(java file) to the specified path inside the java file.

Some of the methods which I've heard to make it possible to run is to
1) modify my Java program to a Servlet.
2) modify my Java program to an Applet.
3) just insert the java code into the jsp project and run it.

I've tried 2nd and 3rd method, somewhow there's some error in it. Am I heading in the right direction? Or What could I do to resolve this? Please advice.

Thanks in advance.
 
Ranch Hand
Posts: 959
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can use scriptlet inside your JSP, but it's not recommended, though. According to MVC architecture, you're better off dispatching your request from your JSP to a servlet and let the servlet call your business logic (your download method).
 
Adrian Lee
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Freddy and thanks for your reply.

I will try to modify my code to a servlet.
 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can we use javabeans here.
If the java file is already there,we can make it a bean and then use that javabean to call the methods of that classes.
I am just guessing,can we do this??
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Using a servlet is the only reasonable means to do this. Why are you resisting the obvious choice?
 
Adrian Lee
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi there again. Sorry, I've been trying to make my servlet work but somehow it can't. I'm not really sure if I'm doing it the right way or I've missed anything. Please advice me.

I've created a HTTP Servlet named ClientServlet.


For the subscription form(jsp), I've added a download button.


Sorry for the long codes...

And Thanks alot for helping me out.
[ September 26, 2007: Message edited by: Adrian Lee ]
 
Freddy Wong
Ranch Hand
Posts: 959
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Adrian,

Can you provide the strack trace? It's a bit hard to tell by only looking at the code.
 
Adrian Lee
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Freddy. Thanks for your fast reply.

There's no stack trace error message. It's just that when I clicked the 'Download' button. It directs me to another page that says:

405 Method Not Allowed
HTTP method POST is not supported by this URL.

The above message comes from mozilla firefox.

Is there anything wrong with my servlet layout? or the way I called my servlet out? Thanks for your help!
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The form performs a POST, while the servlet only implements doGet (which is used for GET operations). Rename doGet to doPost, and everything should work.
 
Adrian Lee
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Ulf! That sure helped me moved on.

Thanks everyone.
 
Adrian Lee
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi everyone again.. There's something weird going on..

I did manage to download the file . However, the file wasn't downloaded to the machine that has completed the subscription form. Instead, It went to the Server machine that is hosting the JSP. :shocked:

I've checked through the program and can't seem to find anything wrong.

I'm running the JSP file from Oracle JDeveloper 10(as a server).
For client, it's running fedora core 6 system.


Would appreciate if someone can point out where I can start looking to resolve this problem. Sorry for the trouble and Thank you very much!
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you explain what the code is supposed to do?

It uses System.in, which in a servlet container is unlikely to do anything useful. I'm also not sure what the sockets are for.

Can't you just stream the file to the client through the HTTP response? An example of that can be found here.
 
Adrian Lee
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ulf.
I'll give a short description on the project.

Server :
1) Will be running server.java(Opens up a socket for client to get file).
2) testbg.gif(file for client to download) is stored on the desktop
3) Will be running login.jsp from Oracle JDeveloper.

Client :
1) Client does his subcription form on his own pc(fedora core 6) through ip address.
2) Upon completion, client.java(servlet) will initiate a file download from server side.
3) Client does not need to state the path to save the file.

My main aim is to let the client complete the subscription form and download the file(42kb size) at the end. Client do not have to state where to save the file. The actual file that i would like to send is a GUI bundle.

--------------------------------

I've run the file streaming sample. I'm just wondering if it will be able to download the file directly to the clients' pc without clients' need to choose the file location. Would that be a problem?

Many thanks for your help.
[ September 27, 2007: Message edited by: Adrian Lee ]
 
Bartender
Posts: 1845
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, you want to download and save a file on the client computer?
In a place you specify and the user can not change?

Can not be done with JSP/Servlet technology.
To allow this sort of ability would be a serious security breach on a computer. Just by visiting a webpage it downloads a file somewhere on your computer without your being able to do anything about it?

With JSP/Servlet, the java code is running on the SERVER.
Not on the clients computer.
So you can access the file system on the server - it won't help you to put a file on the client.

Two suggestions
1 - Just send the file to the client, and have them select where to save it. (Standard HTTP limitations)
2 - Use a signed applet which will then have access to the local client file system, and you can deal with it in that way.

Hope this helps,
evnafets
 
Adrian Lee
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the reply, Stefan.
 
reply
    Bookmark Topic Watch Topic
  • New Topic