Processing JSP From Within Java Code - java

Does anyone know if it's possible to process a JSP page from within Java code and get the resulting HTML string?
I'm working on a project that has a Spring Boot application. One of the requirements is to have an end point that returns JSON. The JSON is suppose to contain one or more properties that contain HTML strings that were generated by JSP templates. The JSON will contain other other properties as well.
I've done some research and I've found examples showing how to process Freemarker and Thymeleaf templates from within Java code. However, I've been unable to find any examples showing how to do this with JSP. Ideally we would like to use JSP as that's what our developers know and in the interest of time there would be no learning curve as opposed to if we went with Freemarker or Thymeleaf.
Has anyone out there done this before with JSP?

Related

Java EE Clarification on Workflow for JSP and Servlets

I am attempting to learn Java EE. After struggling through trying to understand the Java stack, it appears that that a basic dynamic web application can be accomplished by focusing on JSP and Servlets so I want to start there.
In Net Beans I have a basic project set up with a tomcat server and when I hit run I can get the web page to generate and display the default index.jsp page. I have added a css file to the project and this jsp page looks like any standard html page now. I then create a new servlet. I was also able to create a link in the index.jsp page so when I click on it, it calls the servlet.
What I don't understand is why when I create the servlet in Net Beans, the processRequest method comes pre-populated with several html print statements. I could easily make this page look like the jsp page I started with, making me wonder why I even need a JSP page. It appears the entire application could be comprised of servlets.
Could someone please explain the proper interaction between the two? It appears the index page can contain links that call various servlets, perhaps to access a database of comments, then do I create the response table in the servlet, or would I pass that information to another JSP page?
Understanding these basic workflows would really help. Thanks
JSP also a kind of a servlet . Right after you run the jsp , go to your netbeans project directory and right click the jsp and select view Servlet then you get the servlet representation of your jsp file. Actually jsp file is there to perform front end design easily. Without jsp It is really difficult task to perform front-end developing stuff inside servlet.
And you should keep in mind there is no such a thing called proccessRequest in javaEE . It is automatically generated by Netbeans once you create a new servlet. So just stick to basics. Erase all the auto-generated stuff from the class which has extended HttpServlet. And then you can override the required HTTP method/methods. (doGet , doPost etc..) You should take a look at this for more infromation ...
And this page will also improve your knowledge on javaEE

Access Spring MVC Model data in imported js file

I am working on a project with Spring MVC with java as the backend and HTML(JSP)/CSS/JS as the frontend.
In my controller in java I am adding data to the model like this
model.addAttribute("someData", theData);
which I can easily access in the JSP file like this
${theData} or "${theData}"
The problem for me is that this only works in my JSP file. I am importing other css and js files there like
<script src="<c:url value='/resources/javascript/main.js'/>"></script>
and they seem to have no access to the model. I understand that there is no model at runtime on the clientside, but the javascript files are served by the server the same way as the HTML/JSP file, so why does this not work? Or is there any kind of setting I have to use?
I realize that inline js code in my JSP file works, but this is not very flexible and just not a "real" solution for me. Also I would like to avoid additional calls to the server (like AJAX) to get my data.
Thanks for your help!
...but the javascript files are served by the server the same way as the HTML/JSP file...
No, this is not true. The static JavaScript, CSS, and HTML files are being served by the web server as-is, with no processing. The EL snippet in your example...
<script src="<c:url value='/resources/javascript/main.js'/>"></script>
...is only generating a concrete URL based upon the relative URL that you have provided as an argument to the function. There is no knowledge embedded of what main.js is or does. Your JSP files are explicitly processed by the Java servlet container. This is why it has knowledge about your application, beans, models, and all. This is a gross oversimplification, but hopefully it gets the point across.
Edit
You can use JSON from static files in your JSP, but you have to read, process, and store them as Java objects in a bean in your application. I am not sure why you would want to do this, though, since it is so easy to read JSON data via JavaScript.
willOEM gave you a good background. So static files don't know about the model values, but also there's no need for this as you can always code your way out.
If you're calling any function from your main.js within your jsp, than at that point you should use the ${theData} as a function argument. If not, you can always use a data attribute on a dom element, store your model variable there, and access it inside your js file, so something like
<div data-the-data="${theData}">Lorem ipsum</div>
I have "solved" it now by defining the databinding inline in the html document
var theData = "{theData}";
and then using that in my standard javascript file.
Not really perfect but seems to be the best working solution.

Integrating AngularJS app with Java Tapestry platform- Tapestry throwing mark-up errors

I've written a stand-alone AngularJS app, and am now attempting to integrate it into a Java Tapestry framework. Loading all the JS is working fine (printed "AngularJS running" to console from controller to confirm), but I run into problems once I introduce my mark-up. I am getting Tapestry mark-up errors that make me unable to load the page, for example:
"Failure parsing template classpath:path/to/my/app/TableViewBrowser.tml: Attribute name "tooltip" associated with an element type "p" must be followed by the ' = ' character."
What it's objecting to is mark-up like this:
<p ng-class="{checked: case.beratungsanlass}" tooltip>{{ case.beratungsanlass }}</p>
I already went through and replaced all the in-line conditional statements with function calls, because it didn't like less-than or equal-than signs in the mark-up either, but I wonder if I'm not better off figuring out a way to instruct Tapestry to ignore this mark-up, rather than rewriting my entire app mark-up.
Has anyone done this before? Which would be the better option: rewriting the Angular mark-up so that it conforms to the standards Tapestry is using, or instructing Tapestry to ignore portions of the mark-up? And if the latter, how can I do this?
Tapestry requires that templates are properly formed XML. You'll need to do something like:
<p ng-class="{checked: case.beratungsanlass}" tooltip="true">{{ case.beratungsanlass }}</p>

How to render plain html in Play 2.0

I'm working on learning JSP and the Play framework, and I understand that it runs on Scala and renders views based on templates, but what if I just want to use plain HTML rather than scala templates?
The situation I'm in is that I'm designing the site to match a visual template, so I'm using Dreamweaver to build the html files. I really like Play framework though, so I'd like to continue using it. So, what are my options here?
I don't get. Play's views are not just nice html files, of course you can (or even should) use your favorite tools for design part, anyway you have to also learn how to include a dynamic parts in it.
Of course you can use DreamWeaver for that task as it has feature for editing source code. But I can ensure you from my own experience, that there are better tools for every-day work with Play's views than DW.
You can also use plain HTML in your /public folder however in this scenario you won't be able to make it dynamic, so it has no sense, as you can create the pages without any framework - just using static files created with DW.
In general words: you need to verify your needs, cause from your question I read: "I like Play framework, anyway I don't want to use it for its job..."
After-comments edit:
You don't have to make views dynamic. If you won't pass any arguments into the view and will put there pure HTML it will be 'relatively cheap' way for displaying static pages as well. Just you need to remeber to leave first line of the file empty. So you don't need to use File index = new File... instead just put your bare HTML code into ie: app/views/staticContact.scala.html and then use an action:
public static Result staticContact(){
return ok(views.html.staticContact.render());
}
On the quite other hand, last time I was wondering if it wasn't better to put HTML code of the static pages into the DB, in such case you could create an editing page, where you could change HTML without redeploying the application. All what you will need it was just fetching HTML from DB and displaying it in one generic view. For better performance you can use included Cache implementation.
GET / controllers.Assets.at(path="/public/html", file="index.html")
This is working for play 2.0.1 for /public/html/index.html file

Extracting JSP page content

I am working on a Mail API module where i have to develop a generic functionality of sending Mails with respect to various functionality in the Appliication.
There is one proposed functionality where the module will expose a method which along with some required parameters will take nane of JSP template.
It expects that this will extract the content of the JSP which will be a well formated mail template and send mail.
Is there any way in JAVA where i can extract the content (HTML) from this JSP page so that i can use that HTML content in to the Mail.
Thanks in advance
You have two paths to go, with the first one being a little shorter:
use new URL("http://site.com/url/to/page.jsp").openConnection(), get the InputStream and read the contents - this will be as if your server sends a request to itself and gets the result
use a Filter and a HttpServletResponseWrapper, and return a custom Writer / OutputStream. Each time something is written to the writer / stream, delegate it to the original object, and also write it somewhere where you can read it from later. This explanation is not sufficient, because this is less likely what you need, but if you are willing to take this path, tell me.
That's, however, not the way this is usually done. You'd better use some templating technology like Freemaker or Velocity for your email templates.
It sounds like you're trying to use JSPs as a templating engine for your email, which is something it wasn't intended to do. There are other technologies out there better suited for what you want, like Velocity and Freemarker.
However, if you're dead-set on using JSP, you have two options :
1) You can use the method described by Bozho to, essentially, connect to your own site and have it generate the content for you
2) You can write the JSP, compile it at compile time, and include the generated servlet file in your email generator and mock the inputs to the Servlet API that the generated JSP servlet will be expecting to extract content from your compiled JSP.

Categories

Resources