JSP - Date formatting and Google cache - java

I want to display a formatted date on my JSP page, so I use:
<fmt:formatDate pattern="MMM d" value="${myEvent.date}"/>
It works perfectly. It is displayed on the page as
Nov 28
However, a strange thing happens when it is cached by Google - the date on the cached page is displayed like this:
2016-11-28 20:00:00.0
Can anyone explain this? Shouldn't the formatting happen on the server? Doesn't my application server (Tomcat) send only the HTML to the page? How does Google know that this was in fact a Java date in the first place?
Just to clarify, to get to the cached version of the page, I perform a search on Google that displays my page in the results and I click on the down arrow and get to the cached version of my page.

Apparently, when a JSP is requested without the "Accept-Language" HTTP header, the JSTL formatDate tag does not format the date and the result of toString() of the Date object is returned instead.
You can verify this by using cUrl to get the page source (instead of viewing the page source in Chrome).
Bottom line: If you you are using the JSTL formatDate tag and you want your dates to be formatted in Google's cache, you should explicitly set the locale. For example:
<fmt:setLocale value="en_US" />
I hope this helps someone.

Related

get browser Locale

I have a question about the browser Locale. In pricipal, the request.getLocale retrieve the locale from the user's OS. I would like to retrieve the browser Locale independent of user's OS.
The web site has multiable languages (german, italy, english), so that user can switch language manually.
In the html page, there are meta element which show the difference.
<meta http-equiv="content-language" content="de" />
"de" will be changed to en or other language due to user's click.
The question is whether there is a way to retieve this info in java servlet.
Update:
In my LogoutServlet doPost method, I have a logout method which should retrieve the current language from html page of browser.
String locale =request.getLocale().getDisplayLanguage();
It doesn't change to Fr or IT based on the content-language.
Here is answer that could solve one part of your question:
http://www.coderanch.com/t/442889/JSP/java/Reading-META-tag-Servlet
You need to collect the meta tag's data using clientside script and send them to your servlet:
<script type="text/javascript">
function metaKeywords() {
metaCollection = document.getElementsByTagName('meta');
alert('');
for (i=0;i<metaCollection.length;i++) {
var nameAttribute = metaCollection[i].name.search(/foo/);
if (nameAttribute!= -1) {
alert(metaCollection[i].content);
}
}
}
>
</script>
As far as I'm aware, browsers don't have a locale independent of the OS.
You're giving users a way to manually choose languages (great!). To know what language they've chosen, you'll need to do something to send that information back to your server. A cookie would probably be the simplest way, since it will accompany every request. (Keep it small, though, for that same reason.)

call a java method when Click on a html button without using javascript

i work on JSP and i want to call a java method(Function) on Click on a html button without using<script></script>.how?
i try to write this code:
<button onclick="<%po.killThread();%>">
<font size="4">Kill</font>
</button>
but it doesn't work... so please help me.
thanks
You're misunderstanding how server-side programming works. When you load that page, the webserver will get to the line <button onclick="<%po.killThread();%>"> and will immediately parse and execute the JSP snippet, in your case po.killThread(), and replace everything between the <% and %> with the return value of that method, if any. And all these happens on server side, before client receives any thing. (Note that this will only happen if that page is not already been loaded and compiled into a Servlet by the server.)
Thus, the HTML that client receives, will be something like, <button onclick="some return value or nothing">, which means that nothing will happen when you press the button. If you want to execute further JSP commands on the button press you will need to make a new request to the server - for example, by redirecting the page.
This will call the function killThread when you open the website.
Try to redirect to another jsp which calls the function.
this will not run at all because after the jsp page is compiled it will return the po.killThread() value but will not call this method
You can see this by viewing the page source
JSP is a server-side technology. Did I say server-side?
In order to understand how JSP works and to clear any misconception, JavaRanch Journal (Vol. 4, No. 2): The Secret Life of JavaServer Pages is a very good read.
An excerpt from the same,
JSP is a templating technology best-suited to the delivery of dynamic text documents in a format that is white-space agnostic.
Template text within a JSP page (which is anything that is not a dynamic element), to include all white-space and line terminators, becomes part of the final document.
All dynamic elements in a JSP are interpreted on the server and once the document is sent to the client, no further dynamic interaction is possible (short of requesting the same or another document).
If you are using JSPs, then to perform some method calles, you will have to write a servlet and then call the method in doPost or doGet method of servlet.
On the other hand, if you want to make things simpler, use JSF framework which will help you achieve your objective as JSF supports event handling.

jQuery plugin vs Dojo plugin using ModelDriven in Struts 2

I am using Struts2 with ModelDriven for form submitting.
When I use datetimepicker tag of Dojo and select date from that tag the form is submitted correctly with the form values populated in the ModelDriven object.
But when datepicker tag of jQuery is taken in the form and date is selected the object in ModelDriven have null values.
If date is not selected and form is submitted then it work fine.
Is there any setting required that I missed to resolve the issue ?
I had included following jar for JQuery.
JQuery:
<sx:datepicker name="startDate" id="startDate" label="" cssStyle="width:275px; "
minDate="0" displayFormat="dd/mm/y" changeMonth="true" changeYear="true"
readonly="true" value="%{startDate}"/>
remove readonly="true" and everything will work fine.
readonly attribute default to false tells that whether the input is read-only.
I'm pretty sure that
name="startDate"
should be changed to
name="yourmodel.startDate"
Show more code for a better help...
Struts expects a value to be sent to the server as a locale-independent value, conforming to RFC3 339 (yyyy-MM-dd'T'HH:mm:ss) this is what the default dojo tag (now deprecated) provides: http://struts.apache.org/2.0.12/docs/datetimepicker.html
You can just send: 2013-01-03 the rest is optional.
The jQuery Datepicker I uses mm/dd/yy. To change this format to what struts2 expects either change the defaultFormat used for presentation or set altFormat which changes what was input into the format presented during submission (So if you like "mm/dd/yy" you can just set altFormat to "yyyy-mm-dd" and get the desired effect).
http://jqueryui.com/datepicker/#date-formats
http://api.jqueryui.com/datepicker/
As you can see jQuery easily provides for this, how to do it with the tag... I don't know. Someone else is welcome to take this information and extend it in another answer, with a tag specific technique if possible.
Problem was Dojo takes the date value on JSP as Date in java whereas JQuery takes it as string.
And so struts was finding for setDate(String) and not setDate(Date) for JQuery.

struts2 jquery from date and to date

I am working a webproject using struts2 and I used struts2 jquery plugin for datepicker.
Now I need to validate ToDate depending on FromDate.
ToDate shouldnot be greater than FromDate
validation should be like this, datepicker should disable the behind dates accodring to fromdate.Then user not able to select lower than the from date
Please let me know how can I implemt this.
Please post the code will help
Thanks in advance
That's your choice how you want to impliment the validation.There are 2 ways which as of now seems feasible to me
Client side validation using JavaScript.
Server Side validation in your action class.
For the first part your both data-picker must have id and name so once user filled ToDate and FromDate and before submitting the form you can call a java-script function where you can fetch the values of respective date-picker fields using java-script document.getEmelemntById() and can run your comparison logic.
Other option is to let Struts2 fill the ToDate and FromDate values in your action class and you can use validate method inside your Action to perform date validation.choice is all yours.
You can even play around with validation framework even.
Update
After playing around with J query plugin source code here is the workaround we have
<s:form id="form" theme="xhtml">
<sj:datepicker id="date12" name="date12" label="With Close Event" onCompleteTopics="onDpClose"/>
<sj:datepicker id="date13" name="date13" label="With Min and Max Date" minDate="0" maxDate="+2m"/>
</s:form>
<script type="text/javascript">
$.subscribe('onDpClose', function(event,data) {
$('#date13').datepicker( "option" , 'minDate',event.originalEvent.dateText );
});
</script>
In similar way can work for other way around.Hope this will work for you.
There is an example similar to what you ask for on datepicker demos site.
I've modified and tried to simplify that example and created a fiddle for it which you can find here: http://jsfiddle.net/melih/vRULq/

Jquery html() encoding error in chrome

I'm developing an Java Web Application, I used some jQuery and a REST web services that output an JSON object with a list of Javascript objects using AJAX. All is ok but when I try to fill a table created with Javascript using jQuery.html() to a valid div, all hell broke loose in Chrome, including this
Error: INVALID_STATE_ERR: DOM Exception 11
in the Javascript console.
The problem is like this, try this in chrome Javascript console:
$('#ValidadorWrapper').html("<div>AMOXICILIN 100 C&psulapsula</div>");
But if we delete the ampersand, it works
$('#ValidadorWrapper').html("<div>AMOXICILIN 100 Camp;psulapsula</div>");
This problem happens only in Chrome, I suspect it has something to do with the encoding characters but I cant' find any way of doing it. Obviously I need to input an & ( i mean the & entity) in this document.
Some steps I have tried and didn't work:
I'm using the gson library to output a String to an JSP page. My JSP page have this header <%#page pageEncoding= "UTF-8" contentType="text/html; charset=UTF-8" %> . This was my first attempt and didn't work when an ampersand appeared in my JSON object (well any special char).
My second attempt was using the HTMLEntities Java library to encode all special chars. This is the actual version and it still doesn't work
Using unicode chars like \u0026 doesnt work either
There is something more strange. Apparently if I use $('#ValidadorWrapper').html("AMOXICILIN 100 \u0026"); it works!, but this is just an example. I'm trying to fill an HTML table with my object so I really need to put that data inside html (table) tags
Try this:
$('<div></div>').appendTo('#ValidadorWrapper').text('AMOXICILIN 100 Cápsulapsula');

Categories

Resources