Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Trapping 404 URL

 
Randy James
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello All,

I am working on a java web application and have the need to preserve a
URL that throws a 404 error. I have the following entered into my
web.xml:

<error-page>
<error-code>404</error-code>
<location>/servlet/RequestParameterAction</location>
</error-page>

This works fine. In the servlet I forward on to an appropriate page
after some processing. What I am trying to do now is capture the
incorrect URL to display or manipulate. However, when I get the request URI/URL all I get is
the servlet address, for example:

/mycontext/servlet/RequestParameterAction

Is there a way to programmatically trap the URL that threw the 404
error? I'd prefer to not have to go and tweak the webserver settings.
I'm running this application under JRun 3.1, although I've tested it
under 4.0 and get the same results. 3.1 uses the 2.2 Servlet spec, which may be limiting here.

Any ideas? Help is much appreciated!

R
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmmm...

Using Tomcat 5.0.28 I'm getting the original address when I use getRequestURL. Using getRequestURI gives me the url of the error page.

IOW, this works for me:


Furthermore, the J2EE javadocs specifically say:
"the URL the client used to make the request."

The returned URL contains a protocol, server name, port number, and server path, but it does not include query string parameters.

http://java.sun.com/j2ee/sdk_1.3/techdocs/api/javax/servlet/http/HttpServletRequest.html


What container are you using?
[ December 29, 2004: Message edited by: Ben Souther ]
 
Randy James
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm using JRun 3.1. Unfortunately, this uses the Servlet 2.2 spec. So, this is how I am trying to get the request URL in my Servlet:

StringBuffer theRequestURL = javax.servlet.http.HttpUtils.getRequestURL(req);

At that point I shove the URL into a request parameter for display on the forwarded to page:

if(theRequestURL != null) {
req.setAttribute("REQUEST_URL",theRequestURL.toString());
}


But, on the page that I forward to "<%=request.getAttribute("REQUEST_URL")%>" renders the Servlet address, for example: http://mycontext/servlet/RequestParameterAction

Could this be a limitation of the 2.2 specification?

R
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
javax.servlet.http.HttpUtils.getRequestURL(req)


Have you tried calling request.getRequestURL?

What happens if you put:
<%= request.getRequestURL() + "/" + request.getQueryString() %>
in your error page?
[ December 29, 2004: Message edited by: Ben Souther ]
 
Randy James
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The HttpServletRequest method getRequestURL() was introduced in the servlet 2.3 API I believe. That's why I had to use the (now deprecated) HttpUtils method. Since I'm on an older version of JRun, I am stuck with 2.2 for the time being.

R
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, I didn't know that.
Sorry I couldn't help.

Make sure to post the solution if you find one though.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!