How to call JSP from Java stand-alone program - java

How can I call a JSP file from a Java application, and pass to it a Java Bean, so that as a result I can get a rendered HTML code as output.
The Java program is a stand-alone application, that runs by someone. No servlet please.

If Non-Servlet environment & it is a stand-alone application so I think you can use any String template such as Velocity, FreeMaker instead of using JSP.

A JSP is a servlet too. So you need all those Java EE classes, and hence also an implementation by some provider (Java EE container). You could use jetty as an embedded Java EE container.
You can then via URL get the generated HTML page.
If you want minimal JSP functionality, more a scriptable HTML template engine,
look at StringTemplate or Velocity or whatever.

Mostly a complement to #AppsLandia's answer.
Even if it looks like a template engine, JSP is not. JSP files are first translated into Java source servlets and then compiled to .class normal servlets. That's the reason why they cannot be used outside of a web application running on a servlet container like Tomcat or Jetty or...
Velocity and FreeMarker can be used in web applications, but they are general template engines, so you should have a look to them.

Related

Is JSP page a java class?

JSP page in its lifecycle translated into .java file, but is JSP page itself a java class?
Confused and need help.
JSPs are compiled into Java Servlets, and Java servlets are classes. So yes, the JSP is compiled to a Java class. The name is usually automatically generated, and is visible in any stack traces (if you throw an Exception).
From here:
A JavaServer Pages compiler is a program that parses JSPs, and transforms them into executable Java Servlets.
And from here:
Java Servlet technology provides Web developers with a simple, consistent mechanism for extending the functionality of a Web server and for accessing existing business systems. Servlets are server-side Java EE components that generate responses (typically HTML pages) to requests (typically HTTP requests) from clients. A servlet can almost be thought of as an applet that runs on the server side
Since applets are classes, hence, a JSP is a class.
The intend behind JSP is Actually makes easier for JAVA developer + front end Developer to
write not only Java code also need to write HTML code and other which
is easily stuff with html code too for full fill the task complete.
so, let's say, my jsp page is home.jsp, then it's translating from .jsp to .java by home_jsp.java which will enclosed all non-java code into java.
and finally home_jsp.class file generate which will run on JVM.
JSP page life cycle and many of the capabilities of JSP pages (in
particular the dynamic aspects) are determined by Java Servlet
technology.
Now, i hope you are little bit clear on your query that was,
JSP page in its lifecycle translated into .java file, but is JSP page itself a java class?
Best of luck...!!!
A JSP pages compiler is a program that parses JSPs, and transforms them into executable Java Servlets and that java Servlets is a simple java class.

How to get servlet code when the jsp file is processed in the JSP engine

I am using Weblogic for deploying my Java EE applications. I am using JSP technology for my project. I know that the jsp files are processed in the jsp engine and in the life cycle of jsp, it is converted into the servlet code.
Can you please tell me that how I can get the generated servlet code?
This will vary based on the weblogic set up on your box. If weblogic is compiling your JSPs (you are not pre-compiling them) then it may be under domains\<YOUR_DOMAIN_NAME>\servers\<APP_SERVER_NAME>\tmp\_WL_user\<APPLICATION_WAR> .
It is generally a good idea to pre-compile your JSPs for performance reasons and add the servlet mappings in the web.xml. You can see a reference to how to do that in weblogic here under the section "Precompiling JSPs".
In my application I am using Maven and the JSP servlet compiles are sent to the war file in the \target folder of the maven project, i.e. \target\.war\WEB-INF\classes\jsp_servlet. If you find the jsp_servlet folder you will find the compiled Java servlet code and the class file. With Eclipse you could use the debugger and set breakpoints. A word of caution, the servlet code is complex. If you are trying to debug JSP I would use other techniques, such as temporarily embed EL commands in the JSP to display data and functionality. Also, JSP should have little or no business logic so it would also be advisable to focus on the model and controller.

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.

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

JSP PageContext from a Groovlet (Groovy Servlet)

I'm trying to use Groovlets in place of JSPs in an integration framework but one of the vendor's libraries relies on the javax.servlet.jsp.PageContext available in a JSP. I found the GroovyPagesPageContext class that's part of Grails for GSPs.
Anyway to use Groovy in this situation and have a handle to the jsp PageContext? I have not tried using GSPs because there's a significant amount of logic involved.
Since you are replacing your JSPs with Groovlets which are Groovy Servlets, how are you using this vendor's library? If it relies on the JSP API, I'm assuming that it is normally used in a JSP, perhaps as a custom tag library? How are you using the library in a Groovlet? You would have the same problem trying to use a custom tag library inside a plain Servlet.
If you'd like a standalone class that can act as a stand-in for a JSP Page context, have a look at the
JspPageContext.java class that I wrote for Rayures so that I could wrap a JSP tag library in plain Java code.

Categories