Two Laptop Bag*
The moose likes JSP and the fly likes How do I run a java file inside JSP? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "How do I run a java file inside JSP?" Watch "How do I run a java file inside JSP?" New topic
Author

How do I run a java file inside JSP?

Adrian Lee
Ranch Hand

Joined: May 10, 2007
Posts: 71
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.
Freddy Wong
Ranch Hand

Joined: Sep 11, 2006
Posts: 959

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).


SCJP 5.0, SCWCD 1.4, SCBCD 1.3, SCDJWS 1.4
My Blog
Adrian Lee
Ranch Hand

Joined: May 10, 2007
Posts: 71
Hi Freddy and thanks for your reply.

I will try to modify my code to a servlet.
paritosh ranjan
Ranch Hand

Joined: Aug 24, 2007
Posts: 62
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??
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60782
    
  65

Using a servlet is the only reasonable means to do this. Why are you resisting the obvious choice?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Adrian Lee
Ranch Hand

Joined: May 10, 2007
Posts: 71
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

Joined: Sep 11, 2006
Posts: 959

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

Joined: May 10, 2007
Posts: 71
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!
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41108
    
  45
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.


Ping & DNS - my free Android networking tools app
Adrian Lee
Ranch Hand

Joined: May 10, 2007
Posts: 71
Thanks Ulf! That sure helped me moved on.

Thanks everyone.
Adrian Lee
Ranch Hand

Joined: May 10, 2007
Posts: 71
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
Marshal

Joined: Mar 22, 2005
Posts: 41108
    
  45
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

Joined: May 10, 2007
Posts: 71
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 ]
Stefan Evans
Bartender

Joined: Jul 06, 2005
Posts: 1016
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

Joined: May 10, 2007
Posts: 71
Thanks for the reply, Stefan.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How do I run a java file inside JSP?
 
Similar Threads
Why is Runtime.getRuntime().exec() not working in JSP?
Need a few JSP tips
reading data from jsp page to another jsp page using request.getParameter
Distributing apps as jar files
run an no java prog. from java