0

The method response.sendRedirect() is not working in my program. it is redirecting to next Page. I also tried

response.sendRedirect("home.jsp");
return ;

But still it doesnot work

Directory image has been attached

Directory Image

protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    String userAction = request.getParameter("action");

     response.sendRedirect("Test1.jsp");  }

While in debuging the controller move on response.sendRedirect but it is not redirecting to other page.

Haider zia
  • 11
  • 4
  • response.sendRedirect(""); used to redirect to the external resources normally. there may be several cause for this. Please post your error.. what you get.. – Vikrant Kashyap Feb 22 '16 at 06:12
  • can you try using response.sendRedirect("/Test1.jsp"); – Suyash Feb 22 '16 at 06:14
  • without knowing the project directory structure you can't expect or put any answers or comments.. this may harm to your reputation.Thnk you – Vikrant Kashyap Feb 22 '16 at 06:24
  • might be in your current servlet it is uanble to find the path for "Test1.jsp"... Please try by passing complete path of the page – Pavan Kumar K Feb 22 '16 at 07:31
  • @VikrantKashyap i also posted my directory image. i would try ("/Test1.jsp"). and second it is not giving any error – Haider zia Feb 22 '16 at 07:43
  • @VikrantKashyap ("/Test1.jsp") not working. while debugging the control come to response.sendRedirect("Test1.jsp") but not redirecting to Test1.jsp – Haider zia Feb 22 '16 at 07:51
  • ok... resource not found or some this kind of message display on browser page?? or error code you may post. according to you project structure `response.sendRedirect("./Test1.jsp")` should work.. – Vikrant Kashyap Feb 22 '16 at 08:23
  • Answer depends on how servlet is invoked and what exactly happens instead. This information is completely missing in the question. – BalusC Feb 22 '16 at 08:27
  • @VikrantKashyap it is not giving any error. this redirecting is bothering me from last 3 or 4 days. (".Test1.jsp") not working. :( – Haider zia Feb 22 '16 at 08:29
  • Ok. Let me explain it. on button click, control move to loginjs.js from where it move to servlet and from servlet it need to redirect to other page. this last part of redirecting to other page in not working. and not even giving any error. – Haider zia Feb 22 '16 at 08:30
  • i did same in my last project it is working fine. sairectory sme dtructure i followed in my last project. i eve tn tried copy/pastehe codes from there but .. :( – Haider zia Feb 22 '16 at 08:34
  • `loginjs.js` is a JavaScript file? It's thus sending an ajax request to servlet? You're thus basically redirecting the ajax request itself? Is the responsible JavaScript code capable of digesting a whole HTML document output as ajax response? – BalusC Feb 22 '16 at 09:25
  • var loginBtn = document.getElementById('login_btn'); loginBtn.onclick = login; function login(){ $.ajax({ type:'GET', url:'ControllerServlet?action=home', success:function(loginsuccess){ }});} – Haider zia Feb 22 '16 at 09:35
  • @BalusC. ajax move to Controller from where it should move to mentioned jsp page. while in debugging the control come to sendRedirect statement but it doesnot redirect it to new Page – Haider zia Feb 22 '16 at 09:38
  • As I guessed. See duplicate how to make a redirect in jQuery/JavaScript. – BalusC Feb 22 '16 at 09:40
  • @BalusC i want to redirect it from servlet. coz in servlet i am checking some conditions. – Haider zia Feb 22 '16 at 09:44
  • @BalusC i am trying to slove to from last 3 to 4 days. i have done same in other project and its running fine – Haider zia Feb 22 '16 at 09:45
  • As said, your code works only if it's a NON-ajax request, or if the JavaScript code responsible for ajax request is capable of digesting a whole HTML document output as ajax request. How to send a redirect in JavaScript is answerd in the duplicate. Just rewrite your servlet in such way that it triggers that JavaScript code. E.g. by writing a JSON object instructing the JavaScript code to redirect to given URL. – BalusC Feb 22 '16 at 09:51
  • @BalusC Thank you, it worked. – Haider zia Feb 29 '16 at 05:51

0 Answers0