兰舟千帆 2022-05-11 19:10 采纳率: 76.2%
浏览 329
已结题

request.getParameter() 获取post为空的情况。

下面是存在问题的代码片段,分别是表单和Servlet,不知道为什么post提交的参数获取为null,方法为get时,可以正常获取到。我之前用过这样的方法,黑马那边讲的,之前类似这样的操作还可以正常获取到值,但是现在不知道为啥这个方法获取不到post提交的参数的键对应的值。


package com.jgdabc.web;

import com.jgdabc.pojo.Brand;
import com.jgdabc.service.BrandService;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.io.UnsupportedEncodingException;


@WebServlet("/addServlet")
public class AddServlet extends HttpServlet {
    private BrandService service = new BrandService();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, IOException {
        //接收表单提交的数据,封装为brand对象
//        处理乱码问题
        request.setCharacterEncoding("utf-8");


        String brandName = request.getParameter("brandName");
        String companyName = request.getParameter("companyName");
        String ordered = request.getParameter("ordered");
        String description = request.getParameter("description");
        String status = request.getParameter("status");
        //封装为一个Brand对象
        Brand brand = new Brand();
        brand.setBrandName(brandName);
        brand.setCompanyName(companyName);

        brand.setOrdered(Integer.parseInt(ordered));
        brand.setDescription(description);
        brand.setStatus(Integer.parseInt(status));
        service.add(brand);
//        转发到查询所有Servlet
        request.getRequestDispatcher("/selectAllServlet").forward(request, response);


    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}


<%--
  Created by IntelliJ IDEA.
  User: jgdabc
  Date: 2022/5/11
  Time: 17:33
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>添加品牌</title>
</head>
<body>
<h3>添加品牌</h3>
<form action="/brand-demo/addServlet" method="post">
    品牌名称:<input name="brandName"><br>
    企业名称:<input name="companyName"><br>
    排序:<input name="ordered"><br>
    描述信息:<textarea rows="5" cols="20" name="description"></textarea><br>
    状态:
    <input type="radio" name="status" value="0">禁用
    <input type="radio" name="status" value="1">启用<br>

    <input type="submit" value="提交">
</form>
</body>
</html>



  • 写回答

4条回答 默认 最新

  • 吕布辕门 后端领域新星创作者 2022-05-12 09:14
    关注

    写法错了,要把this.去掉。不然接收不到的。

    
     
    package com.jgdabc.web;
     
    import com.jgdabc.pojo.Brand;
    import com.jgdabc.service.BrandService;
     
    import javax.servlet.*;
    import javax.servlet.http.*;
    import javax.servlet.annotation.*;
    import java.io.IOException;
    import java.io.UnsupportedEncodingException;
     
     
    @WebServlet("/addServlet")
    public class AddServlet extends HttpServlet {
        private BrandService service = new BrandService();
     
        @Override
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, IOException {
            //接收表单提交的数据,封装为brand对象
    //        处理乱码问题
            request.setCharacterEncoding("utf-8");
     
     
            String brandName = request.getParameter("brandName");
            String companyName = request.getParameter("companyName");
            String ordered = request.getParameter("ordered");
            String description = request.getParameter("description");
            String status = request.getParameter("status");
            //封装为一个Brand对象
            Brand brand = new Brand();
            brand.setBrandName(brandName);
            brand.setCompanyName(companyName);
     
            brand.setOrdered(Integer.parseInt(ordered));
            brand.setDescription(description);
            brand.setStatus(Integer.parseInt(status));
            service.add(brand);
    //        转发到查询所有Servlet
            request.getRequestDispatcher("/selectAllServlet").forward(request, response);
     
     
        }
     
        @Override
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            doGet(request, response);
        }
    
      private 
    }
     
    

    img

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(3条)

报告相同问题?

问题事件

  • 系统已结题 5月20日
  • 已采纳回答 5月12日
  • 赞助了问题酬金10元 5月11日
  • 创建了问题 5月11日

悬赏问题

  • ¥115 用Java解决探地雷达dzt文件的解析过程
  • ¥20 有关神经网络这道(b)determine the parameters of neural network
  • ¥25 annaconda jvpyter
  • ¥20 来一个会抓包app支付接口的
  • ¥30 vivado2017.4的sdk打不开
  • ¥50 matlab,文献复现
  • ¥20 寻找Flink CDC指导,有偿咨询
  • ¥15 cruise如何仿真不同温度下车辆的经济性?
  • ¥20 扑克牌游戏的完整代码
  • ¥88 求帮安装Python架构的vnpy