【救命】一个不到20行的代码,一直调不通

lovefox_zoe 2017-12-15 08:30:12
1、开发环境:JDK1.8 和JRE1.8,TOMCAT 8.5

2、主代码:
<%@ page language="java" contentType="text/html; charset=UTF-8"
import="java.io.*"
import="com.http.*"
import="net.sf.json.*"
pageEncoding="UTF-8" %>
<body>
<%--
浏览器或客户端通过POST方式提交请求。请求内容为JSON数据。
通过自定义函数 HttpRequestCommon.getPostData 可以从本次请求的输入流中读取到完整的POST数据
读取成功后,使用JSONObject解析,然后读取我们需要的参数进行处理
需要判断json是否解析成功,不成功可以返回一个错误标识。
--%>
<%
String postdata = Common.getPostData(request.getInputStream(), request.getContentLength(), null);
out.println(postdata);

if (postdata != null) {
JSONObject json = JSONObject.fromObject(postdata);
if (json != null) {
out.println("{\"result\": 1, \"username\": \"" + json.getString("username") + "\"}" );
} else
out.println("{\"result\": 0, \"errmsg\": \"无效的请求\"}");
} else
out.println("{\"result\": 0, \"errmsg\": \"无效的请求\"}");
%>
</body>

3、定义了一个Common,代码如下:
package com.http;

import java.io.IOException;
import java.io.InputStream;


public class Common {

public static String getPostData(InputStream in, int size, String charset) {
if (in != null && size > 0) {
byte[] buf = new byte[size];
try {
in.read(buf);
if (charset == null || charset.length() == 0)
return new String(buf);
else {
return new String(buf, charset);
}
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
}

4、用命令行javac编译后Common.java后,把Common.class文件放到WebContent\WEB-INF\classes目录下。

5、运行后提示错误,如下:
type Exception report

message Unable to compile class for JSP:

description The server encountered an internal error that prevented it from fulfilling this request.

exception

org.apache.jasper.JasperException: Unable to compile class for JSP:

An error occurred at line: 15 in the jsp file: /test.jsp
Common cannot be resolved
6、源码地址:SVN: https://github.com/yangyxd/JSP_JSON_POST_Demo

...全文
224 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
强长黑粗硬 2017-12-16
  • 打赏
  • 举报
回复
>>用命令行javac编译后Common.java后,把Common.class文件放到WebContent\WEB-INF\classes >>package com.http; 你的Common定义了package,class文件要保存在和package相同的文件目录中的,如果直接放在classes下是不行的。
  • 打赏
  • 举报
回复
jsp导入Common类就行了
Inhibitory 2017-12-16
  • 打赏
  • 举报
回复
Common.class 文件应该放到 WebContent\WEB-INF\classes\com\http 目录下

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧