1.编写一个包,实现一个servlet类,继承自httpservlet
package com.bzu.servlet;
import java.io.IOException;
import java.io.OutputStream;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class helloservlet extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
//根据响应,获得一个输出流
OutputStream stream=resp.getOutputStream();
//字符串转化成字节数组,输出一段字符串
stream.write("sasa".getBytes());
}
}
2.在web.xml中首先声明servlet,然后做出外部访问路径的映射
<servlet>
<servlet-name>helloservlet</servlet-name>
<servlet-class>com.bzu.servlet.helloservlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>helloservlet</servlet-name>
<url-pattern>/hello</url-pattern>
</servlet-mapping>