Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Redirecting URL Servlet not working

843841Nov 13 2007 — edited Nov 13 2007
Hi all,

All my JSPs are located in pages folder of my Application's Web Context. I need to redirect JSP URLs to the pages folder. For example if user requests http://localhost:8080/RedirectJSPApp/test.jsp then I need to redirect to http://localhost:8080/RedirectJSPApp/pages/test.jsp. But if user request http://localhost:8080/RedirectJSPApp/pages/test.jsp then i dont need to do any redirection.

So I define a Servlet:
package com.srh.servlet;

public class RedirectServlet 
      extends javax.servlet.http.HttpServlet 
{
   public void doGet(HttpServletRequest request, HttpServletResponse response)
         throws ServletException, IOException
   {
      doPost(request, response);
   }

   public void doPost(HttpServletRequest request, HttpServletResponse response)
         throws ServletException, IOException
   {
      String contextPath = request.getContextPath();
      String requestUrl = request.getRequestURL().toString();
      String targetRequestUrl = null;
      String jspRequested = null;		
      int index = -1;
  
      index = requestUrl.indexOf("/pages/");

      // The JSP is not called properly; 
      if (index == -1)
      {
         // Call the JSP properly 
         index = requestUrl.indexOf(contextPath);
         if (index != -1)
         {
            targetRequestUrl = requestUrl.substring(0, index+contextPath.length());
            jspRequested = requestUrl.substring(index+contextPath.length()+1);
            targetRequestUrl += "/pages/" + jspRequested; 
            response.sendRedirect(targetRequestUrl);
         }
      }
   }
}
In the web.xml I wrote:
   <servlet>
      <description>This Servlet redirects JSP URLs to appropriate lcoation w.r.t. Server</description>
      <display-name>RedirectServlet</display-name>
      <servlet-name>RedirectServlet</servlet-name>
      <servlet-class>com.srh.servlet.RedirectServlet</servlet-class>
   </servlet>
   <servlet-mapping>
      <servlet-name>RedirectServlet</servlet-name>
      <url-pattern>*.jsp</url-pattern>
   </servlet-mapping>
I have a test.jsp in pages folder:
<html>
   <head>
      <title>Test</title>
   </head>
   <body>
      Inside test.jsp
   </body>
</html>
I deployed on JBoss 4.2.0. I type http://localhost:8080/RedirectJSPApp/test.jsp in my browser and it changes to http://localhost:8080/RedirectJSPApp/pages/test.jsp which is perfect but it never prints "Inside test.jsp" on the page. Why this is happening?

If I remove RedirectServlet entries from web.xml and just type in http://localhost:8080/RedirectJSPApp/pages/test.jsp then it prints "Inside test.jsp" on the page. What I am missing here?

TIA


TV
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Dec 11 2007
Added on Nov 13 2007
1 comment
279 views