JavaWeb课程复习资料(十)——修改功能
生活随笔
收集整理的這篇文章主要介紹了
JavaWeb课程复习资料(十)——修改功能
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
承接上文:JavaWeb課程復(fù)習(xí)資料(九)——刪除功能
目錄
1、創(chuàng)建【UpdateByIdServlet.java】
2、創(chuàng)建【UpdateServlet.jsp】
3、創(chuàng)建【UpdateServlet.jsp】
4、執(zhí)行測試
1、創(chuàng)建【UpdateByIdServlet.java】
?
package com.item.servlet;import com.item.dao.UserInfoDAO; import com.item.daoimpl.UserInfoDAOImpl; import com.item.pojo.UserInfo;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 java.io.IOException;@WebServlet("/UpdateByIdServlet") public class UpdateByIdServlet extends HttpServlet {@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {resp.setContentType("text/html;charset=utf-8");req.setCharacterEncoding("utf-8");String id=req.getParameter("id");int i = Integer.parseInt(id);UserInfoDAO db=new UserInfoDAOImpl();UserInfo userInfo = db.SelectById(i);req.setAttribute("userInfo",userInfo);req.getRequestDispatcher("UpdateServlet.jsp").forward(req,resp);}@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {doPost(req,resp);} }2、創(chuàng)建【UpdateServlet.jsp】
?
<%@ page import="com.item.pojo.UserInfo" %><%--Created by IntelliJ IDEA.User: 紅目香薰Date: 2022/2/26Time: 13:30To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head><title>修改頁面</title><link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/css/bootstrap.min.css"> </head> <body> <form action="/UpdateServlet" method="post"><%UserInfo userInfo=(UserInfo)request.getAttribute("userInfo");%><p><input type="text" name="id" value="<%= userInfo.getId()%>" class="form-control" readonly required/></p><p>創(chuàng)建時間:<%= userInfo.getCreateDate()%></p><p><input type="text" placeholder="請輸入用戶名" value="<%= userInfo.getUserName()%>" name="userName" class="form-control" required/></p><p><input type="text" placeholder="請輸入愛好" value="<%= userInfo.getIntroduce()%>" name="introduce" class="form-control" required/></p><p><input type="submit" value="修改" class="btn btn-primary"/></p> </form> </body> </html>3、創(chuàng)建【UpdateServlet.jsp】
?
package com.item.servlet;import com.item.dao.UserInfoDAO; import com.item.daoimpl.UserInfoDAOImpl; import com.item.pojo.UserInfo;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 java.io.IOException; import java.io.PrintWriter;@WebServlet("/UpdateServlet") public class UpdateServlet extends HttpServlet {@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {resp.setContentType("text/html;charset=utf-8");req.setCharacterEncoding("utf-8");String id = req.getParameter("id");String userName = req.getParameter("userName");String introduce = req.getParameter("introduce");PrintWriter out = resp.getWriter();if (id == null || userName == null || introduce == null) {out.println("<script>alert('參數(shù)不允許為空。')</script>");}UserInfoDAO db = new UserInfoDAOImpl();UserInfo userInfo = db.SelectById(Integer.parseInt(id));userInfo.setUserName(userName);userInfo.setIntroduce(introduce);boolean isf = db.UpdateById(userInfo);if(isf){out.println("<script>alert('修改成功');window.location.href='/index.jsp';</script>");}else{out.println("<script>alert('修改失敗');window.location.href='/index.jsp';</script>");}}@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {doPost(req, resp);} }4、執(zhí)行測試
?
?
?
測試成功。?
整個Servlet完成,希望能給大家?guī)硪欢ǖ膸椭?/p>
總結(jié)
以上是生活随笔為你收集整理的JavaWeb课程复习资料(十)——修改功能的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: JavaWeb课程复习资料——中文乱码上
- 下一篇: JavaWeb课程复习资料——用于突击考