码迷,mamicode.com
首页 > 其他好文 > 详细

编写一个小Servlet程序

时间:2016-07-02 20:13:55      阅读:172      评论:0      收藏:0      [点我收藏+]

标签:

 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>

 

编写一个小Servlet程序

标签:

原文地址:http://www.cnblogs.com/huiziz/p/5635959.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!