67,513
社区成员
发帖
与我相关
我的任务
分享
//这是servlet程序
package cn.eden.httpServer;
import java.io.*;
import javax.servlet.http.*;
import javax.servlet.*;
import cn.eden.connetGameWorld.Client;
public class HelloWebTest extends HttpServlet {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException
{
String username =req.getParameter("username");
String password =req.getParameter("password");
System.out.println("Thereceived username and password is: " + username + "/" +password);
PrintWriter out =resp.getWriter();
out.print("OK");
out.flush();
out.close();
super.doPost(req, resp);
}
}
//这是模拟HTTP请求
public class PostRequestEmulator
{
public static void main(String[] args) throws Exception {
// 服务地址
URL url = new URL("http://127.0.0.1:8080/HelloWebTest/test");
// 设定连接的相关参数
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setRequestMethod("POST");
OutputStreamWriter out = new OutputStreamWriter(
connection.getOutputStream(), "UTF-8");
// 向服务端发送key = value对
out.write("username=kevin&password=pass");
out.flush();
out.close();
// 获取服务端的反馈
String strLine = "";
String strResponse = "";
InputStream in = connection.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
while ((strLine = reader.readLine()) != null) {
strResponse += strLine + "\n";
}
System.out.print(strResponse);
}
}
Thereceived username and password is: kevin/pass
2012-4-12 15:55:46 org.apache.catalina.core.StandardWrapperValve invoke
严重: Servlet.service() for servlet [test] in context with path [/HelloWebTest] threw exception
java.lang.IllegalStateException: Cannot call sendError() after the response has been committed