String拼接字符串开始位置出现null问题

今天在写一个jsp页面请求一个rest接口,并把返回的json串返回的过程中出现了一个很奇怪的问题——jsp返回的json串前面多出来了一个null。直接调用rest接口并没有这样的情况,跟同事请教以后发现是因为字符串在声明的时候赋值为null了,在后面再对字符串进行拼接的时候就在开始位置出现了null,解决办法有两个:

  1. 使用String类型的话再声明的时候赋值为空串。再进行字符串拼接,示例如下:
	String result = "";
	result += line;
  1. 使用StringBuffer或者StringBuilder,拼接的时候使用append方法,示例如下:
	StringBuilder sb = new StringBuilder();
	sb.append(line);

下面是截取的打印有null的代码,可能的原因是,vm在进行拼接的时候,如果之前声明为null的会把null放在最前面进行拼接。

    String content = null;
    String line;
    String result = "";
    String url = "http://localhost:8133/xxxxx/rest/xxxx/";
    String method = request.getParameter("callMethod");
    if ("queryKeyKpiValues".equals(method)) {
        url += "queryKeyKpiValues";
        URL postUrl = new URL(url);
        HttpURLConnection connection = (HttpURLConnection) postUrl.openConnection();
        connection.setDoInput(true);
        connection.setDoOutput(true);
        connection.setRequestMethod("POST");
        connection.setUseCaches(false);
        connection.setInstanceFollowRedirects(true);
        connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        connection.connect();
        DataOutputStream outputStream = new DataOutputStream(connection
                .getOutputStream());

        String keyKpiType = request.getParameter("keyKpiType");
        if (keyKpiType != null) {
            content = "keyKpiType=" + URLEncoder.encode(keyKpiType, "utf-8");
        }
        String timeType = request.getParameter("timeType");
        if (timeType != null) {
            content += "?timeType=" + URLEncoder.encode(timeType,"utf-8");
        }
        outputStream.writeBytes(content);

        outputStream.flush();
        outputStream.close(); // flush and close

        BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
        while ((line = reader.readLine()) != null) {
            result += line;
        }
        reader.close();
        connection.disconnect();
        out.print(result);

    }

在迁移一个老应用的时候又遇到了另外一个问题。

错误信息: Unable to compile class for JSP: An error occurred at line: 27 in the jsp file: /kpi/kpiBasicConf.jsp The type java.lang.CharSequence cannot be resolved. It is indirectly referenced from required .class files 24: for (int i = 0; i < l.size(); i++) { 25: Code code = l.get(i); 26: JSONArray arr2 = new JSONArray(); 27: arr2.put(code.getConfigID()); 28: arr2.put(code.getConfigName()); 29: arr.put(arr2); 30: } Stacktrace:
Stack Trace is :
org.apache.jasper.JasperException: Unable to compile class for JSP: 

An error occurred at line: 27 in the jsp file: /kpi/kpiBasicConf.jsp
The type java.lang.CharSequence cannot be resolved. It is indirectly referenced from required .class files
24: 			for (int i = 0; i < l.size(); i++) {
25: 				Code code = l.get(i);
26: 				JSONArray arr2 = new JSONArray();
27: 				arr2.put(code.getConfigID());
28: 				arr2.put(code.getConfigName());
29: 				arr.put(arr2);
30: 			}

错误原因是因为低版本的tomcat不兼容jdk8。包括tomcat6和一些早期版本的tomcat7.

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值