问题
I am making an insert into a database ,and after the operation is done,I want to stay on the same page and the results after inserting to be present.
Now after the inserting when I am trying to sent to the same page I got as result the page without any data.
Here is my java code:
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try{
DBConnection connection = new DBConnection();
Connection con = connection.Connect();
PreparedStatement ps=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 = '"+retetaid+"'),('"+nota+"'))");
ps.executeUpdate();
String destination = "/Retete";
RequestDispatcher rd = request.getRequestDispatcher(destination);
rd.forward(request, response);
con.close();
ps.close();
}
catch (Exception e2)
{
e2.printStackTrace();
}
finally
{
out.close();
}
}
Here is my jsp form where is called my method:
<form action = "Nota" method="post">
<div class="grid_6">
<input type="hidden" name="reteta1" value="${Nume} "/>
<input type="submit" name="nota" class="btnrtg" value="Acorda Nota!"/>
<input class="star star-5" id="star-5" type="radio" name="mark" value="100"/>
<label class="star star-5" for="star-5"></label>
<input class="star star-4" id="star-4" type="radio" name="mark" value="75"/>
<label class="star star-4" for="star-4"></label>
<input class="star star-3" id="star-3" type="radio" name="mark" value="50"/>
<label class="star star-3" for="star-3"></label>
<input class="star star-2" id="star-2" type="radio" name="mark" value="25"/>
<label class="star star-2" for="star-2"></label>
<input class="star star-1" id="star-1" type="radio" name="mark" value="0"/>
<label class="star star-1" for="star-1"></label>
</div>
</form>
In "Retete" is another servlet wehere are made extracting from db operations.
Could anyone help me? Thanks.
回答1:
First of all the way you are doing is wrong still i have a solution for you. and also you don't have explained whole flow.
Still follow steps below it will make your code tightly coupled but still will work..
- Add data extracting code in your java code after insertion and before forwarding that is
ps.executeUpdate();
//place your extracting code here
String destination = "/Retete";
Then add your extracted data to a variable
again add it to response
Then on your jsp page show that data
Finally your should look like this
DBConnection connection = new DBConnection();
Connection con = connection.Connect();
PreparedStatement ps=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 = '"+retetaid+"'),('"+nota+"'))");
ps.executeUpdate();
String destination = "/Retete";
// Enter your data extraction logic here and save all your data to a single generic variable
// Now here add the variable to the request like request.setAttribute("variableName",variable);
RequestDispatcher rd = request.getRequestDispatcher(destination);
rd.forward(request, response);
But remember if you are redirecting to other servlet from here then don't put response.sendRedirect(""); statement in other servlet if you want this data in JSP
And once again this is not the proper way to do it first refer MVC Design Pattern...
来源:https://stackoverflow.com/questions/37608960/stay-on-the-same-page-after-inserting-into-db