• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Devaka Cooray
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Tim Holloway
  • Claude Moore
  • Stephan van Hulst
Bartenders:
  • Winston Gutkowski
  • Carey Brown
  • Frits Walraven

Problem with redirecting from servlet to servlet  RSS feed

 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
    Hello everybody!

    I trying to create code, which must perform redirecting from one servlet to other servlet.
    This is invoking servlet



 And this is target servlet



  web.xml



  But "requestDispatcher("/newsPage").forward(request, response)"  does not works properly. As well as "response.sendRedirect("/newsPage")".
  It discards me on "/loginPage.jsp"...

  The project structure in attachment.
   


   
Project-structure.png
[Thumbnail for Project-structure.png]
Project structure
 
Saloon Keeper
Posts: 1112
36
IBM DB2 Java Netbeans IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the ranch! I think you have a typo in your code, instead of newsPage you should have newsPage.jsp. Moreover at line 32 the call to doGet method is useless.
 
Sheriff
Posts: 21649
101
Chrome Eclipse IDE Java Spring Ubuntu VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Claude, I think you missed the @WebServlet annotation. While there indeed is a newsPage.jsp file, there is also a servlet mapped to /newsPage.
 
Claude Moore
Saloon Keeper
Posts: 1112
36
IBM DB2 Java Netbeans IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob, I 've completely missed it ! Sorry.
At this point I would ask OP to add some logging in doGet() / doPost() method in PushNewsServlet classes; at least he should be able to verify that the secondary servlet is invoked.
By the way, in case of successful login, what landing page he would expect to hit ? PushNewsServlet doesn't redirect to any JSP page...
 
Andrey Dmitriev
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
   Claude Moore, thank you for reply!

   But it is not a typo. I attempt to pass on "/newsPage" (not "/newsPage.jsp") intentionally.
   The thing is JSP-page ("/newsPage.jsp") contains useBean, which takes a data from base, through "doGet" in Servlet-code and HQL in hibernate-code.
   Thats why primarily "/newsPage" is invoked.
   And therefore redirecting must be fulfilled directly on "/newsPage".
 
Claude Moore
Saloon Keeper
Posts: 1112
36
IBM DB2 Java Netbeans IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, does PushNewsServlet redirects at the end of its doPost / doGet methods to newsPage.jsp ?
Moreover, can you debug your code and be sure that your request is actually forwarded to PushNewsServlet ?
 
Andrey Dmitriev
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Claude Moore wrote:Ok, does PushNewsServlet redirects at the end of its doPost / doGet methods to newsPage.jsp ?
Moreover, can you debug your code and be sure that your request is actually forwarded to PushNewsServlet ?



  To make situation more clearly, I provide here the full code of second servlet


   As visible, doGet redirects on JSP-page after starting. And its do it properly.

   Maybe, the problem is that the first servlet ("\loginPage") invokes "requesDispatch.forvard" in "doPost"-method?
   Indeed, second servlet invokes doGet at first.

   To thread this supposed problem, i tried the following:



     But this variant has not been successful...  
 
Claude Moore
Saloon Keeper
Posts: 1112
36
IBM DB2 Java Netbeans IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
At a first glance I cannot see anything wrong with your code. The problem is that you are redirected to loginPage.jsp, that it's also your default welcome page, right ? My guess is that your application server (what appserver are you using, by the way ? ) somehow is not able to redirect to newsPage.jsp and you end with landing on welcome page instead... it's only a guess. I would try to define another welcome page and to redirect, in newsPage servlet, to another pure html page (for example a page with only a  text message like 'Done').
This way, I would check that no hidden redirections happen.
 
Andrey Dmitriev
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Claude Moore wrote:At a first glance I cannot see anything wrong with your code. The problem is that you are redirected to loginPage.jsp, that it's also your default welcome page, right ? My guess is that your application server (what appserver are you using, by the way ? ) somehow is not able to redirect to newsPage.jsp and you end with landing on welcome page instead... it's only a guess. I would try to define another welcome page and to redirect, in newsPage servlet, to another pure html page (for example a page with only a  text message like 'Done').
This way, I would check that no hidden redirections happen.



  I use Tomcat, deployed on Maven.
  When I attempt to redirect on JSP-page ("newsPage.jsp"), application do this without problems. The only thing is that in the browser URL "http://localhost:8080/loginPage" become visible, when successfully redirection on "newsPage.jsp" is fullfiied....

 
 
Claude Moore
Saloon Keeper
Posts: 1112
36
IBM DB2 Java Netbeans IDE Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But the content of newsPage.jsp is displayed or not? If I remember well, forward doesn't change the URL you see in your browser. To redirect the browser to another page you have to use sendRedirect method, not forward.
 
Andrey Dmitriev
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Claude Moore wrote:But the content of newsPage.jsp is displayed or not? If I remember well, forward doesn't change the URL you see in your browser. To redirect the browser to another page you have to use sendRedirect method, not forward.



 Yes, the content of newsPage.jsp is displayed successfully.
 
Andrey Dmitriev
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
   For testing cases I created new servlet-class (@WebServlet("/TestServlet"))with only doPost-method, which gets some data from new jsp-page. Without connection to any database.
   In doPost of AuthorServlet I invoked "returnPage = "/TestServlet";
               request.getRequestDispatcher(returnPage).forward(request, response);"
   But result is the same: I remained in welcome jsp ("/loginPage").

   Maybe, the problem is focused in server-settings, indeed (Tomcat, deployed on Maven).
 
Claude Moore
Saloon Keeper
Posts: 1112
36
IBM DB2 Java Netbeans IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you tried with send redirect()?
 
Andrey Dmitriev
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
   sendRedirection is proved successful!  

   And its works properly not only in relation of test-class, but in relation of "/newsPage"  too.
   getRequestDispatcher does not work similar way.

    I feel partial satisfaction: from one side, the problem is solved, but from other side I until end don't understand what way it was solved.

    Further is a working code







     
 
 
I'm still in control here. LOOK at this tiny ad!
Become a Java guru with IntelliJ IDEA
https://www.jetbrains.com/idea/
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!