request.getParameter(String arg0)获取的字符串为空或者是空串问题小白分析

版权声明: https://blog.csdn.net/acDream_/article/details/82594834

1.request.getParameter(String arg0)获得字符串为空
            1).参数与name属性的属性值不匹配
            2).参数与id属性的属性值匹配而没有设置name属性的属性值
            3).未通过POST/GET传递过来所对应的参数值
            4).与URL里的参数不匹配

2.request.getParameter(String arg0)获得字符串为空串
            1).提交是value属性的属性值为"";


运行一下如下的JSP代码应该很快就会明白:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'index.jsp' starting page</title>
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
  </head>
  
  <body>
    <form action="" method="post">
    <input type="text" value="" name="ts" id="tsId"><br>
    <input type="submit" value="提交">
    </form>
     <a href="index.jsp?tsURL=我是URL里的提示信息">跳转</a><br>
    	提示信息:
    <%	
    	String ts = request.getParameter("ts");
    	if(ts==null){
    		out.print("我是Null!");
    	}else if(ts.equals("")){
    		out.print("我是一个空串!");
    	}else{
    		out.print(ts);
    	}
    	out.print("<br>");
    	if(request.getParameter("tsURL")!=null){
    		out.print(request.getParameter("tsURL"));
    	}
    	
    	/**
    	1.request.getParameter(String arg0)获得字符串为空
    		1).参数与name属性的属性值不匹配
    		2).参数与id属性的属性值匹配而没有设置name属性的属性值
    		3).未通过POST/GET传递过来对应的参数值
    		4).与URL里的参数不匹配
    	2.request.getParameter(String arg0)获得字符串为空串
    		1).提交是value属性的属性值为"";
    	**/
     %>
  </body>
</html>

刚刚进来的时候是没有通过POST/GET传递过来参数值的,所以一开始显示的我是Null!

而在输入框里什么都不加提交时获得的是空串,因为value属性默认的属性值为"",显示的是我是一个空串

之后在输入框中添加一些信息的时候,原封不动打印

点击跳转后会过得URL里的参数值,在最下面打印我是URL里面的提示信息

猜你喜欢

转载自blog.csdn.net/acDream_/article/details/82594834