今天学习了java web学生信息系统的修改操作。
jsp代码如下:
<%@page import="com.servlet.Student" %> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <% Student s=(Student)request.getAttribute("student"); %> <form action="editsuccess" method="post"> <input type="hidden" name="id" value="<%=s.getId()%>"><br> 姓名:<input type="text" name="name"><br> 年龄:<input type="text" name="age"><br> 学号:<input type="text" name="num"><br> <input type="submit" value="提交"> </form> </body> </html>
servlet代码如下:
package com.servlet; import java.io.IOException; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.jdbc.jdbcUtil; /** * Servlet implementation class edit */ @WebServlet("/edit") public class edit extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public edit() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub Integer id=Integer.parseInt(request.getParameter("id").toString()); try { Connection con=jdbcUtil.getcon(); String sql="select * from stu where id=?"; PreparedStatement pst=con.prepareStatement(sql); pst.setInt(1, id); ResultSet rs=pst.executeQuery(); Student s=null; while(rs.next()) { Integer newid=rs.getInt("id"); String name=rs.getString("name"); Integer age=rs.getInt("age"); String num=rs.getString("num"); s=new Student(newid,name,age,num); } jdbcUtil.close(con, pst, null); request.setAttribute("student", s); request.getRequestDispatcher("edit.jsp").forward(request, response); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub } }
package com.servlet; import java.io.IOException; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.jdbc.jdbcUtil; /** * Servlet implementation class editsuccess */ @WebServlet("/editsuccess") public class editsuccess extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public editsuccess() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub request.setCharacterEncoding("utf-8"); try { Connection con=jdbcUtil.getcon(); String sql="update stu set name=?,age=?,num=? where id=?"; PreparedStatement pst=con.prepareStatement(sql); pst.setString(1, request.getParameter("name")); pst.setInt(2, Integer.parseInt(request.getParameter("age").toString())); pst.setString(3, request.getParameter("num")); pst.setInt(4, Integer.parseInt(request.getParameter("id").toString())); pst.executeUpdate(); jdbcUtil.close(con, pst, null); response.sendRedirect(request.getContextPath()+"/list"); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }