研究了才搞清楚,其实挺简单的,在这里记录下,以便以后参考。
  一、创建一个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
}
}