你的位置:首页 > Java教程

[Java教程]Servlet接口应用(开发servlet三种方式)


第一种:实现Servlet接口

public class ServletDemo1 implements Servlet {

@Override
public void init(ServletConfig config) throws ServletException {
// TODO Auto-generated method stub

}

@Override
public ServletConfig getServletConfig() {
// TODO Auto-generated method stub
return null;
}

@Override
public void service(ServletRequest req, ServletResponse res)
throws ServletException, IOException {
HttpServletRequest request=(HttpServletRequest) req; //类型转换
HttpServletResponse response=(HttpServletResponse) res;
//获取请求的类型
String type=request.getMethod();
//判断类型GET/POST
if("GET".equals(type)){
this.doGet(request,response);
}else{
this.doPost(request,response);
}

}

private void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
// TODO Auto-generated method stub

response.setContentType("text/html;charset=utf-8");//设置response 字符编码
PrintWriter writer = response.getWriter();//得到打印流对象
writer.print("实现Servlet类的方式1,实现Servlet接口");//浏览器打印

}

private void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
// TODO Auto-generated method stub
doPost(request, response);//执行调用doPost,因为默认是doGet
}

@Override
public String getServletInfo() {
// TODO Auto-generated method stub
return null;
}

@Override
public void destroy() {
// TODO Auto-generated method stub

}

}

 

<?<web-app version="2.5"
xsi:schemaLocation="http://java.sun.com/ http://java.sun.com/

<servlet>
<servlet-name>ServletDemos</servlet-name>
<servlet-class>cn.itcast.servlet.demo.ServletDemo1</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ServletDemos</servlet-name>
<url-pattern>/servletdemo1</url-pattern>
</servlet-mapping>

</web-app>

 

第二种:继承抽象类形式 GenericServlet

//通过继承GenericServlet类实现 Servlet开发
public class ServletDemo2 extends GenericServlet {
    //必须实现GenericServlet方法
  @Override
  public void service(ServletRequest request, ServletResponse response)
  throws ServletException, IOException {
  response.getWriter().write("hello GenericServlet");
  //直接往浏览器输出
  }

}

<servlet>
<servlet-name>ServletDemo2</servlet-name>
<servlet-class>cn.itcast.servlet.demo.ServletDemo2</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ServletDemo2</servlet-name>
<url-pattern>/servletdemo2</url-pattern>
</servlet-mapping>

 

第三种:继承抽象类 HttpServlet (推荐)

//通过继承HttpServlet 开发Servlet
//最常用的
public class ServletDemo3 extends HttpServlet {
//重写方法
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
//重写方法
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");//设置编码
PrintWriter pw=response.getWriter();
pw.write("第三种继承HttpServlet(推荐)");//输出
}
}

<servlet>
<servlet-name>ServletDemo3</servlet-name>
<servlet-class>cn.itcast.servlet.demo.ServletDemo3</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ServletDemo3</servlet-name>
<url-pattern>/servletdemo3</url-pattern>
</servlet-mapping>