Showing posts with label JSP. Show all posts
Showing posts with label JSP. Show all posts

Tuesday, March 10, 2009

Invoking a JSP From a Cron Job

To invoke a JSP page from a cron job. Simply use wget:


wget http://mynicesite.com/mynicejsp.jsp



Wednesday, July 16, 2008

Replacing Brackets in a String (Java)

Trivial issue really but something I always forget...

sNiceString = sNiceString.replaceAll("\\(", "");

or

sNiceString = sNiceString.replaceAll("\\)", "");


You could probably use a purer regular expression too but this does the job.


Monday, July 07, 2008

Redirect and masking with mod_rewrite

I'm not sure if this will work for anyone else in the world except me, since mod_rewrite is such a tricky beast (and apparently powered by voodoo).


I managed to mask the JSP on my site which was something like:

http://www.mysiteblahblah.com/doSomethingGood.jsp?type=reallygood

to

http://www.mysiteblahblah.com/doSomethingGood/ReallyGood.html


Simple! I thought. Mod_rewrite can handle this; before I was hit with:

"You don't have permission to access /doSomethingGood/ on this server"


So I searched around the web here and there and found out that you need to 'enable' mod_proxy. So I did, by uncommenting the line in my httpd.conf:

LoadModule proxy_module modules/mod_proxy.so


I still had the same problem though and looking through my Rewrite Log (which was set to loglevel 3) everything seemed normal and the only meaningful message was "go-ahead with proxy request proxy." So all seemed fine even though it wasn't working.

After another round of searches I just tried my luck and uncommented all other modules that are related to mod_proxy. Lo' and behold, it all began to work:


LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_connect_module modules/mod_proxy_connect.so
LoadModule proxy_http_module modules/mod_proxy_http.so
#LoadModule proxy_ftp_module modules/mod_proxy_ftp.so


Now my virtualhost tag looks something like:


<VirtualHost *>

DocumentRoot "D:\website\webapps\mySiteBlahBlah"
ServerName www.mysiteblahblah.com

# SECTION FOR MOD_REWRITE
RewriteEngine On
RewriteCond %{REQUEST_METHOD} ^TRAC(E|K)
RewriteRule .* - [F]
RewriteRule /doSomethingGood/([A-Z]+).html http://www.mysiteblahblah.com/doSomethingGood.jsp?type=$1 [proxy,QSA]
RewriteLog "D:\Program Files (x86)\Apache Group\Apache2\logs\mod_rewrite.log"
RewriteLogLevel 1
# END OF SECTION FOR MOD_REWRITE

</VirtualHost>


I hope this helps at least someone out there.

Many thanks to all these sites for helping me along:

WebMasterWorld.com
FluidThoughts.com
JSW4.net
Nabble.com
ChinaUnix.net
Apache: mod_rewrite documentation
Apache: mod_proxy documentation



Sunday, January 13, 2008

Pesky URLEncoder, JSP and UTF-8

Seems like request.getParameter in JSP doesn't like dealing with UTF-8 encoded strings much. Here's what I did to solve it:


Call from Java:
sURL += "?myparam="+URLEncoder.encode(sMyUTF8String, "UTF-8");



Necessary Code in JSP:
<%@ page contentType='text/html; charset=UTF-8' %>

request.setCharacterEncoding("UTF-8");
String sMyParam = new String(request.getParameter("myparam").getBytes("ISO8859_1"),"UTF8");



Wednesday, August 16, 2006

Methods in JSP

It isn't really all that tricky but nice to know anyway:

Just use the exclamation mark to declare the method. Note that request and response objects are not available. However, that doesn't stop you from hacking around it and passing the request or response as a parameter to the method:

<%!

public String myMethod(HttpServletRequest req){

//do something really cool with the req

return "Boo!";
}


%>