Escaping HTML characters in JSP without special library - java

I'm a new programmer at a software house, and let's just say I can't make too much modification that includes a major changes, for example like adding library. In the current project, we're using JSP and Servlet, and some other advanced GUI that I never knew (they said it's a derivative of Eclipse, called Enfinity). The Enfinity also hides the libraries under obscure locations, and it's very different than in Java. So I don't think I will able to understand about the library location too, moreover adding some new library.
The problem here, I need to escape HTML characters like &, <, >, ", and ', but when I search solution on the internet, usually the solution involves using JSTL ( c:out or ${fn:escapeXML} ) or importing a library (Spring's HTMLEscape, or Apache's StringEscapeUtils). JQuery, on the other hand, is imported, but sadly, not related to solution. But the problem is JSTL is not part of the library readily imported into the project. Java, JSP, and Servlet are kinda new to me, as I didn't get Java at all in my college, so I don't know either what library is standard in JSP (already present, without I have to add it physically). I don't even know whether the Apache's StringEscapeUtils is present or not. Do you have any suggestion / codes on how I should escape the HTML characters under my circumstances? Thank you very much.

If your target platform is really Enfinity - as you are stating in your questions and in the tags - you should be using the Enfinity constructs even though this is not completely what you know from JSP. Please allow me to reopen this old thread and try to help you with that.
Enfinity got an own "templating language" called ISML. In the end ISML is precompiled to JSP. You can find a documentation with any installation of the Enfinity application server (a PDF called enfsuite_dev_programming). You should ask your project manager or build engineer if you don't have it available.
On the other hand I read from your statement that you possibly have the Enfinity Studio available (which is the IDE of Enfinity - a derivate of Eclipse. You should be able to access the developer guide through Enfinity Studios Help Menu. This menu may have some errors in some versions of the Studio unfortunately. However, you can get there through Window > Show View > Other > Help. On bottom of the help window is a "Content" link that will take you to the overview. The developer guide is under the table of contents link Enfinity Suite Application Programming Guide.
However you get to the guide: in the appendix you find a section "Reference > ISML Tags / ISML Functions / ISML Modules". Browsing through it you will find the function:
<isprint value="#value#" encoding="on|off">
Encoding is "on" by default and this statement will do exactly what you need: it will encode all HTML special characters in #value#. The special here is that the key value matches to an object in the so called Pipeline Dictionary which is a construct storing objects coming out of the Enfinity business logic workflow layer (so called pipelines).
This pipeline dictionary can be manipulated in JSP using:
Map<String, Object> pdict = getPipelineDictionary();
The dictionary is a standard java Map and can be manipulated using the known operations. However, the preferred way would be using pipelines or at least the respective ISML tag
<isset name="name" value="#value#" scope="request|session">
A full example for usage with JSP/ISML would be:
<%
String myString = "<b>Test</b>";
getPipelineDictionary().put("myDictKey", myString);
%>
<isprint value="#myDictKey#">

You can import org.apache.commons.lang.StringEscapeUtils and add its jar file. That are not by default present in jsp/servlets. It will provide you facility to escape characters from html, mysql, xml etc. Also you can make your own method to check for the character sequence and then use it as a escape function to escape data you want.

Related

How do I replace header and footer instantly in multiple web pages?

I'm setting up a new web app and came to know that the company name is spelled incorrectly in all the web pages. So, I want to change it in all of them. I am using eclipse neon.
Is there any way to add or replace text from all the web pages in Eclipse ? or Will I have to change them manually one by one?
Tried using ctrl+shift+R but it's for the change of method or variable name in eclipse.
The eclipse IDE is mainly a programming tool, and as such it also supports:
searching for files using wildcards
replacing content using regular expressions within all finds matching your condition
Simply turn to Search"->"File" and enter your search text, a file pattern and then use replace!
And of course: the real answer is to step back and look into what you are doing! Such repeating information (that has to be always the same in many different places) should always come out of 1 source file, not out of 120 different files. That comment about "don't repeat yourself" is the real solution here!
I think, it is better to create a separate header page and separate footer page and include them in all web page. So next time if you make this type of mistake you should not think about the all page, you can just change in one page and see the change in all page.
Eclipse - Search - File..
in this example will be replace "conpany" in all jsp's with 'company'

Should I use webpack or just html imports when building a single page app (large scale)

I have been reading this tutorial about html imports. However I became more confused after it. I'm coming from an Angular 1 & 2 background with some React recently. I have been progressing trough all the stages of javascript evolution. Using scripts in the header than require.js than system.js and now I've been doing just fine with webpack.
Reading about these imports it becomes a bit fuzzy. I don't have a clear image when to or not to use them. If webpack is used, do I still need html imports (probably for 3rd party stuff)? Certainly I love typescript and I definitely don't want to build the js artefacts myself using the command line like in the early days. I feel webpack is a must-have.
I know this question can be viewed as vague and closed, but I'm really stuck. Right now resources on web components are few and I can't manage to find my way towards a clear and confident answer.
P.S I want to use vanila components, not polymer stuff.
Some of the major browsers(i.e. Firefox) are not planning to ship HTML imports, ever.
Quoting from https://hacks.mozilla.org/2014/12/mozilla-and-web-components/
Mozilla will not ship an implementation of HTML Imports. We expect that once JavaScript modules — a feature derived from JavaScript libraries written by the developer community — is shipped, the way we look at this problem will have changed.
It will still be good to learn about the standard though, because of its unique feature of HTML parsing, which won't be possible with any representation of HTML in JS modules(which are being pioneered as replacement for HTML imports)
But for now, it seems like HTML imports are not getting anywhere, until something comes out of HTML Modules proposal, which plans to rebuild HTML Imports functionality using the ES Modules.

Translate English to some other Language in RAD or Eclipse

I have written comments in English in my Java files but now my client wants it in Spanish, So is there any tool/Plugin available in RAD or ECLIPSE IDE to convert all the comments in other language. I am using google translator to convert comments manually.
I'm not aware of any plugin like that. First, I would make sure the client really really wants this. Doing an automatic translate of highly technical text will not give a very good quality. I question if they will be usable at all, do a test translate of a couple of comments first to get approval.
If they really want something that is usable someone with language and technical skill would have to manually translate everything, which would take time/money.
But, if I had to do it, I would take the idea from this answer to automatically launch a web browser from Eclipse, and use the URL to google translate, https://translate.google.se/#en/es/${selected_text}, should work.
If the codebase is large and the comments can be easily found, such as a javadoc, I would write some script to automatically handle this. Google translate has an API you can use. See REST API doc
Every now and then a developer has to work on a code base that is in a language that is unknown to him. In this case, he would need to copy variable, class, method names to Google Translate to see what they actually mean.
The Source Code Translation in Eclipse tries to help such developers, by providing a popup with the translated words, when hovering the mouse over them. The plugin is able to handle compound words or phrases that use camelCase or underscore "_" as separation methods.
For the plugin to work, a properties file containing the word to be translated, and the translation needs to be created and added from the Preferences section of the plugin.
Add this URL to your Eclipse Installation to reach this solution's update site.
https://github.com/Testehan/TranslationPluginInstall/raw/master
In External Tools Configurations
Location : ${system_path:cmd.exe}
Arguments : /C start "" "https://translate.google.com/#en/fr/${selected_text}"

How do I translate a page in java with lot of text,without using resource bundle?

I have a web page with lots of text.Is there any means through which I can translate it,without using resource bundle(which involves using properties files,requiring key value pairs for all words.)?
Thanks for your precious time.
An alternative is to create separate views for each language. So a "mypage_en_US.html" for the US-english version and a "mypage_en_GB.html" for the british-english version. This gives you total control over the text and layout but has the drawback of possible code duplication if there is any logic in your view.
Wicket uses pretty clean views which should hardly contain any logic so this works pretty well there.
Just be innovative here. If you are getting shitty copy pase work. Write a program to convert the properties file and then use that properties file using google translate api, but yeah end of the day you will have to go with properties file.
I belive there would be other way too using google translate api again, would love to hear that myself too
Depends on your web framework.
For example, Wicket can apply I18N on webpages in two ways :
- using I18N files and resourcesbundles, with placeholders where required in the page
- by having totally separate pages, one for each language. The page template itself is postfixed with the locale, much like property files : HomePage_en.html, HomePage_fr.html, etc.
Other web frameworks may have similar features. If you're using raw JSP/Servlets, I'm afraid you're pretty much on your own.
But it's totally possible to implement your own templating system. For example, you could use a set of Freemarker templates, and load the one that matches the desired locale.

Is it possible to use flashvars with JBoss?

I'm part of a team developing a product using JSF 2.0 and I was asked to investigate the possibility of including FusionCharts free in the app. I have tried different ways of inserting a simple chart in a JSF page but with no luck.
On of the methods involves using the elements OBJECT and EMBED but hhen I try to use them I get a "null source" error from JBoss. From what I could find online (through Google), I am under the impression that 'flashvars' isn't quite compatible with JBoss. Is anyone here able to confirm this? If this is the case, what workaround would you suggest me?
Other ways I also found online didn't show the chart not even an error message.
Thanks in advance.
It is hard to tell what the other methods quoted were, but the preferred way of embedding flash is to use swfobject, a javascript library that does not require any special tags (nor server-side support).
It boils down to preparing a div for your flash content, giving it an id, and then calling a single function that takes the swf file url, size of the clip, flasvars and so. The javascript could easily contain EL expressions.
You might want to read this:
http://www.adobe.com/devnet/flashplayer/articles/swfobject.html
but skip to the Under the hood: dynamic publishing section, you will not be using the static publishing nor GUI.
The probable solution might be to pass the value of the flashvars as querystring of the user loading the chart swf file.
e.g.,
Column3D.swf?debugMode=1&dataURL=mydata.xml&registerWithJS=1&chartWidth=200&chartHeight=300

Categories

Resources