JavaWeb课程复习资料(十)——修改功能

举报
红目香薰 发表于 2022/02/27 14:17:12 2022/02/27
【摘要】 ​承接上文:JavaWeb课程复习资料(九)——删除功能目录1、创建【UpdateByIdServlet.java】2、创建【UpdateServlet.jsp】3、创建【UpdateServlet.jsp】4、执行测试1、创建【UpdateByIdServlet.java】​package com.item.servlet;import com.item.dao.UserInfoDAO;i...

承接上文:JavaWeb课程复习资料(九)——删除功能

目录

1、创建【UpdateByIdServlet.java】

2、创建【UpdateServlet.jsp】

3、创建【UpdateServlet.jsp】

4、执行测试


1、创建【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 {
    @Override
    protected 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);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }
}

2、创建【UpdateServlet.jsp】

<%@ page import="com.item.pojo.UserInfo" %><%--
  Created by IntelliJ IDEA.
  User: 红目香薰
  Date: 2022/2/26
  Time: 13:30
  To 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>
        创建时间:<%= 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、创建【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 {
    @Override
    protected 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('参数不允许为空。')</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>");
        }
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }
}

4、执行测试

测试成功。 

整个Servlet完成,希望能给大家带来一定的帮助。

【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。