GoogleSearchBox

Custom Search

Wednesday, August 28, 2013

format java date from string MM/dd/yyyy HH:mm:ss a

String datetimeString = "6/7/2013 2:55:44 PM";  // A String representing your date
java.util.Date result = new Date();
DateFormat formatter = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss a");

// Below Will Throw Exception in thread "main" java.text.ParseException: Unparseable date:
// SimpleDateFormat formatter = new SimpleDateFormat("MM-dd-yyyy"); 

result = formatter.parse(datetimeString); // Throws ParseException
System.out.println("Parse Java Util Date  = "+result);
System.out.println("formatted: " + formatter.format(result));


Output is:
Parse Java Util Date  = Fri Jun 07 14:55:44 GMT+05:30 2013

formatted: 06/07/2013 02:55:44 PM



Refer below posts for more examples:
http://viralpatel.net/blogs/check-string-is-valid-date-java/
http://docs.oracle.com/javase/1.5.0/docs/api/java/text/SimpleDateFormat.html

Tuesday, August 20, 2013

How to pass multiple property (option) values as an argument to a method in JQuery

For an jQuery API we have multiples of options to use from for a method.
For example, for the jQuery UI Dialog widget we have a lot of options to choose from like we have maxHeight, minWidth, modal, resizable, draggable to achieve different functionalyties with the widget.

So how to use all or some of these options with the dialog method ?

Here is how we can do it:
We can create a variable containing all the options (name:value) with comma(,) in between multiple values (basically json notation) and then pass the variable to the dialog() method.

  $(function(){
 var dialogOpts = {
  show: true,
  hide: true,
  maxHeight: 400,
  minWidth: 850
 };
 $("#dialog").dialog(dialogOpts);
  });

where "dialog" is the div name inside your html's body as follows :
<div id="dialog" title="Basic dialog">

  <p>This is the default dialog which is useful for displaying information. The dialog window can be moved, resized and closed with the 'x' icon.</p>

  <p>This is the default dialog which is useful for displaying information. The dialog window can be moved, resized and closed with the 'x' icon.</p>

  <p>This is the default dialog which is useful for displaying information. The dialog window can be moved, resized and closed with the 'x' icon.</p>

  <p>This is the default dialog which is useful for displaying information. The dialog window can be moved, resized and closed with the 'x' icon.</p>

  <p>This is the default dialog which is useful for displaying information. The dialog window can be moved, resized and closed with the 'x' icon.</p>

  <p>This is the default dialog which is useful for displaying information. The dialog window can be moved, resized and closed with the 'x' icon.</p>

  <p>This is the default dialog which is useful for displaying information. The dialog window can be moved, resized and closed with the 'x' icon.</p>

</div>




Tags:
jquery option chain
jquery ui option chaining
jquery method option chaining
How to use multiple options to pass to a method in JQuery
How to use multiple options using options for jQuery method
How to use multiple options to pass to a method in JQuery

What is the dollar Sign ($) means in JQuery Or how to avoid $ collision from jquery with other languages or technologies

$ (dollar) sign in Jquery is just a method name or identifier name.

As javascript allows the $ sign as a special character including some other special characters to be used as a valid variable name (identifier) or a valid method name.

To make it simple, jQuery and some other languages (scripting or coding) has choosen this $ to use their api (methods) easily.

In jQuery, we can replace the $ sign with the word "jQuery" whenever we encounter $ collision issues with some other coding languages like JSTL (JSP, EL) or prototype etc. and it will do the trick.
Infact we can say, $ is a shortcut symbol for the "jQuery" word.

So in jQuery below code :
<script>
  $(function() {
    $( "#dialog" ).dialog();
  });
</script>

is Same as below code:
<script>
  jQuery(function() {
    jQuery( "#dialog" ).dialog();
  });
</script>



Tags:
What is the dollar Sign ($) means in JQuery ?
How to avoid $ collision from jquery with other languages or technologies ?

Friday, July 12, 2013

How to know whether a WebService WSDL file is of SOAP version 1.1 or 1.2 compliant

If your WSDL file has a namespace named "soap12" inside the <wsdl:definitions   tag, as shown below:

<wsdl:definitions xmlns:s="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/"



Then the wsdl is SOAP 1.2 version compliant /compatible.


References:
http://schemas.xmlsoap.org/wsdl/soap12/soap12WSDL.htm

validate a webservice wsdl file online

Below are some sites to validate the WSDL file hosted some where in the internet.

1. http://www.validwsdl.com/
2. http://xmethods.net/ve2/Tools.po



Thursday, July 4, 2013

Content is not allowed in prolog. org.xml.sax.SAXParseException: Content is not allowed in prolog.

SEVERE: Parse Fatal Error at line 2 column 1: Content is not allowed in prolog.

org.xml.sax.SAXParseException: Content is not allowed in prolog.

        at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source)

        at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.fatalError(Unknown Source)

        at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(Unknown Source)

        at com.sun.org.apache.xerces.internal.impl.XMLScanner.reportFatalError(Unknown Source)

        at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$PrologDriver.next(Unknown Source)

        at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(Unknown Source)

        at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)

        at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)

        at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)

        at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(Unknown Source)

        at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(Unknown Source)

        at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown Source)

        at org.apache.tomcat.util.digester.Digester.parse(Digester.java:1642)

        at org.apache.catalina.startup.Catalina.load(Catalina.java:524)

        at org.apache.catalina.startup.Catalina.load(Catalina.java:562)

        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)

        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)

        at java.lang.reflect.Method.invoke(Unknown Source)

        at org.apache.catalina.startup.Bootstrap.load(Bootstrap.java:261)

        at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:413)

Jul 4, 2013 3:43:44 PM org.apache.catalina.startup.Catalina load

WARNING: Catalina.start using conf/server.xml:

org.xml.sax.SAXParseException: Content is not allowed in prolog.

        at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(Unknown Source)


We get this error, due to some characters in the xml file (in my case it was server.xml file) are not liked by the Parser. Even when you open the same file in IE browser, it will not open up. Also IDE like MyEclipse also shows same error "Content is not allowed in prolog."

I could not find out, which character or line are not liked by the parser.
So the solution for me : I took another same xml file (which was working) and replaced it and made changes as per my requirement.

That's it !!

Wednesday, July 3, 2013

java.lang.NullPointerException at org.apache.jsp.index_jsp._jspInit(index_jsp.java:22) and javax.servlet.ServletException: java.lang.LinkageError

SEVERE: Servlet.service() for servlet jsp threw exception
java.lang.NullPointerException at org.apache.jsp.index_jsp._jspInit(index_jsp.java:22) at org.apache.jasper.runtime.HttpJspBase.init(HttpJspBase.java:52)
 at org.apache.jasper.servlet.JspServletWrapper.getServlet(JspServletWrapper.java:164) 
 at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:340)
 at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
 at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
 at javax.servlet.http.HttpServlet.service(HttpServlet.java:723)
 at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
 at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
 at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
 at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
 at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
 at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
 at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
 at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
 at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:861)
 at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:606)
 at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
 at java.lang.Thread.run(Thread.java:619)
Jul 3, 2013 5:27:29 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet jsp threw exception


I googled it, found some suggestions like,
we have jsp-api.jar and servlet-api.jar placed inside the project’s WEB-INF/lib directory and That’s causing the issue. Because it should not be there as Tomcat has it already these jars in its lib.
But removing that jar from the projects   gives me this below error:


















And Sometimes, when you delete some conflicting jars (but still some conflicts remains there), then you may get below exception :

javax.servlet.ServletException: java.lang.LinkageError: loader constraint violation: when resolving interface method "javax.servlet.jsp.JspApplicationContext.getExpressionFactory()Ljavax/el/ExpressionFactory;

" the class loader (instance of org/apache/jasper/servlet/JasperLoader) of the current class, org/apache/jsp/index_jsp, and the class loader (instance of org/apache/catalina/loader/StandardClassLoader) for resolved class, javax/servlet/jsp/JspApplicationContext,
have different Class objects for the type javax/el/ExpressionFactory used in the signature
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:343)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)

root cause

 java.lang.LinkageError: loader constraint violation: when resolving interface method "javax.servlet.jsp.JspApplicationContext.getExpressionFactory()Ljavax/el/ExpressionFactory;" the class loader (instance of org/apache/jasper/servlet/JasperLoader) of the current class, org/apache/jsp/index_jsp, and the class loader (instance of org/apache/catalina/loader/StandardClassLoader) for resolved class, javax/servlet/jsp/JspApplicationContext, have different Class objects for the type javax/el/ExpressionFactory used in the signature


Now the root cause of this issue is jar files conflict.
Basically identical jar's located inside server's (like Tomcat's) lib directory conflict with same jars found in your deployed application's WEB-INF/lib/ directory.
Below jars in my case above were causing the issue.
1. servlet-api.jar
2. jsp-api.jar
3. el-api.jar

I had these in the BuildPath of my MyEclipse, because of class files had dependency on these.. so could not delete from there. But when I deploy the application, these jars get copied into the TOMCAT_HOME/webapps/myproject/WEB-INF/lib/

And by default my TOMCAT_HOME/lib/  directory had these jars.

Now, I can not delete the servlet-api and jsp-api jars from my MyEclipse build path, otherwise my code will have so many compile errors due to classNotFound.

So the solution I used is:
1. I deleted  servlet-api and jsp-api jars from my MyEclipse build path, and in place of these, I used javaee.jar (which had all the required class files for my project to compile).
2. Also completely deleted the el-api.jar  file too from the build path

Hope that helps for you too!!

References:
http://stackoverflow.com/questions/8487048/java-lang-linkageerror-javax-servlet-jsp-jspapplicationcontext-getexpressionfac

spring / JSTL $ variable does not display the value Or Spring $ variable not carrying into view / JSP

The expression language (EL, $ syntax) like ${message} doesn't work in my JSP / JSTL tags while working with Spring!
 I noticed this issue while working with Spring demo.

Lets discuss first what was my settings and what I tried to make it work, then will take about the solution.
I was setting the variable in the spring Controller like below:

 @RequestMapping("/hello")
 public ModelAndView helloWorld() {
  String message = "HELLO SPRING MVC";
  return new ModelAndView("abcd", "msg", message);
 }
 
        /* Even I tried below way of setting the variable msg */
 @RequestMapping("/welcome")
 public String welcomeWorld(Model m) {
  String s = "HELLO SPRING MVC";
  m.addAttribute("msg", s);
  return "welcome";
 }

And I was trying to use the "msg" variable  in the JSP (abcd.jsp) as below:

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="s" uri="http://www.springframework.org/tags" %>

<html>
<body>
Message is: <%= request.getAttribute("msg") %> <br/>
Message is: <c:out value="${msg}" /> <br/>
Message is: ${"msg"} <br/>
Message is: ${msg} <br/>
</body>
</html>


Tried every possible combination of displaying the variable in the jsp.
 But I was not getting any value corresponding to the variable when used along with the EL $, I used to get below response through JSP page when run under tomcat 5.5.x:












So, after googling out, I found that, its the Servlet and JSP versions not compatible with the server.
Basically in my web.xml I had declared the version of Servlet I am using is 2.5 as below:

<?xml version="1.0" encoding="UTF-8"?>
<web-app
    xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    version="2.5">

   <servlet>
  <servlet-name>spring</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <load-on-startup>1</load-on-startup>
 </servlet>

 <servlet-mapping>
  <servlet-name>spring</servlet-name>
  <url-pattern>*.html</url-pattern>
 </servlet-mapping>
</web-app>


But, I was deploying above application on a Tomcat server of version 5.5.x (which supports Servlets version 2.4). And that was the main root cause.

So, When I deployed the application to Tomcat 6.x (supporting Servlets 2.5 and a higher version of JSP and EL), It worked fine.

Now I am getting the expected result of the above JSP (abcd.jsp) page, the output is shown below:











Hope this helps you too!!

References I followed:
http://stackoverflow.com/tags/jstl/info