插入数据库后留在同一页面上 [英] Stay on the same page after inserting into db

查看:111
本文介绍了插入数据库后留在同一页面上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在插入数据库,操作完成后,我想停留在同一页面上,插入后的结果将一直存在.

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.

这是我的Java代码:

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();
        }
    }

这是我的jsp表单,称为我的方法:

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>

在"Retete"中,是从数据库操作中提取的另一个servlet.

In "Retete" is another servlet wehere are made extracting from db operations.

有人可以帮助我吗? 谢谢.

Could anyone help me? Thanks.

推荐答案

首先,您做的方式是错误的,但我仍然为您提供解决方案. 而且您也没有解释整个流程.

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..

  1. 在插入之后和转发之前在Java代码中添加数据提取代码

ps.executeUpdate();

ps.executeUpdate();

//在这里放置提取代码

//place your extracting code here

字符串目标="/Retete";

String destination = "/Retete";

  1. 然后将提取的数据添加到变量

  1. Then add your extracted data to a variable

再次将其添加到响应

然后在您的jsp页面上显示该数据

Then on your jsp page show that data

最后您应该看起来像这样

Finally your should look like this

DBConnection连接=新的DBConnection();

DBConnection connection = new DBConnection();

连接con = connection.Connect();

Connection con = connection.Connect();

PreparedStatement ps = con.prepareStatement(插入到nota(Id_utilizator,Nume_reteta,Nota)值中((从E_mail ='" + request.getSession().getAttribute("email")+')的用户中选择Id_user) ,(SELECT Nume FROM reteta其中Nume ='"+ retetaid +"'),('"+ nota +"')));

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();

ps.executeUpdate();

字符串目标="/Retete";

String destination = "/Retete";

//在此处输入数据提取逻辑,并将所有数据保存到单个通用变量

// Enter your data extraction logic here and save all your data to a single generic variable

//现在将变量添加到请求中,例如request.setAttribute("variableName",variable);

// Now here add the variable to the request like request.setAttribute("variableName",variable);

RequestDispatcher rd = request.getRequestDispatcher(destination);

RequestDispatcher rd = request.getRequestDispatcher(destination);

rd.forward(请求,响应);

rd.forward(request, response);

但是请记住,如果您要从此处重定向到其他servlet,那么如果您希望在JSP中使用此数据,请不要在其他servlet中放置response.sendRedirect("");语句 再一次,这不是正确的方法,首先请参考MVC设计模式...

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...

这篇关于插入数据库后留在同一页面上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆