0

I have a question regarding a doPost method. I have made doPost method that executes a query on a db.

code:

try{


         Statement st1 = con.createStatement();

        PreparedStatement ps1=con.prepareStatement("INSERT into nota (Id_utilizator,Nume_reteta,Nota) values ((SELECT Id_user from user where E_mail = '"+request.getSession().getAttribute("email")+"'),(SELECT Nume FROM reteta where Nume = '"+retetaidd+"'),('"+nota+"'))");
         ps1.executeUpdate();

        response.sendRedirect(request.getRequestURI());   

        st1.close();
        con.close();
        ps1.close();

    }
    catch (Exception e2)
    {
      e2.printStackTrace();
    } 

Now I want that when my query is executed the page don't redirect to another page, I want to stay on the same page and the query to be executed. I know that I should replace

request.getRequestDispatcher("/health.jsp").forward(request, response);

but I don't know with what.Could anyone help me?Thanks

Bogdan
  • 155
  • 2
  • 3
  • 10

3 Answers3

0

You can use HttpServletrequest.getRequestURI for redirecting same page

httpResp.sendRedirect(httpReq.getRequestURI());

UPDATE1:

Then set your necessary data in your request scope and use forward() method of your RequestDispatcher for controlling your desired JSP.

request.setAttribute("results", results);
request.getRequestDispatcher("/WEB-INF/page.jsp").forward(request, response);

For full demonstration, go to Servlet Wiki

Credit goes to @BalusC

Community
  • 1
  • 1
SkyWalker
  • 28,384
  • 14
  • 74
  • 132
0

If you want to execute the query without page reload then you can use AJAX. Otherwise you can use RequestDispatcher like this way

request.setAttribute("AttributeName", "AttributeValue");
request.getRequestDispatcher("/health.jsp").forward(request, response);

Here health.jsp is the same page from where request comes and to where request goes.

  • how can I integrate ajax into my code to do what i want?I don't know so much about ajax.Thanks – Bogdan Jun 19 '16 at 07:12
  • @Bogdan Have a look at the answer for ajax call http://stackoverflow.com/questions/4112686/how-to-use-servlets-and-ajax?rq=1 –  Jun 19 '16 at 08:49
  • I managed to do something,but now I have another probelm:( please look at : http://stackoverflow.com/questions/37905594/ajax-submit-for-all-forms – Bogdan Jun 19 '16 at 09:08
  • I have answered the question. IF it works then please accept the answers for both the question :) –  Jun 20 '16 at 10:35
0

What you can do is just put code below replacing response.sendRedirect(request.getRequestURI());

String previousURL = request.getHeader("referer");
response.sendRedirect(previousURL);

This might solve your problem

Shalin Patel
  • 1,079
  • 1
  • 10
  • 15