aspose file tools*
The moose likes Servlets and the fly likes Problem getting Servlets to work correctly with Tomcat Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Problem getting Servlets to work correctly with Tomcat" Watch "Problem getting Servlets to work correctly with Tomcat" New topic
Author

Problem getting Servlets to work correctly with Tomcat

Randall Stevens
Ranch Hand

Joined: Jul 01, 2003
Posts: 65
I have two servlets that run on a Tomcat server in a RedHat Linux environment. I can access the servlets, but when they try to redirect to various local HTML pages I receive a file not found error. To call the servlet on my local host, I type http://localhost:8080/servlet/myServlet.
Then based on the remoteAddress(), I should have been directed to http://localhost:8080/127.0.0.1/mypage.html, instead it tries to go to http://localhost:8080/servlet/127.0.0.1/mypage.html and I receive an error file not found.
I have asked about this several times in the Tomcat group, and several times the question has gone unanswered. I need to fix this problem as it is holding up my application deployment.
Randall
Giselle Dazzi
Ranch Hand

Joined: Apr 20, 2003
Posts: 168
I must be missing something big time but I�ll ask anyway, you are trying to redirect to
localhost:8080/localhost/whatever.html ...... this is very weird... that�s probably why it�s not finding the file.


Giselle Dazzi<br />SCJP 1.4
Tim Baker
Ranch Hand

Joined: Oct 04, 2003
Posts: 541
lets see the code that generates the string that you are redirecting to please. it looks like that is where the error is?


Kim Jong II (North Korea's Dear Leader) said:Nuclear weapons don't kill people, people kill people.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61774
    
  67

The URL that you redirect to is relative to the current URL and not to the filesystem. If you are using a URL such as "xyz.html", it will assume that it is in the same 'folder' as the current URL, e.g. in this case 'servlet' which isn't a folder at all.
When using servelts you need to be fairly precise about your URLs, making them server- or context-relative as appropriate.
bear


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Randall Stevens
Ranch Hand

Joined: Jul 01, 2003
Posts: 65
I'll try to answer both people at the same time.
First, the need to redirect is based on each system has a group of web pages that the user's are supposed to access. So per the requirement, a machine that has an IP of 183.23.109.8 would have a directory structure under jakarta-tomcat-4.0.4/webapps/ROOT called /183.23.109.8 where the index.html for that machines specific access pages are located. This is a requirement and cannot be changed.
The test code is:
String url = "/127.0.0.1/index.html";
request.sendRedirect(url);
The servlet should go to jakarta-tomcat-4.0.4/webapps/ROOT/127.0.0.1/index.html and the url that should display in the browser should be http://localhost:8080/127.0.0.1/index.html but instead it is showing http://localhost:8080/servlet/127.0.0.1/index.html
Any suggestions?
Randall Stevens
Ranch Hand

Joined: Jul 01, 2003
Posts: 65
I appear to be unable to set the context, the code works fine in Weblogic but does not work in Tomcat. As the end-user has a requirement of Tomcat 4.0.4, I have to try to get it to work in that environment.
Marty Hall
Author
Ranch Hand

Joined: Jan 02, 2003
Posts: 111
I have two servlets that run on a Tomcat server in a RedHat Linux environment. I can access the servlets, but when they try to redirect to various local HTML pages I receive a file not found error. To call the servlet on my local host, I type http://localhost:8080/servlet/myServlet.
Then based on the remoteAddress(), I should have been directed to http://localhost:8080/127.0.0.1/mypage.html, instead it tries to go to http://localhost:8080/servlet/127.0.0.1/mypage.html and I receive an error file not found.

Well, I am a little bit baffled by why you are trying to redirect to an HTML page that has "localhost" as part of the address part of the URL. But, putting that aside, your question is of a common form:

In most questions of this form, the problem lies in using relative URLs. For example, suppose a servlet does this:

What URL will the browser try to use for the image? Well, if your servlet uses the default URL from the invoker servlet (ie http://host/webAppPrefix/servlet/ServletName), then the browser will think that the image is in the nonexistent "servlet" directory.
There are a couple of solutions:
  • Stop using the invoker servlet -- it is really only a development convenience and you should not use it for deployed servlets.
  • Use a URL that starts with a slash (eg /webAppPrefix/mom.gif)
  • Use a URL that starts with ".." (eg ../mom.gif)


  • Anyhow, I do not know what went wrong or even what you were trying to do because you did not include the code. But, my guess is that you did response.sendRedirect with a simple relative URL that did not start with a slash, so your problem is a specific case of the more general problem cited above. If this is not the problem, please be more specific about what you are trying to do and the code that you used to try to do it.
    Cheers-
    - Marty


    Java training and consulting
    Randall Stevens
    Ranch Hand

    Joined: Jul 01, 2003
    Posts: 65
    Marty,
    The use of localhost was used as an example, as was the use of the various IP addresses in my responses.
    In one of my latter responses, I did show a sample of test code which I used to test with and it did include the slash.
    I have tried the following:
    "../127.0.0.1/index.html"
    "./127.0.0.1/index.html"
    "/127.0.0.1/index.html"
    "127.0.0.1/index.html"
    All result in the file not being found in the Tomcat environment.
    Marty Hall
    Author
    Ranch Hand

    Joined: Jan 02, 2003
    Posts: 111

    The use of localhost was used as an example, as was the use of the various IP addresses in my responses.
    In one of my latter responses, I did show a sample of test code which I used to test with and it did include the slash.
    I have tried the following:
    "../127.0.0.1/index.html"
    "./127.0.0.1/index.html"
    "/127.0.0.1/index.html"
    "127.0.0.1/index.html"
    All result in the file not being found in the Tomcat environment.

    Could you post the exact response.sendRedirect call that you use, the exact real location of your index.html file, and the exact URL of the servlet that does the redirection?
    Cheers-
    - Marty
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Problem getting Servlets to work correctly with Tomcat