can i use java code as it is in a jsp file? - java

Please forgive me if my question sounds too dumb :(... I have created a java desktop application, which has a single UI screen in Swing... The application creates a pdf report... I want to convert this application into a jsp based web application. So do I just have to create a simple screen where the parameters required to create the report are asked, this screen replaces the swing based UI... and the main block of java code (used to create the report with the help of input parameters) can be invoked from a jsp file? Is that it? Or is something more significant required to make this app into a web app. How do I find out what else is required?

You should have a web server. Create a web archive (.war) from your code base by reusing Java classes and new jsp file (UI). So your understanding is correct. and remember there won't be any main method now.

Yes - JavaSE code is the same Java on server side. You have to change UI. You may use plain HTML or web framework such as JSF.

This highly depends on your code. Business logic and PDF generation might be reused but the UI will definitely be replaced. You'd then also have to account for the download part, i.e. the response could be a page with the DL link or the generated PDF.
Edit: as Azodious already stated, you'd need a webserver like Tomcat (or JBoss which is a fully fledged application server and includes Tomcat) that is able to execute the Javacode, so just replacing the UI wouldn't help much.

Related

Communication between frontend (jsp webpage on localhost) and backend (java program) using eclipse

I am new to front end developing. I have so far a back-end program written in java with Eclipse that does all I want, saves the output in an object and prints it in the console (for my convenience).
My goal is to display this output information dynamically (meaning that the backend part of the program might update and send new output) in a webpage on a local server.
Hence I have "transformed" or "added" a Dynamical webpage to my Eclipse project using "Project Facets" in Eclipse Project Properties, and created an index.jsp file hosted on a tomcat server (See picture of the File organisation).
Ultimately, I want to have my index.jsp file open in my browser and when my backend program (which I assume I have to compile in an executable jar) detects changes , display those changes in the webpage. Also, I have a button on my webpage for which I would like to send information back to my backend program when it is clicked.
Should I use POST request in my backend with the URL of the index.jsp (although I don t want to display the information in a form, just regular text and images)?
or create a Javabean class in the webcontent/WEB-INF/ (if I can access and modify it dynamically from my backend) and then use that to get and set data from the jsp page?
or am I obliged to use something like Spring ?
I ve heard I should seperate backend and front end, but I don t really understand how they communicate dynamically !
Thank you for your help !
Seeing your project structure I believe you want to build a simple Java Web, you don't need to separate your front-end and back-end unless you want to build something complex.
I'm not quite sure about your experience with Java Web, but if this is your first time I suggest to learn more about Servlet because this is the fundamental in building Java Web project. After knowing how Java handle Http Request is up to you to build pure Java Web project, or using something like Spring MVC even if you want to build a separate front-end that communicate using REST API towards the back-end.

Web interface for a Java application

I recently developed a whole system in Java that connected to a database and exports and imports the table content to an excel sheet. I used SWING for the user interface. the user will interact with it for authentication and file management.
Apparently the client changed the requirements, He wants everything from a Web Interface. My team leader advised to look through JSP.
What does JSP actually do?
Will I have to rewrite the User Interface in Web if I used JSP?
is there an more effective and efficient solution to do this job?
I would Appreciate a specific answer
I'm not sure what you mean by "specific answer", but here goes:
JSP is a kind of template language, based on Java, and a technology for dynamically generating HTML. It's a server side technology. Look here.
Yes, if you're going for a pure web/HTML solution, you'll need to completely rewrite the UI.
There are other frameworks for creating webapps, such as Vaadin or Play! Framework that may be "better" than JSP, but then there's a whole new API/framework to learn...
What does JSP actually do?
Will I have to rewrite the User Interface in Web if I used JSP?
is there an more effective and efficient solution to do this job?
and
I used SWING for the user interface.
and
exports and imports the table content to an excel sheet.
not, have to look at JavaFX 2
You will certainly need to rewrite the user interface if you convert to JSPs.
JSPs are essentially just a method for dynamically generating HTML (with the option to embed Java code to produce parts of the page).
It is still possible to run Swing applications from a web browser: you might want to take a look at Java Web Start. This will save you from having to do a complete rewrite.
1.) JSP is pretty much like PHP. It is server side scripting. When ever a browser request for a page (JSP page), server (mostly Tomcat or any application server which you deploy your JSP project) will generate HTML content using the JSP code. Mainly JSP consist of part HTML, JavaScipt (if you want dynamic stuff), and Java.
2.) As far as I know if you are aked to do it in JSP then you need to o all the client side work again in JSP. There you will be generating HTML UIs using Java codes. But you can use all the back end codes you used.
You may can use SWING in a Applet.

Provide an interface between the web browser and a Java application

Which language/tool do I use to provide an interface between the web browser and a Java application?
My task is create a web page and provide a feature which will enable results to be uploaded as XML files to dynamically update the page.
I have been instructed to use languages such as Java, Javascript(JQuery), Ruby, Grails, PHP etc.
So I have so far written some java classes that will process the XML files but I am running this application from the command line. I DO also understand how to display the XML on the web browser using XSLT.
My problem is, that I have only ever written desktop applications... I don't know what tools to use to provide an interface between my java back-end and the web front-end or how to (for lack of another method) invoke the JVM from the browser.
How do I send uploaded XML files to my java classes and get the XML results back? Can anyone shed some light into which tools I should use and the network packages (if needed) I need to import in Java? (note: this only needs to be run locally)
Feel free to make edits to tags/title and question if necessary.
I have been working in java and i feel that servlet-jsp are the best option
to full fill your requirement.

When JEditorPane will not do. JRex, JRex wherefore are thou JRex?

IDEA: Implement a recent web browser into a java application (for saved offline, non server content).
The question is this: can I have a java application implement a webbrowser with jquery / html / css support within a java program?
So I am asking anyone who has played with JRex for advice: I want to know how complicated will it be to integrate an open source webbrowser into java. I am not all that keen on the idea of compiling Mozilla from source build. Is there a ready made compiled version?
Is there a simplified method to have latest compiled version (most current in terms of support for HTML css & javascript), and integrate that into an application?
Also: I appreciate the amount of work required to support for HTML4 nevermind 5, and CSS2 compliance. How close is JRex to that?
Application: My intention with the webbrowser is to render a webpage from offline content. It will not need to be online content, and will simply be for file based displays = e.g. file:///C:...
Does the webbrowser have to be wrapped into a server to function, e.g. to pass files to the browser to render is how complicated? I am not keen to have to implement Jetty or another server type application just for this.
If JRex is not the solution... what then? Is it possible to start a browser implementation within Java and can Java interact with the information and traverse the Dom?
Or alternatively is there .hta equivalent in recent browsers like firefox?
If you need to have the embedded browser interact with your application code, you could try the SWT Browser control, it's actually maintained as opposed to JRex. Browser uses either WebKit or Gecko or embedded IE as appropriate, or lets you choose which one you want, so it should run jQuery and familiar Javascript. And since SWT is a JNI library to begin with they probably already have guidance on how to deploy an app that uses JNI.
You can feed HTML into the control from a string (example) or a java Url - which can point to local files or resource files in your JAR, which I assume will let you split your app into different files.
To call Java code, you need to expose it as Javascript functions. example
To manipulate the HTML from Java code, you need to call Javascript functions from Java. example
To make the previous two tasks easier, you might want to look into a JSON library to simplify passing around complex data.
Does it have to be implemented within a Java program? Could you let the user use the default browser on their machine (ie does it matter what browser)?
If not would use the Java Desktop API.
if (desktop.isSupported(Desktop.Action.BROWSE)) {
txtBrowserURI.setEnabled(true);
btnLaunchBrowser.setEnabled(true);
}
If you are using Java 1.5 try http://javadesktop.org/articles/jdic/

How to use wkhtmltopdf in Java web application?

I am newbie in wkhtmltopdf. I am wondering how to use wkhtmltopdf with my Dynamic Web Project in Eclipse? How to integrate wkhtmltopdf with my Java dynamic web application?
Is there any tutorials available for beginners of wkhtmltopdf ?
(Basically, I would like to use wkhtmltopdf in my web application so that when user click a save button , the current page will be saved to PDF file).
First, a technical note: Because you want to use wkhtmltopdf in a web project, if and when you deploy to a Linux server machine that you access via ssh (i.e. over the network), you will need to either use the patched Qt version, or run an X server, e.g. the dummy X server xvfb. (I don't know what happens if you deploy to a server running an operating system other than Linux.)
Second, it should be really quite simple to use wkhtmltopdf from any language in a web project.
If you just want to save the server-generated version of the current page, i.e. without any changes which might have been made like the user filling on forms, or Javascript adding new DOM elements, you just need to have an extra optional argument like ?generate=pdf on the end of your URL, which will cause that page to be generated as a PDF, and then the PDF button will link to that URL. This may be a lot of work to add to each page manually if you are just using simple JSP or something, but depending on which web framework you are using, the web framework may offer some help to implement the same action on every page, if you need to implement that.
To implement this approach, you would probably want to capture the response by wrapping the response object and overridding its getWriter() and getOutputStream() methods.
Another approach is to have a button "submit and generate PDF" which will generate the next page as a PDF. This might make more sense if you have a form the user needs to fill in - I don't know. It's a design decision really.
A third approach is to use Javascript to upload the current state of the page back to the server, and process that using wkhtmltopdf. This will work on any page. (This can even be used on any site, not just yours, if you make it a bookmarklet. Just an idea that occurred to me - it may not be a good idea.)
A fourth approach is, because wkhtmltopdf can fetch URLs, to pass the URL of your page instead of the contents of the page (which will only work if the request was a HTTP GET, or if it's equivalent to a HTTP GET on the same URL). This has some small amount of overhead over capturing your own response output, but it will probably be negligible. You will also very likely need to copy the cookie(s) into a cookie jar with this approach, since presumably your user might be logged in or have an implicit session.
So as you can see there are quite a lot of choices!
Now, the question remains: when your server has the necessary HTML, from any of the above approaches, how to feed it into wkhtmltopdf? This is pretty simple. You will need to spawn an external process using either Runtime.getRuntime().exec(), or the newer API called ProcessBuilder - see http://www.java-tips.org/java-se-tips/java.util/from-runtime.exec-to-processbuilder.html for a comparison. If you are smart about it you should be able to do this without needing to create any temporary files.
One of the wkhtmltopdf websites is currently down, but the main README is available here, which explains the command line arguments.
This is merely an outline answer which gives some pointers. If you need more details, let us know what specifically you need to know.
Additional info:
If you do end up trying to call wkhtmltopdf in an external process from java (or for that matter, any language), please note that the "normal" output that you see when using wkhtmltopdf from the command line (i.e. what you would expect to see in STDOUT) is not not in STDOUT but in STDERR. I raised this issue in the project page
http://code.google.com/p/wkhtmltopdf/issues/detail?id=825
and was replied that this is by design because wkhtmltopdf supports giving the actual pdf output in STDOUT. Please see the link for more details and java code.
java-wkhtmltopdf-wrapper provides an easy API for using wkhtmltopdf in Java.
It also works out-of-the-box on a headless server with xvfb.
E.g., on a Ubuntu or Debian server:
aptitude install wkhtmltopdf xvfb
Then in Java:
Pdf pdf = new Pdf();
pdf.addPage("http://www.google.com", PageType.url);
pdf.saveAs("output.pdf");
See the examples on their Github page for more options.

Categories

Resources