Extracting JSP page content - java

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.

Related

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.

How to share the same properties file for Java and jQuery i18n

I try to use properties file to manage the string text.
For now , the java's property file is located at src/org/XXX/lang/bundle, and the jQuery.i18's is located at WebContent/i18n.
Is there a way to share the same property file ?
It is possible to share the same properties file. One way suggested in a comment was to simply copy the file over; it could be easily done by the build script. Frankly it seem the easiest solution.
Other ways to share the file:
A servlet or a web service that will return the required content on HTTP GET request (i.e. dynamically generate required language file) - requires some coding,
You can use the same method you are using to translate the html templates (whatever technology you're using, JSP, Thymeleaf?) to either generate an array to be used by jQuery or create pre-translated HTML pieces (i.e. Handlebars templates).
The choice depends on the technology stack as well as requirements (i.e. response time); definitely it will be faster if you already have pre-translated HTML parts that you can reuse, generating HTML from strings always brings overhead.

URL Designs with JSP

I am new to Java with knowledge MVC architecture and JSP.
I have created a website with multiple JSP pages (on MVC architecture) which is data dependent and data is displayed as the request parameters passed in the URL.
My URL is something like this:
localhost:8080/web/Area.jsp?name=stack&place=internet
What i want to achieve for better SEO is something like this
localhost:8080/web/stack/internet
I have tried URlRewritefilter but the XML file being created is too big and is causing Java out of memory errors.
I aim to achieve this through java code/framework (please suggest which I should learn) where in the requested data can be processed by the jsp in backend and achieve a cleaner URL.
What you want is not a Java tech but a server URL rewrite tech. If you are using Apache just try the .htaccess.
You can try writing a JEE filter which does a kind of pre-controller work, translating that SEO-friendly format you write about to the format your app needs (for example, if you had a struts 1 application with a url like
somemachine:xxxx/web/myAction.do?name=stack&place=internet
it should be trivial to write a filter which translated something like
somemachine:xxxx/web/myAction/stack/internet
into the real URL and managed it internally to the server
http://docs.oracle.com/javaee/5/api/javax/servlet/Filter.html
http://docs.oracle.com/javaee/6/tutorial/doc/bnagb.html

Web application to serve dynamic text substituted files

I have a web application which caters javascript files
example:
/static/com/example/sample.js
I want to do make customized (some string substitutions) version of this file made available for international traffic under /dynamic/{country}
/dynamic/en/static/com/example/file1
/dynamic/jp/static/com/example/file1
/dynamic/au/static/com/example/file1
I wish to dynamically generate these versions of file by doing these string substitution operations.
I envisage having a single servlet mapped at "/dynamic". This would take the url(/dynamic/en/static/com/example/file1), find the country code(en) and source file (/static/com/example/file1), and do corresponding translations
How do i configure servlet to get the url
Read the actual file using relative path
Are there are better approaches to help my situation?
It sounds like you are trying to do i18n for javascript.
You need to make it so that your language conversions are stored separately from your code and that your code will pull the correct translated message as opposed to being preprocessed.
That is your code ("sample.js") will pull the translated message. Your translated messages will be stored declaratively (like a big json object with keys, or a properties file) in their own file.
Take a look at http://plugins.jquery.com/project/jquery_i18n_properties which works very similar to how Java does it.
Your approach to the problem is interesting but it is generally not what people do.

asynchronous file upload with java servlet

Here's I want to do, I want to upload a file that will be processed by a servlet. I would use Apache Commons - File Upload to handle the file to be uploaded.
I've seen the gmail-like AJAX file upload, where there would be a hidden iframe that would later be populated with a javascript to stop showing the upload image or displaying a message that the upload is succesful. However, this uses PHP, where the php file to handle the file upload would include the javascript inside the iframe.
My question is, how would I do this in Java using servlets, without resorting to JSP and imitating the above implementation on PHP. I don't even know if this is possible, so please guide me on a good implementation (without external libraries except for commons fileupload).
Note: I am aware that there are libraries out there that could do this easily, but I first want to know how this happens, how this is possible, and to dirty my hands and learn this.
Edit: Just to add, I would use the streaming API of Apache-Commons FileUpload
It is exactly the same.
The client makes an HTTP request to the server (by submitting a form).
The server responds with some HTML (which links to or embeds some JavaScript).
Switching from PHP to Java is just a drop in replacement. You don't need to change any of the JavaScript. The user guide tells you how to set it up.
http://oreilly.com/pub/a/javascript/2002/02/08/iframe.html is the best idea to file-upload. i done file upload using hidden iframe. Please consult with attached link.

Categories

Resources