Java to JSP. How to? - java

i have written a java program which currently runs as a desktop app,
it is returning all results accurately as required in the console. I am now faced by a challenge to convert this application to a web application (JSP).
Currently, the code for my projects is in user/workspace (as i am using eclipse)
and my tomcat has been installed at c:/server/tomcat..
I am basically a .net developer and know peanuts about java web development. Can someone please guide me as to how i can convert my current project into a web application?
how do i include the project in user/workspace in the jsp file? havent done it before so its as confusing as hell.
it will be most helpful if you can point me in the right directon, or even provide some links so that I can use thm as a pointer
Many Thanks in Advance.

I suggest not to use the JSP technology anymore unless you have to (-> customer demands it).
Instead, get GWT or Grails. Both come with tools to setup a project in Eclipse from scratch that compiles and can be run with the press of a button. Also, both technologies are much more advanced than JSP and you'll find them much more simple to understand as a .Net developer.

If all you want do do is to display the output in a html page, the simplest ways is still to use jsp.
You can do as mentioned in kgiannakakis answer
If your class already returns a string with a nice output, everything you need do this.
<%
MyClass = new MyClass();
%>
<pre> <%-- pure html, preformatted --%>
<%= myClass.formattedStuff(); %>
</pre>
No fancy html formatting, though. Just a text dump.
You can use for-loops and if-clauses and what not, as everything between <% %> is normal java.
If you need more functionality, You should probably not bother with jsp at all. (And absolutely not at all with JSTL, which would be a total waste of time if you are not intending to start a career writing web apps for big enterprises.
Perhaps GWT :-) Its almost java.

I recommend reading a JSP tutorial. It isn't that hard. To get you started quickly, these are the required steps:
Create a web application. It is better to use a IDE like Eclipse or Netbeans to do so. A quick and dirty solution is to create the jsp under webapps/ROOT in Tomcat
The jsp is like an html file with some special tags that allow it to run java code.
This is how you include classes or packages:
<%# page language="java" import="mypackage.MyClass" %>
You put your Java code inside a <% %>. You can practically put all your java code there.
Replace System.out.println in your console app with the following tag:
The mymessage will be directly printed in the html source.
Finally start Tomcat and point to your jsp file.
For a beginner, I would recommend to start from a tutorial for Eclipse or Netbeans.

The technical part of converting your project into a web project is fairly easy. You can simply create a new static or dynamic web project using eclipse and copy your code into the source directory of that project. Google it and you're there.
The next step would probably be to understand how your application is deployed, and how do you configure it.
Keep in mind that you switch architecture here. A web application is a client server application that executes in a servlet container which comes with some rules unless all you need to do is print some text into a browser.
I suggest that you start reading here:
http://java.sun.com/j2ee/tutorial/1_3-fcs/doc/Servlets.html#99975
http://java.sun.com/j2ee/tutorial/1_3-fcs/doc/JSPIntro.html
The first link refers to Servlets, the reason I point you to Servlets first is that JSPs are actually servlets, and servlets are easier to begin with.
Hope this helps.

Try this, http://simple.souther.us it has all to get you started.

Related

Is it possible to get the whole source code of a jsp page including the java code within tags in it from a webpage?

I wanted to know if there is a way or ways to get the whole source code of a JSP page, which means the HTML code and the JAVA code within the tags, using any tools or any libraries. I know its possible to get the HTML code programatically. But I am not sure if one can get the JAVA code also. Any help will be greatly appreciated. I am talking with respect to application security.
unless you have access to jsp, it is not possible. JSP/Java code will be translated HTML before sending to browser.
I know this was a long time ago, but I'd like to point out that there was a very well documented container vulnerability on JRun that allowed you to see the full JSP source code.
http://server/page.js%2570
Where %25 is a URL Encoded "%" and 70 is the hexadecimal value for p.

Gwt and jsp in same project

i was looking for similar title here but i didn't find much info, so if anyone can provide some example or url or any approach how to do it.
For example: is it possible to call a jsp from a gwt widget?
or communication (server - jsp) in gwt project and so on..
Is there any limitation is using jsp in gwt project?
Thanks :-)
GWT compiles down to html+javascript (static files), while JSPs are internally compiled and behave like servlets. You can configure URLs to point to both (via your server config and/or web.xml).
Both GWT and JSP allow you to go to a new URL. In GWT you can use Window.Location.assign(url) (this will load new URL and close current GWT app).
Additionally GWT allows you to load data by making XHR calls to URLs: http://code.google.com/webtoolkit/doc/latest/tutorial/JSON.html
So:
Yes you can goto JSP pages from GWT app.
Yes you can load data provided by JSPs into your GWT app.
In any jsp applications actions lead to URLs. The URL can point to a GWT module.
You will find applications which offer a back-end for the administrator in GWT, while the front-end runs in Struts2. Konakart e-shop is such an example.
My impression is that GWT will replace the other frameworks on the long run. Everybody will want Rich Internet Applications very soon.
On the other hand in the early 80's i was thinking that in very few years everybody would be using Unix, which never happened, but Unix did quite well after all...

Programmatically invoke the JSP parser

I would like to invoke the JSP parser programmatically. That means that I want to be able, in Java, to 'run' a jsp page and get its output, without sending it back to the client (I actually want to save the output to a file). I don't want to 'forward' the request to the JSP page. I want to be able to do that on several JSP pages in a row.
What is the best way of doing this?
I have found this question, but BalusC doesn't really answer the question directly.
In case you are wondering, I need to do this is because I want to 'precompile' the JSPs for using on other platforms than a Java servlet container.
EDIT
What I need is not the .class file, but the HTML output. Indeed, that will be static once generated but I have some custom jsp tags and I want to leverage the JSP parser to expand them.
I'm not sure that I understand the point of all this.
JSPs are parsed and precompiled to .class files. They're Java servlets at that point. You need a servlet engine to execute them.
If your intent is to capture the generated HTTP response as the "precompiled" response, it would suggest that there's no dynamic content and the response is the same every time you send that particular request. If that's the case, what you've got is static HTML.
If I'm correct, this would seem to be a poor way to generate such a thing.
If your wish is to precompile JSPs to .class files, the problem is that different Java EE app servers use different JSP precompilation engines. You can't precompile JSPs using Tomcat and use them on WebLogic.
The best way to get the html output from a jsp page is to actually deploy it to a real webserver and then call the page and save the rendered output.
If you want to automate some part of this, you might want to look into using a testing tool that exercises through the real interface, such as Selenium or that emulates the browser, such as HttpUnit.
But this is doing much more than just invoking the JSP compiler.
Maybe it would be more practical to use template engines like http://freemarker.sourceforge.net/ or http://velocity.apache.org/
Freemarker even seems to support JSP Taglibs: http://freemarker.sourceforge.net/features.html
Is your JSP dynamically generated. If so then you are stepping into a potential disadvantage situation wherein your JSP will be compiled again and again, leading to performance issues.
However if you could have a single large JSP with all the rules that you need to prepare your display, you could use HttpClient to make a call to your own JSP and that would return the HTML. This would ensure that you are not app-server dependent. If you use JSP Parser you are going to be vendor dependent.
But if your JSP is being dynamically constructed then you should look at options wherein your HTML can be generated on Java side. But if it involved rule based HTML creation, you are better off creating it in Java. You can use Apache Jakarta ECS library for this.
And yes JSPs are not meant for this purpose.

How to make a nice jsp GUI without using HTML tags?

I'm new to servlets and JSP and I'm trying to discover forms, and how to make my own web form. I know HTML but and I see examples for using HTML tags in the servlets, but isn't there a more higher level way to just say "Make a form here, make a table there" without getting my hands dirty with HTML tags?
I want to build a war file and drop it on my jboss/tomcat server for testing. Then when its ready to go, I want to be able to distribute that war file and for it to be plug and play.
The end goal is to have a web site with some interaction (dropdowns, text fields) and some graphs that are drawn based on the user inputs.
Sorry so vague, I need some direction in the Java GUI department.
Check out Java Server Faces. (http://java.sun.com/javaee/javaserverfaces/)
ps, I just noticed Wikipedia has a nice list of web-application frameworks for the various languages/environments.
One of the most difficult things to do absolutely right in a browser, is form submittal. That is, form submittal with error recovery.
Hence, it is a natural sweet spot for a helping library and many exist. I would suggest you look into JavaServer Faces where 2.0 with facelets is very powerful and default in Java EE6, but right now only ships with Glassfish 3.
If this is for learning, I would suggest using Glassfish instead of Tomcat, so you get a prepackaged learning environment.
You may be interested in the echo framework.
Try Google Web Toolkit
code.google.com/webtoolkit/

Using Java code on embedded HTML instead of JavaScript?

I am working on a project which is basically a Java application with an embedded IE browser (using JDIC 0.9.5) to display custom HTML files (stored locally that I created). I have a test HTML file with a JavaScript function that checks a simple form with checkboxes and alerts the user with a dialog stating which checkboxes are checked.
My question is, is there a way for my Java application to do the same procedure on the embedded HTML form instead of using JavaScript. I want to keep my application and HTML files simple without the clutter of JavaScript in my HTMLs or a pile of .js files.
Thanks for any help and guidance!
You have two options. Either shift the project to run the Java on the server side using JSP technology inside a web container like Apache Tomcat or Jetty, or write you web page to open up a Java applet.
The applet route allows you to run the code on someone else's machine, and as a trade off you will have to run the application in a strongly security constrained environment. After all, if someone were to run code on your machine, you wouldn't want it able to access your disk, etc.
The JSP solution will have you running the code on the same machine as your web server, since you (probably) control your own web server, the code will not be ran with as many security constraints enabled. This means the code can make requests to other machines, write and read files, etc.
You could replace your model with a client-server model using Java Server Pages (JSP).
If you are displaying the page through an embedded browser it is very unlikely that you will be able to get access to the DOM via Java.
One option is to use GWT javascript compiler to code in java and then convert to javascript. If it will only use IE, then you will only need to keep one of the generated .js files, so the clutter will be low.
Since you're embedding your HTML files in your own Java program, I would recommend you to use one of these 2 approaches:
1.- Use Javascript and structure the files cleanly, is not that complex.
2.- When you do the POST check the values in your Java code and return a new dynamically generated HTML file with needed info
I sincerely recommend you to follow the first option. Other options to work with Java in HTML would be JSP or GWT, but both will require a proper J2EE server, which would be overkill in your application

Categories