Java通过HttpURLConnection与servlet通信
作者:网络转载 发布时间:[ 2015/6/9 13:16:38 ] 推荐标签:编程语言
研究了才搞清楚,其实挺简单的,在这里记录下,以便以后参考。
一、创建一个servlet项目
主要包括(WEB-INF)里面有classes文件夹、lib文件夹、web.xml文件。
将写好的servlet类编译成的.class放到项目中的classes文件夹下(我的是helloServlet.class)
1、在web.xml中配置
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<servlet>
<servlet-name>HelloServlet</servlet-name>
<servlet-class>helloServlet</servlet-class><!-路径加名称,如果是在classes中的myclass中则用:myclass/helloServlet。如果是lib中的.jar包则用com.gis.arcgisengine.ServiceContext的形式->
</servlet>
<servlet-mapping>
<servlet-name>HelloServlet</servlet-name>
<url-pattern>/hello</url-pattern><!-通过url访问的别名,实际上是对helloServlet.class的访问,前面的/不能少->
</servlet-mapping>
</web-app>
2、helloServlet文件(是servlet类)
import java.io.IOException;
import java.io.PrintWriter;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class helloServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public helloServlet() {
super();
}
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request,response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//overwrite
String[] myName = null;
response.setContentType("application/octet-stream");
try {
ObjectInputStream ois = new ObjectInputStream(request.getInputStream());
Object obj = ois.readObject();//读取输入对象(传过来的流对象)
ois.close();
myName = (String[])obj;
for(int i=0;i<myName.length;i++){
System.out.println(myName[i]);
}
} catch (Exception e) {
e.printStackTrace();
}
String name = request.getParameter("name");
ServletOutputStream out = response.getOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(out);
oos.writeObject(name);//写入输出对象(返回给调用函数)
oos.close();
out.close();
/*String name = request.getParameter("name");
if(name != null){
name = new String(name.getBytes("ISO-8859-1"),"GB2312");
}else{
name = "默认值";
}
response.setContentType("text/html;charset=GB2312");
PrintWriter out = response.getWriter();
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
out.println("<h1>"+name+":你好</h1>");
out.println(" </BODY>");
out.println("</HTML>");
System.out.println(name);
out.flush();
out.close();*/
}
public void init() throws ServletException {
// Put your code here
}
}
相关推荐
更新发布
功能测试和接口测试的区别
2023/3/23 14:23:39如何写好测试用例文档
2023/3/22 16:17:39常用的选择回归测试的方式有哪些?
2022/6/14 16:14:27测试流程中需要重点把关几个过程?
2021/10/18 15:37:44性能测试的七种方法
2021/9/17 15:19:29全链路压测优化思路
2021/9/14 15:42:25性能测试流程浅谈
2021/5/28 17:25:47常见的APP性能测试指标
2021/5/8 17:01:11