25 Dec 2011

JavaScript in JSP : Bad Practice

A JSP(Java Server Pages) is a server side technology whereas the JavaScript is a client side. Even after both these are work on different side , they work together to create more dynamic responsive pages on the world wide web. I had initiated a discussion ,whether  is it a bad practice to use JavaScript within JSP pages ,on the  javaranch for which i get great response. Here are some quotes of the discussion : 


Paul Clapham said
"As long as you realize that the Javascript code you put in your JSP is not going to be executed as part of the JSP, it's going to be executed only when it gets to the browser i.e. on the client side. That seems to be a concept that a lot of people aren't told about when they start learning JSP. As already pointed out, it's better to put your Javascript in a separate file and just provide a link to that file in your JSP. That way the browser can cache the Javascript instead of having to download it repeatedly as part of the JSP. There are cases, though, where you might want your JSP to generate some Javascript which varies based on input parameters or session data or whatever. "
 That seems reasonable if something is there to use  then use it ,as simple as that. But some fellow has different opinion about that.Lets see what Tim has said 


Tim Holloway said
"JavaScript is an unholy mess, but it can make pages more interactive because it programs the client instead of the server.You shouldn't depend on JavaScript to be your sole data validator. The final determination of what's valid should be in the server-side code, where it cannot be spoofed by a malicious client. Plus, people do sometimes turn JavaScript off in their browsers, in which case none of the client-side code will execute at all!"
a very important point is been noted here since JavaScript is run inside browser and browser can turn off the JavaScript so one must be aware of that and action must be taken on server side.  

Bear Bibeault said
a JSP is simply a template for an HTML page. To the JSP processor, anything that's not a JSP construct is just template text that is completely ignored by the JSP engine and sent to the client verbatim. So JavaScript is completely opaque to the JSP. Now, putting Java scriptlets into a JSP: that's not only a poor practice, but completely irresponsible at this point in time when we're coming up to the 10th, yes 10th, anniversary of JSP 2.0. Ten years is plenty of time for anyone to have come up to speed on the JSTL and EL and there is no excuse, none, nada, zero zilch, justification for still writing JSPs as if it were 2001. 



see full discussion  here

22 Dec 2011

JSTL library problem

i was creating a web application on  the Eclipse IDE using the GlassFish server.when i trying to use the JSTL library by 
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core"  %> in my jsp file.
But what i get is 
org.apache.jasper.JasperException:According to TLD or attribute directive in tag file, attribute value does not accept any expressions.


The root cause of the error is that the JSTL's uri is incorrect for the used jsp version.

Make Correct use of Library :
1. Check the servlet version and refer to it in the Deployment Descriptor(web.xml) of web application.
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">

2. Refer correct JSTL uri for the servlet or jsp.
servlet_2.3 or jsp_1.2 => JSTL_1.0 (JSTL uri =http://java.sun.com/jstl/core)
servlet_2.4 or jsp_2.0 => JSTL_1.1 (JSTL uri =)http://java.sun.com/jsp/jstl/core
servlet_2.5 or jsp_2.1 => JSTL_1.2 (JSTL uri =http://java.sun.com/jsp/jstl/core

)

1 Dec 2011

Passed OCPJP 6


Wait is over  yesterday i passed my OCPJP 6 exam. It has been long since i have been thinking about to give a shot at it. The exam tests ones basic concepts about java programming which were categorized into different objectives. The exam covers all of the specified objectives. The question were easy once one is through with the concepts.

The Bible book for the exam is SCJP 6 by Kathy Sierra and Bert Bates . The book explains a topic in most simple and effective to build a concept.It is very important to write small code to learn a topic ,tweak with & play with it , i wrote about 300 small programs on different topics .The wonderful place for a java lover is javaranch this a place where i clear my doubts and also learn new things.Two weeks before the exam i started doing mocks available on line after given exam i think question set given at end of  "The Bible" is enough to practice.

So it was a good experience as to gain global certification and getting the grasp on the very basics of java programming and of course personal satisfaction is there.