All Things Techie With Huge, Unstructured, Intuitive Leaps
Showing posts with label jsp. Show all posts
Showing posts with label jsp. Show all posts

Getting Google Contacts with JSP

I found this blog entry incredibly helpful in getting Google contacts using JSP.  It even sends emails through the Google SMTP.  It's a great resource:

http://javasantoshanand.blogspot.com/2013/06/how-to-get-gmail-contacts-using-jsp.html

I'd like to thank the author for putting it up.

PHP on Tomcat with JSPs




First of all, in spite of the fact that the world runs on LAMPS (Linux, Apache, MySQL, PHP, SQL), I really dislike PHP or Python.  They are sort of pseudo-Object Oriented and do not have the power of J2EE where one can integrate pure Java classes with JSPs.  My preferred approach is LAMJS where J is JSP or Java.  Our webserver is Apache Tomcat.

At any rate, we have a rang-dang-doo enterprise app written with Java, JSP, ServerFaces, and all of the good stuff.  Our business requirements say that we have to have a blog and a newsletter and a marketing side to our enterprise app.  We only have so many developers and we believe in the AGILE approach with continuous iteration and working software.  So to put the marketing side in place, we decided not to re-invent the wheel.  Wordpress has templates galore and all of the features that we need.  Wordpress is written in PHP and uses PHP, so we have to make Tomcat play with JSPs and PHP.  It was a hair-pulling experience.

The first thought was to use JavaBridge and JavaBridge.jar.  We kept getting this stack trace:



Fatal Error: Failed to start PHP ["php-cgi", "-v"], reason: java.io.IOException: Cannot run program ""php-cgi"" (in directory "C:\Users\Delon"): CreateProcess error=2, The system cannot find the file specified
Could not start FCGI server: java.io.IOException: PHP not found. Please install php-cgi. PHP test command was: [php-cgi, -v] 

php.java.bridge.http.FCGIConnectException: Could not connect to server
at php.java.bridge.http.NPChannelFactory.test(NPChannelFactory.java:64)
at php.java.bridge.http.FCGIConnectionPool.<init>(FCGIConnectionPool.java:175)
at php.java.bridge.http.FCGIConnectionPool.<init>(FCGIConnectionPool.java:189)
at php.java.servlet.ContextLoaderListener.createConnectionPool(ContextLoaderListener.java:541)
at php.java.servlet.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:185)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4791)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5285)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549)
at java.util.concurrent.FutureTask$Sync.innerRun(Unknown Source)
at java.util.concurrent.FutureTask.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: java.io.IOException: File \\.\pipe\C:\Program Files\Apache Software Foundation\Tomcat 7.0\temp\JavaBridge3030620009245189355.socket not writable
at php.java.bridge.http.FCGIConnectException.<init>(FCGIConnectException.java:37)
... 15 more
Caused by: java.io.IOException: PHP not found. Please install php-cgi. PHP test command was: [php-cgi, -v] 
at php.java.bridge.Util$Process.start(Util.java:1145)
at php.java.servlet.fastcgi.FCGIProcess.start(FCGIProcess.java:68)
at php.java.bridge.http.NPChannelFactory.doBind(NPChannelFactory.java:94)
at php.java.bridge.http.FCGIConnectionFactory.runFcgi(FCGIConnectionFactory.java:88)
at php.java.bridge.http.FCGIConnectionFactory$1.run(FCGIConnectionFactory.java:109)


Obviously we hadn't configured Tomcat to find the PHP executables which were supposed to be in the .war file.  We tried various things like trying to install PHP on Windows (our dev machines are Windows, but our production and test servers are Linux), all to no avail.  We found plenty of instructions on how to show Apache how to find the PHP executables, but none for Tomcat.

I was really looking for a plug and play solution, and I found it with Caucho and their Quercus war file.  I simply downloaded the war file, and took a copy of it.  I changed the copy's .war extension to a .zip extension and unzipped it.  I took the resultant folder and dropped it into our webapps folder where Tomcat could find it and it worked first time.  Someone ought to preserve their blood for posterity.  You too can make PHP play with Tomcat by downloading the war file from here:  http://quercus.caucho.com/

 Thank you Caucho and Quercus where ever you are.

Best Open Source, Free Blogging Software




We are a java shop.  We are committed to J2EE, Java, JSP as the best web delivery system for what we do.  Recently I was asked to implement blogging software.  Without a doubt, the best open source JSP blogging software is Apache Roller





Apache Roller is a full-featured, multi-user and group-blog server suitable for blog sites large and small. It runs as a Java web application that should be able to run on any Java EE server and relational database. Currently, Roller is best supported on Tomcat and MySQL -- but users have reported success running Roller on Glassfish, Websphere, JBoss, Resin, Geronimo, Derby, PostgreSQL, and Oracle.
Here are some of Roller's key features:
  • Multi-user blogging: can support tens of thousands of users and blogs
  • Group blogging with three permisson levels (editor, author and limited)
  • Support for comment moderation and comment spam prevention measures
  • Bloggers have complete control over blog layout/style via Apache Velocity-driven templates
  • Built-in search engine indexes weblog entry content
  • Pluggable cache and rendering system
  • Support for blog clients that support MetaWeblog API
  • All blogs have entry and comment feeds in both RSS 2.0 and Atom 1.0 formats

You can get the software here:

Javascript -- Round Number to nearest 100 -- Validate

I have this web page that is created by a jsp.  The user has to input an amount of money to make a bid, but I don't want the users entering silly amounts like $1098.66.  I want the amount to be in exact increments of $100.  So I have an input tag "<input tabindex='1' type='text' name='amt' id='amt' value='00.00'  size='10' />" and I want to validate and make sure that the number is an increment of $100.  When the user presses the submit button, I call onclick="javascript:validateAndSubmit()".

My validate and submit function is something like this:


var bestPrice = document.(insert Form Name).amt.value;
                //get rid of the dollar sign
var match= bestPrice.match(/[0-9,.]*/);
if (match!==null) {
                    //Make the number into a float
   var amount= parseFloat( match[0].replace(/,/g, '') ); 
                 //find out if the number is not in multiples of 100 using modulus
   var rem = amount % 100;
   if ( rem > 0)
    {
    alert("Your offer must be in multiples of $100.")
    }

Hope that this helps.

JSP - How to Programatically Set the Select of a Combo Box

I had to look this one up, so I am posting this JSP tip in case anyone else needs help. Here is the scenario. I have a form with a select tag and a bunch of options. The data is stored in a data base. When I read the data back and display the page, I want to set the select of the combo box to display the data value of the variable or the database value. It is very easy with a scriptlet. Just add the following attribute code to your option tag:

option value = "Chocolate Brownies" <% if (icecream.equalsIgnoreCase("Chocolate Brownies") out.print("selected"); %>

There you have it. Easy peasy!

JSP ~ Login before going to bookmark or URL

So you have a web app or website in JSP where you want the user to login before proceeding to a bookmarked page or a URL. And you want to proceed to that page or URL automatically after login. How do you do it?

For me, every protected JSP page has an include page:

<%@ include file="isLoggedIn.jsp"%>

That file, isLoggedIn.jsp is called on every page. In that file, the first thing that I do, is I get the request URL. I do it like this:

String redirectUrl = request.getRequestURL().toString();

Then is there is a query string afterwards (www.mysite.com?showMe=cars -- the query string is showMe=cars), you need to get the query string in a separate step:

String queryString = request.getQueryString();

Since the queryString doesn't have the question mark, you need to re-assemble the called URL:

redirectUrl = redirectUrl +"?"+queryString;

Now I usually have a session attribute called "AccessGranted" or "isLoggedIn". I check for that:

String accessGranted = (String) session.getAttribute("AccessGranted");

if (accessGranted.equalsIgnoreCase("false"))
response.sendRedirect("login.jsp?NotLoggedIn=True&requrl="+redirectUrl);


What you see above, is that I send the request URL to the login page.

On the login page, if the login is successful, I redirect to a menu page. Otherwise, I redirect to the URL that was called in the first place. I do it this way:

String redirectURL = (String) request.getParameter("requrl");
if (redirectURL == null)
redirectURL = "menuPage.jsp";

If the login fails, then:

redirectURL="login.jsp";

Finally I send the user to where they have to go with the jsp redirect directive:

response.sendRedirect(redirectURL);


Easy as pie.


The Meta Refresh Tag with JSF, JSP, XHTML and Icefaces Apparently Doesn't Work

I have an application that not only has the Icefaces Ajax push, but certain elements require a client side refresh. I had a heck of a time trying to make the meta refresh tag work.

Tried a whole pile of stuff, then I realized that the tag should be in template and not on the xhtml page. Worked like a charm.

I thought that I would put this up for others who struggle with this and Google is of little help. So if your meta refresh tag doesn't work, put it in the template.

Escape The Ampersand

I just spent several hours pulling out my hair trying to escape the ampersand character in an .aspx URL with parameters. The URL was a third party URL which needed to be encoded. I was working in JSP, JavaScript, XHTML, JSF, and JSTL.

It went something like this:

www.mydomain.com/login.aspx?name=Username&Password=password&accountNo=AcctNo

I constructed the string and tried to escape it with a "\". Of course, I quickly realized that it was illegal. There are only a few things that work with that escape character.

Then I tried the '& amp;' thing. That didn't work.

Then I tried the '% 26' thing. That didn't work.

I imported the URLEncoder and fed the string into that. It didn't work.

I tried using the URI constructor with the 5 input elements. I have more than 5 inputs and it kept throwing a path exception. Nothing seemed to work.

I tried most things and they didn't work.

What finally worked was the \u0026 thing.

In the JSP the URL looks like this:

String url = "www.mydomain.com/login.aspx?name=Username\u0026Password=password\u0026accountNo=AcctNo";

Hope this helps somebody.