遇到request.getParameter()获取空值

本文介绍了在使用request.getParameter()获取参数值为空的问题及解决方案。详细解释了pageEncoding和charset的区别,并通过实例展示了正确的配置方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

编程学习就是一个不断填坑的过程大哭
今天遇到request.getParameter()获取空值的情况,各种查,最后终于找到原因,问题解决掉了!
说白了就是pageEncoding和charset的用法不同,以下是它们的区别:
pageEncoding表示这个页面接收到参数以什么字符集来编码,charset为指定此页面的编码方式。前者指明了此页面对request里的参数如何编码,后者指明了如何对本页面里的字符如何编码。
上源码:
parameter_1.jsp

<%@ page contentType="text/html;charset=GB2312"%>
<html>
<head>
    <title>jsp:parameter动作</title></head>
<body>
     <jsp:include page="parameter_2.jsp" flush="true">
         <jsp:param name="username" value="Tom" />
         <jsp:param name="password" value="123"/>
     </jsp:include>
</body>
</html>

parameter_2.jsp

<%@ page contentType="text/html" ****pageEncoding="UTF-8"**** %>
<html>
  <head>
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
    <title>jsp:parameter动作</title>
  </head>
  <body>
  <hr>
  用户名:<%=request.getParameter("username")%><br>
  密码:<%=request.getParameter("password")%>
  <hr>
  </body>
</html>

parameter_2.jsp中的pageEncoding=”UTF-8”千万不要写成charset,否则就会获取到空值。

package com.hafu.servlet; import com.hafu.model.Student; import com.hafu.service.StudentService; 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("/student") public class StudentServlet extends HttpServlet { private StudentService studentService = new StudentService(); protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String action = request.getParameter("action"); if (action == null) { action = "list"; } try { switch (action) { case "new": showNewForm(request, response); break; case "edit": showEditForm(request, response); break; case "delete": deleteStudent(request, response); break; default: listStudents(request, response); break; } } catch (Exception e) { e.printStackTrace(); } } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String action = request.getParameter("action"); if (action == null) { action = "list"; } try { switch (action) { case "insert": insertStudent(request, response); break; case "update": updateStudent(request, response); break; default: listStudents(request, response); break; } } catch (Exception e) { e.printStackTrace(); } } private void listStudents(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setAttribute("students", studentService.getAllStudents()); request.getRequestDispatcher("student-list.jsp").forward(request, response); } private void showNewForm(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.getRequestDispatcher("student-form.jsp").forward(request, response); } private void showEditForm(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { int id = Integer.parseInt(request.getParameter("id")); Student student = studentService.getStudentById(id); request.setAttribute("student", student); request.getRequestDispatcher("student-form.jsp").forward(request, response); } private void insertStudent(HttpServletRequest request, HttpServletResponse response) throws IOException { Student student = new Student(); student.setName(request.getParameter("name")); student.setGender(request.getParameter("gender")); student.setAge(Integer.parseInt(request.getParameter("age"))); student.setDepartment(request.getParameter("department")); student.setEmail(request.getParameter("email")); student.setPhone(request.getParameter("phone")); studentService.addStudent(student); response.sendRedirect("student"); } private void updateStudent(HttpServletRequest request, HttpServletResponse response) throws IOException { int id = Integer.parseInt(request.getParameter("id")); Student student = studentService.getStudentById(id); student.setName(request.getParameter("name")); student.setGender(request.getParameter("gender")); student.setAge(Integer.parseInt(request.getParameter("age"))); student.setDepartment(request.getParameter("department")); student.setEmail(request.getParameter("email")); student.setPhone(request.getParameter("phone")); studentService.updateStudent(student); response.sendRedirect("student"); } private void deleteStudent(HttpServletRequest request, HttpServletResponse response) throws IOException { int id = Integer.parseInt(request.getParameter("id")); studentService.deleteStudent(id); response.sendRedirect("student"); } }报错
最新发布
06-03
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值