我想实现在1.jsp中点击保存按钮,将数据保存到数据库,然后页面跳转到2.jsp中。在2.jsp中会看到1.jsp中提交过来的数据。之后再2.jsp中按返回按钮返回到1.jsp。此时1.jsp中的数据已经清空,可以进行重新输入。我想看代码,因为刚学~~谢谢各位大侠、、

解决方案 »

  1.   

    才开始学的用servlet。一个servlet将1中提交的数据保存到数据库中、并跳转到2.jsp.
      

  2.   

    一共三个jsp文件,1和2是如你所说,3.jsp是处理1的相关操作的(为了简单,没有使用Servlet,而用3.jsp代之)1.jsp 代码如下:
    <%@ page contentType="text/html; charset=utf-8" language="java" import="java.sql.*" errorPage="" %>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>无标题文档</title>
    </head><body>
    <form name="form1" method="post" action="3.jsp">
      <table width="300" border="0" align="center" cellpadding="0" cellspacing="0">
         <tr align="center">
           <td colspan="2">用户注册</td>
        </tr>
         <tr>
           <td>用户名:</td>
           <td><input type="text" name="username"></td>
         </tr>
         <tr>
           <td>密 码:</td>
           <td><input type="password" name="passwords"></td>
         </tr>
         <tr>
           <td colspan="2" align="center"><input type="submit" name="Submit3" value="提交">
             
           <input type="reset" name="Submit22" value="重置"></td>
        </tr>
       </table>
    </form>
    </body>
    </html>
    <%@ page contentType="text/html; charset=utf-8" language="java" import="java.sql.*" errorPage="" %>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />2.jsp 代码如下:
    <title>无标题文档</title>
    </head><body>
    <p align="center">用户列表</p>
    <table width="300" border="1" align="center" cellpadding="0" cellspacing="0">
      <tr align="center">
        <td width="146">用户名</td>
        <td width="148">密码</td>
      </tr>
      <% 
    String username="";
    String passwords="";
    Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
    Connection conn=DriverManager.getConnection("jdbc:sqlserver://localhost:1433;DatabaseName=mydb","sa","12345");
    String sql="select * from user_tb";
    PreparedStatement ps=conn.prepareStatement(sql);
    ResultSet rs=ps.executeQuery();
    while(rs.next()){ 
    username=rs.getString(1);
    passwords=rs.getString(2);
    System.out.println("username--->"+username);
    System.out.println("passwords--->"+passwords);
    request.setAttribute("username",username);
    request.setAttribute("passwords",passwords);
    %>
    <tr align="center">
         <td>${username}</td>
         <td>${passwords}</td>
       </tr>
    <% } %>
    </table>
    <p align="center"><a href="1.jsp">返回</a></p>
    </body>
    </html>3.jsp 代码如下:
    <%@ page contentType="text/html; charset=utf-8" language="java" import="java.sql.*" errorPage="" %>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>无标题文档</title>
    </head>
    <% 
    String username=request.getParameter("username"); 
    String passwords=request.getParameter("passwords");
    Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
    Connection conn=DriverManager.getConnection("jdbc:sqlserver://localhost:1433;DatabaseName=mydb","sa","12345");
    String sql="insert into user_tb values(?,?)";
    PreparedStatement ps=conn.prepareStatement(sql);
    ps.setString(1,username);
    ps.setString(2,passwords);
    ps.executeUpdate();
    response.sendRedirect("2.jsp");
    %>
    <body>
    </body>
    </html>我也是初学者,可能做得也不太好