标签:
1、编写一个java类,此类继承HttpServlet
继承:右击鼠标,source-override/Implement Methods,选择 HttpServlet
2、重写doGet()和doPost()方法
//继承于HttpServlet
public class HelloServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
System.out.println("处理Get()请求");
PrintWriter out = response.getWriter();
response.setContentType("text/html;charset=utf-8");
out.println("<strong>Hello Servletgfdgdfgd</strong><br>");
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
System.out.println("处理Post()请求");
PrintWriter out = response.getWriter();
response.setContentType("text/html;charset=utf-8");
out.println("<strong>Hello Servletgfdgdfgd</strong>");
}
}
3、在web.xml中注册Servlet,定义两个标签
<servlet>
<servlet-name>HelloServlet</servlet-name>
<servlet-class>servlet.HelloServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HelloServlet</servlet-name>
<url-pattern>/servlet/HelloServlet</url-pattern>
</servlet-mapping>
4、index.jsp中 设置使用get()方式的 超链接,post()方式的 提交按钮
<a href="servlet/HelloServlet">Get方式请求HelloServlet</a>
<form action="servlet/HelloServlet" method="post">
<input type="submit" value="Post方式请求HelloServlet123123"/>
</form>
标签:
原文地址:http://www.cnblogs.com/huiziz/p/5635959.html