package servlet; import java.io.IOException; import java.io.PrintWriter; import javabean.User; import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class FindUser extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request,response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 获取信息 String username = request.getParameter("username"); //调用Javabean User user = new User(); user = user.findUserByName(username); // 传递值 request.setAttribute("user", user); // 选择界面对用户进行响应 RequestDispatcher rd = request.getRequestDispatcher("updateuser.jsp"); rd.forward(request, response); } }
11、编写修改信息的Servlet
参考代码如下:
package servlet; import java.io.IOException; import java.io.PrintWriter; import javabean.User; import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class UpdateUser extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request,response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 获取信息 String username = request.getParameter("username"); String userpass = request.getParameter("userpass"); //调用Javabean User user = new User(); user.setUsername(username); user.setUserpass(userpass); boolean b = user.update(); // 传递值 String info ; if(b) info="修改成功!"; else info="修改失败!"; // 选择界面对用户进行响应 RequestDispatcher rd = request.getRequestDispatcher("getAllUser"); rd.forward(request, response); } }