Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/382.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在jsp中将编辑按钮保存在同一表单上_Java_Mysql_Jsp_Servlets - Fatal编程技术网

Java 在jsp中将编辑按钮保存在同一表单上

Java 在jsp中将编辑按钮保存在同一表单上,java,mysql,jsp,servlets,Java,Mysql,Jsp,Servlets,我有一个简单的表单,我想保存数据,并通过在同一表单中给出第一个值来编辑数据。我可以保存数据,但来编辑我有问题,我认为我遵循了正确的方法,但我无法找到问题,我没有得到任何错误,我删除了我的总代码请解决此问题 index.jsp: <body> <%String status; status=(String)session.getAttribute("edit"); if(status=="fail") {%> <h6>there are no reco

我有一个简单的表单,我想保存数据,并通过在同一表单中给出第一个值来编辑数据。我可以保存数据,但来编辑我有问题,我认为我遵循了正确的方法,但我无法找到问题,我没有得到任何错误,我删除了我的总代码请解决此问题

index.jsp:

  <body>
 <%String status;
  status=(String)session.getAttribute("edit");
if(status=="fail")
{%>
<h6>there are no records with this no</h6>
<% }
%> 
   <form action="indexaction.jsp" method="post">
   Id <input type="text" name="id" id="id2"><br>
   <%
   if(status=="success")
   {%>
   <script type="text/javascript">
   document.getElementsById("id2").value='<%=request.getAttribute("id1")%>';
   </script>
   <%} %>
   Name <input type="text" name="name" id="name2"><br>
   <%
   if(status=="success")
   {%>
   <script type="text/javascript">
   document.getElementsById("name2").value='<%=request.getAttribute("name1")%>';
   </script>
   <%} %>
   sex <input type="text" name="sex" id="sex2"><br>
   <%
   if(status=="success")
   {%>
   <script type="text/javascript">
   document.getElementsById("sex2").value='<%=request.getAttribute("sex1")%>';
   </script>
   <%} %>
   <input type="submit" name="action" value="save">
   <input type="submit" name="action" value="edit">
   </form>
  </body>  
index.jsp:

<body>

 <%!

 PreparedStatement preparedStatement;
  int id1;
  String name1,sex1;
  %>

  <%
  Connection connection=DBCreation.getConnection();
  String action=request.getParameter("action");
  int i=Integer.parseInt(request.getParameter("id"));
  String name=request.getParameter("name");
  String sex=request.getParameter("sex");

  if(action.equals("save")){

  preparedStatement=connection.prepareStatement("insert into sample values(?,?,?)");
  preparedStatement.setInt(1, i);
  preparedStatement.setString(2, name);
  preparedStatement.setString(3, sex);
  int j=preparedStatement.executeUpdate();
  if(j>0)
  {
    out.println("<h3>inserted success</h3>");
    response.sendRedirect("index.jsp");
  }
  }
  else if(action.equals("edit")){
  int x=0;
  System.out.println("edit called");
  preparedStatement=connection.prepareStatement("select * from sample where id=?");
  preparedStatement.setInt(1, i);
  ResultSet resultSet=preparedStatement.executeQuery();

  while(resultSet.next())
  {
  x++;
  request.getSession().setAttribute("edit", "success");
  System.out.println("while called"+x);
   id1=resultSet.getInt(1);
   name1=resultSet.getString(2);
  sex1=resultSet.getString(3);

  request.setAttribute("id1", id1);
  request.setAttribute("name1",name1);
  request.setAttribute("sex1", sex1);
  }


  if(x<=0)
        {
            request.getSession().setAttribute("edit", "fail");

        }

  response.sendRedirect("index.jsp");
        }
   %>
  </body>
</html>

使用此代码,如果我输入了错误的id,我会收到错误消息,没有带有此“否”的记录,这意味着它工作正常,因为设置了状态“fail”。如果我输入正确的id,问题就来了。thanku

我发现每次比较字符串时,都使用==这是不正确的,所以请使用status.equalstr

编辑:第二件事不要使用重定向,因为它会创建一个新的请求,这就是为什么您会丢失参数,所以请使用forward

语法:

pageContext.forward("index.jsp");
试试这个

<body>
 <%String status;
  status=(String)session.getAttribute("edit");
if(status.equals("fail")) //compare string with .equals not ==
{%>
<h6>there are no records with this no</h6>
<% }
%> 
   <form action="indexaction.jsp" method="post">
   Id <input type="text" name="id" id="id2"><br>
   <%
   if(status=="success")
   {%>
   <script type="text/javascript">
   document.getElementsById("id2").value='<%=request.getAttribute("id1")%>';
   </script>
   <%} %>
   Name <input type="text" name="name" id="name2"><br>
   <%
   if(status=="success")
   {%>
   <script type="text/javascript">
   document.getElementsById("name2").value='<%=request.getAttribute("name1")%>';
   </script>
   <%} %>
   sex <input type="text" name="sex" id="sex2"><br>
   <%
   if(status=="success")
   {%>
   <script type="text/javascript">
   document.getElementsById("sex2").value='<%=request.getAttribute("sex1")%>';
   </script>
   <%} %>
   <input type="submit" name="action" value="save">
   <input type="submit" name="action" value="edit">
   </form>
  </body>  

如果您输入正确的id,您会得到什么?感谢您的响应,控制台中没有错误,我在编辑逻辑中编写了一些println消息,这些消息都已执行,x值已递增。还有什么问题我不知道,我仍然不理解您的问题。.如果您检查我的代码,在输入正确的id后,单击编辑按钮else iAction.equalEdit{}执行并输入到,同时设置循环状态“success”,并将所有值设置为session,我将页面重定向到index.jsp,在那里我检查状态是否为success,然后使用id属性(如果输入类型)将值设置为文本框。请粘贴堆栈跟踪。。。我认为问题在于sql语句。如果我理解正确,您使用“保存”按钮意味着插入和编辑按钮手动更新…仍然存在相同的问题,状态失败有效,但成功无效,如果可能的话,请执行我的代码。您是否使用“相等”进行了检查?好的,我正在检查。@kumar请查看编辑答案并告诉我这是否有效?