Convert Standalone Java Application to Deployable Web Application - java

I am new to Java EE.
I have a Standalone maven project which I need to make war deployable project. It contains many servlets & embedded-Jetty is used to run them.
Step which I have done:
Made a new Maven project with archtype-webapp.
Copy all the servlets and other classes in src/main/java
Added those servlets to web.xml
The original project contains a main which sets up the jetty server and does a initial login check and initializes all the servlets.
Can someone explain what exactly happens when we run a program as run on server.
What gets executed first (like main in standalone program).

Here is quick list of what happens-
As you open the url, e.g. http://localhost:8080/AppName, the file mentioned in the welcome file is served in the browser.
You can have an HTML form here with fields for login id and password, and the url in the of the servlet which will be invoked when the form is submitted.
Your servlet would be invoked and from thereon you can do whatever you want, call whatever class you want to call.
This is the most basic form, you might want to start with a simple web application to understand the basics before you start converting your application. That will allow you to understand the flow and then adapt your code accordingly.
Hope this helps.

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.

Is It Possible to Convert Regular Java Applications to Web Applications?

What i mean by this question is that can people start a .jar application embedded on web or they have to download it ? I made a simple online game and people nowadays will not want to individually download the game instead of directly accessing it through the browser. I developed the game on desktop, which steps should i take to make it a web application, or can it directly be converted to a web application ?
If you don't want the user to download the entire application then you must recode it using web technologies.
If you want your answers be able to launch the application via their browse (which involve the download of the application "transparently") you can just make an Applet like #huseyin tugrul buyukisik said or you can use Java Web Start : http://docs.oracle.com/javase/tutorial/deployment/webstart/
You can wrap your classes in an applet, just add a button to launch the class.
init() method will be overloaded to load the classes, start() method is to launch things. Applet is single thread so if you launch expensive loop in one of the overloaded methods, applet can stuck. You can need threads.
There is no direct conversion from jar to web application. Web servers wouldn't understand this. what you need is to
create a web application folder structure, copy jar to web-inf\lib folder
prepare web.xml as required for your application
bundle web application folder into a war file or deploy it exploded
Typical web application folder looks like:
webapp
|-*.html,*.images, *.js, *.css
|-WEB-INF
|-WEB-INF/web.xml
|-WEB-INF/lib/*.jar
|-WEB-INF/classes/*.class, *.properties
The major change in this scenario IMO would be change in routing of request to web-server instead of approach taken by your desktop app i.e. single JVM, calls directly routed to handler class instance.
Say a hypothetical case, upon save from a GUI the desktop app might be serializing data to local disk, now in case of web application it might be required to send this data to web-server (say specific SaveServlet) which takes care of this logic at server machine instead of clients.
If you provide specific usecase of your desktop app functionality, we might be able to help better.
you can do this using jnlp (java native language plugin), which will be download automatically when user visits your page (even by providing link for the same).
After that it will ask user permission to execute that jnlp (same as a jar) & your jar will be executed at the client side.
http://www.mkyong.com/java/java-web-start-jnlp-tutorial-unofficial-guide/

Invoking a method of another Java Project from a Web Project deployed in Tomcat

I have a Web Project which is running on Tomcat7.
When a user uploads a pdf file I want to pass it to another Java Project which can be called by command line (not written by me but by Apache).
Since the Web Project itself is quite huge, I do not want to include the other Java Project inside the Web Project.
Also logically it makes sense to keep the Projects separate.
This is how the flow works - User uploads a pdf file. This is passed to the Web Projects Business Layer. . After getting the details of the pdf from the "other" project I want to save it in the DB via the Web Projects DAO layer.
Can you suggest ways to call the method of another Project?
You can create a wrapper for this command line tool and host it as service. It will be easier for your webcomponent to deal with service. Also all the exception scenario can be handled by service making it easier for your webproject
You can write a shell script and call it from your webproject to invoke another project
Runtime.getRuntime().exec("script.sh");

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

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.

how to start coding and setting up a web server with java?

so now i think i have learned all basics and terminology for java. but what i don´t know is how to code and display a web page with Netbeans in Java.
the most tutorials contains lots of talks about different technologies "Java uses Java Beans, JSP and servlets" and so on. Where can i find short practical tutorials that actually teach me where to code what and then compile and where to put all the files (war, jar, ear..) in Glassfish to be able to see the output from a Web browser. Simples things that makes one understand all these different "layers" which are just classes using classes. Feels like i never get to know how i can put up a web server with Java cause I can´t find this kind of tutorials.
Would be great if someone could send some links to such practical stuff.
Thanks.
This is the first such document I found: http://www.java-tips.org/java-tutorials/tutorials/introduction-to-java-servlets-with-netbeans.html
More:
http://netbeans.org/kb/docs/web/quickstart-webapps.html
http://blogs.oracle.com/jonasdias/entry/webservices_with_jsp_on_netbeans
http://www.fuzzylizard.com/archives/2005/09/18/628/
http://cit.wta.swin.edu.au/cit/subjects/CITP0014/tutorials/netbeans/tomcat/Running_Tomcat_from_Netbeans.html
http://supportweb.cs.bham.ac.uk/documentation/java/servlets/netbeans-webapps/
I even found a small ebook on this (PDF!) http://www.comp.dit.ie/bduggan/Courses/projects/Getting%20Started%20with%20Tomcat%20&%20NetBeans.pdf
How do I code and display a web page with NetBeans in Java?
Let's go! Fire up NetBeans. I'm using NetBeans 6.7.1 with the Java EE stuff installed, and I've got a GlassFish installed and tied up, so I don't have to care about that stuff. Your setup might differ in the details.
Do a File->New Project, and pick "Java Web" from the categories. Select "Web Application" and hit Next. Enter a project name and tweak the location, if liked. Hit Next. The next page should have a server selection drop-down; as hinted above, mine has "GlassFIsh v2.1" selected. That's fine - as long as NetBeans can interact with a Java application server of some sort, this crash course will run okay.
Make a note of the "context path" - this will be based on the project name, and basically forms the base of the URL at which your application will reside. Hit Next. Ignore the next page, for now, which talks about various frameworks, and hit Finish.
Churn, churn. You should eventually see your web project created. It's a very simple application which contains a single JSP file, and that will be open in the main editor. It's got a bunch of HTML in it, and some JSP syntax.
Take a look at the project structure. You've got a "web pages" folder which contains a WEB-INF directory, and an index.jsp file. That's the same file you're looking at. WEB-INF is a standard directory which contains the metadata used to deploy your application, and also the compiled classes that power it.
The only thing you should need to do now, in order to get to the original objective, is to hit the big ol' Run button, or right-click on the project and select "Run" from the menu. NetBeans will compile, and then fire up your application server and deploy the application to it. Finally, your web browser should pop open a new tab with the classic "Hello, world" page in it.
At this point, what do you actually have? You've got an empty web project with a single JSP file in it. You could customise it, but that's maybe not very exciting. What you're really looking at is a basic framework in which you can apply your learning of JSP and of servlets as you get to grips with them.
How to proceed with said knowledge transfer? I recommend a decent book or two. The one I used to get going was "Beginning JSP, JSF and Tomcat Web Development: From Novice to Professional" (Zambon, Guilio; Apress; ISBN 1-59059-904-7), which has a decent beginner's guide to how JSP and servlets work together, and a handy reference guide for the former.
As soon as possible, you're going to want to migrate away from raw servlets and JSP to tying them together in a slightly more flexible way using one of the frameworks I skipped over earlier. I'm not going to tell you which one to learn; there are several pretty decent ones. Try Spring MVC, or Struts. Once again, I'd suggest getting a decent book.
I would start by taking a look at servlets and JSP. I found this book helpful when I read it: Head First Servlets and JSP
Netbeans comes with many sample projects, create a few and browse the source code.
A already working sample is always a good starting-point for your coding.
If you're also interested in using Eclipse, which makes it very simple to create and deploy web applications here's a nice tutorial - WTP Tutorials
Hi to start code and setting up web server with java follow the following procedure.
First you need to install Apache-Tomcat or Jetty any web container or servlet container.
And you have set the classpath of servlet-api.jar file.
Next you will have to save your web-page code stuff in web-apps folder of tomcat.In web-apps folder there you have to create a web-inf folder in which web.xml file is stored.And after writing servlet and jsp programs the compiled class files are stored in classes folder of web-inf folder.JSP files are stored along with WEB-INF folder.
You have to keep all the jar files in LIB folder of classes folder.
The web container will take care of initalizing servlet, loading class using inti method.Using service method it will create two objects request and response.
The Java EE stack is quite a mouthful. I suggest you just look at writing a Web Application (WAR), and deploy it to Tomcat.
Unfortunately doing a full WAR-file deployment is rather tedious so you generally want some help from your IDE or the web container.
The easiest place to start is probably installing and starting Tomcat and then fiddle with the files in the file system. There is an example application.

Categories

Resources