GET、POST、添加、编辑
1、Get
1.采用URL请求路径传输参数,参数拼接在URL后面
2.参数传输过程中隐私性较差,直接在URL后面
3.路径可以容纳的数据有限,只能传递少量参数
4.form表单请求默认就是get
http://localhost:8080/student?method=deleteById&id=23
http://localhost:8080/student?name=zhangsan&age=12&gender=男
Get方式传参,不是非得在form表单里面,可以手动写,在超链接的href里面直接在地址后面加?id=2
2、POST
1.采用实体内容传参数
2.参数在传输过程中不可见,隐私性好
3.实体内容专门用来传输数据,大小没有限制
4.使用:在form上加method=“post”
不管是Get方式还是POST方式传参数,后台代码获取参数的方式都是一样的。
req.getParameter(“name”);
3、添加
case "add":add(req,resp);break;private void add(HttpServletRequest req, HttpServletResponse resp) throws IOException {System.out.println("StudentServlet.add");String name = req.getParameter("name");String age = req.getParameter("age");String gender = req.getParameter("gender");Connection connection = null;PreparedStatement preparedStatement = null;//不是查询类,没有结果集try {connection = JDBCUtil.getConnection();String sql = "INSERT INTO student(name,age,gender) VALUES(?,?,?)";//一个一个填补括号中的“?”preparedStatement = connection.prepareStatement(sql);preparedStatement.setString(1,name);preparedStatement.setInt(2,Integer.parseInt(age));preparedStatement.setString(3,gender);//打印出来,显示在执行什么语句(非必须)System.out.println(preparedStatement);//返回影响行数int count = preparedStatement.executeUpdate();System.out.println("count: " + count);} catch (SQLException e) {throw new RuntimeException(e);} finally {JDBCUtil.close(connection,preparedStatement,null);}//更新类的操作,同样也需要重定向resp.sendRedirect("/student");}
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head><title>Title</title>
</head>
<body><%--action 属性用于指定表单提交时数据要发送到的目标地址--%><%--form表单默认getget方法会将传过来的参数直接写在地址后面(后面),这会覆盖掉我们原先写在“?”后面的“method=add”例如http://localhost:8080/student?name=zhangsan&age=12&gender=男所以需要用post方法接收(参数不会显示在地址后面,覆盖我们自己的method)--%><form action="/student?method=add" method="post">姓名:<input type="text" name="name"><br>年龄:<input type="text" name="age"><br>性别:<input type="text" name="gender"><br><input type="submit" value="添加"></form>
</body>
</html>
4、编辑
case "toUpdate":toUpdate(req,resp);break;case "update":update(req,resp);break;
//完成update后台操作private void update(HttpServletRequest req, HttpServletResponse resp) throws IOException {System.out.println("StudentServlet.update");String id = req.getParameter("id");String name = req.getParameter("name");String age = req.getParameter("age");String gender = req.getParameter("gender");Connection connection = null;PreparedStatement preparedStatement = null;//不是查询类,没有结果集try {connection = JDBCUtil.getConnection();String sql = "UPDATE student SET name=?,age=?,gender=? WHERE id=?";//一个一个填补括号中的“?”preparedStatement = connection.prepareStatement(sql);preparedStatement.setString(1,name);preparedStatement.setInt(2,Integer.parseInt(age));preparedStatement.setString(3,gender);preparedStatement.setInt(4,Integer.parseInt(id));//打印出来,显示在执行什么语句(非必须)System.out.println(preparedStatement);//返回影响行数int count = preparedStatement.executeUpdate();System.out.println("count: " + count);} catch (SQLException e) {throw new RuntimeException(e);} finally {JDBCUtil.close(connection,preparedStatement,null);}resp.sendRedirect("/student");}//toUpdate是为了去后台拿数据,用于数据回显,是update的一个中间过程private void toUpdate(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {//通过selectAll代码修改的System.out.println("StudentServlet.toUpdate");//拿出request中的idString id = req.getParameter("id");Connection connection = null;PreparedStatement preparedStatement = null;ResultSet resultSet = null;//在这里不再是封装成list集合,封装为一个student对象即可Student student = null;try {connection = JDBCUtil.getConnection();String sql = "SELECT id,name,age,gender FROM student WHERE id=?";preparedStatement = connection.prepareStatement(sql);//填补“?”preparedStatement.setInt(1,Integer.parseInt(id));System.out.println(preparedStatement);resultSet = preparedStatement.executeQuery();while (resultSet.next()) {String name = resultSet.getString("name");int age = resultSet.getInt("age");String gender = resultSet.getString("gender");student = new Student(Integer.parseInt(id), name, age, gender);}}//ClassNotFoundException在JDBCUtil静态代码块中已被捕获,所以不需要在这里再捕获了catch (SQLException e) {throw new RuntimeException(e);} finally {JDBCUtil.close(connection, preparedStatement, resultSet);}//(设置属性)req.setAttribute("student",student);//转发到student_update.jsp页面进行数据回显req.getRequestDispatcher("student_update.jsp").forward(req,resp);}
<%@ page import="com.easy.web.pojo.Student" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head><title>Title</title>
</head>
<body><%--action 属性用于指定表单提交时数据要发送到的目标地址--%><%--form表单默认getget方法会将传过来的参数直接写在地址后面(后面),这会覆盖掉我们原先写在“?”后面的“method=add”例如http://localhost:8080/student?name=zhangsan&age=12&gender=男所以需要用post方法接收(参数不会显示在地址后面,覆盖我们自己的method)--%><%--JS脚本,写java代码获取封装的student--%><%Student student = (Student) request.getAttribute("student");%><%--确认修改后跳转到/student?method=update进行数据库修改,修改真实数据--%><form action="/student?method=update" method="post"><%--value属性展示数据--%><input type="hidden" name="id" value="<%=student.getId()%>">姓名:<input type="text" name="name" value="<%=student.getName()%>"><br>年龄:<input type="text" name="age" value="<%=student.getAge()%>"><br>性别:<input type="text" name="gender" value="<%=student.getGender()%>"><br><input type="submit" value="修改"></form>
</body>
</html>