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

时间:2014-03-13 05:32:44

标签: java mysql jsp servlets

我有简单的形式,我想保存数据并通过给出相同形式的第一个值来编辑数据。我能够保存数据但是来编辑我有问题,我想我遵循正确的方法但我是无法找到问题,我没有得到任何错误,我删除我的总代码,请解决此问题

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

indexaction.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我收到错误消息“没有这个没有记录”,意味着它工作正常,因为状态'失败'被设置。如果我输入正确的ID问题就来了.thanku

2 个答案:

答案 0 :(得分:0)

问题我发现每次比较字符串时,你使用的是 == ,这是不正确的,所以请使用status.equals("str")

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

语法:

pageContext.forward("index.jsp");

答案 1 :(得分:0)

试试这个

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