oneiros
oneiros

Reputation: 3578

Java Equivalent for code-behind in ASP.net?

I have a rather curious scenario, which I am sure that the immense talent browsing through the questions here at stackoverflow will have very little trouble helping me out.

I have a project in Java - which pretty much does something with taking a file and uploading it in an imaging system. The code works in a way that given the path of the file, it takes it and uploads to the system I want it (this includes a lot of business-specific logic which serves no purpose to be disclosed).

Now, I would like to have an HTML page with a FILE input, that takes a file and then CALLS the code that I wrote which will do the job of taking the file and uploading it to the system.

This means that I have to migrate my Java project into something that can be "callable".

There are a lot of third party jar files that help my code run.

  1. First scenario would be to somehow make the code into a scriptlet - all the other code can be compiled into a jar. I hate having a jsp that does the deal, but just to get it to a stage where it is working, I would like to entertain this option. My question now is where do the jar files go? This will be running in Apache Tomcat, so I guess I just place the jars in the lib directory and restart Tomcat and that should do the deal? Is there any classpath file that needs to be modified to make the Tomcat server be aware of the new jar files?

  2. Second scenario would be to have a servlet, and then the jars go in the WEB-INF directory? Please somebody guide me with this one.

What do you think would be the best way to "migrate" my Java code that does the business logic to something that can be called from an html page? What would be the equivalent of a code-behind?

Thank you very much in advance and hope you can elucidate this matter for me a little bit.

Upvotes: 1

Views: 3309

Answers (2)

Abdullah Jibaly
Abdullah Jibaly

Reputation: 54790

This is actually a pretty standard scenario, and there are tons of approaches you can take depending on how scalable you want this service to be, how secure, etc. I'll start with the approach that requires the least amount of work on your end:

  1. Create a JSP as you mentioned with the file input.
  2. Create a servlet the simply takes the uploaded file (Tomcat will store it in a temporary folder, you may want to move it first) and gets the path.
  3. Have your servlet invoke your existing Java code through a ProcessBuilder and pass the path to it as a command-line argument. Wait for it to complete.
  4. Delete the temp file as necessary.

A more thorough approach will involve having your servlet directly call the backend Java code. If you already have an interface you can call then you can put the jar file into your webapp's WEB-INF/lib folder and your servlet will be able to reference the classes in the jar file so you can call whatever class you need to directly. If this process is going to be time consuming then you do not want to hold up the servlet, and consequently the request, so it's good practice to hand it off to an asynchronous thread that will take care of doing whatever needs to be done with the file and cleaning up afterwards.

Upvotes: 3

Jack Edmonds
Jack Edmonds

Reputation: 33171

Perhaps the closest equivalent would be a servlet. In WEB-INF/web.xml You can set up servlets to handle web requests coming to certain URLs. Once you have handled the user's request in the servlet, you usually forward the respone to a .jsp file.

3rd party libraries go in WEB-INF/lib. (Another approach is to put the 3rd party jars into tomcat's lib folder. I wouldn't recommend this approach because, say you decide to migrate over to a different version of Tomcat. How do you remember which jars to copy?)

Upvotes: 1

Related Questions