javaWeb学习笔记_servlet使用

servlet主要用负责web程序中的逻辑处理,属于MVC三层结构中的C(control)层,一般来讲一次请求会对应有一个servlet类,eclipse中可以直接创建servlet类

1.新建servlet

2.填写Class名:

3.设定访问路径

4.点击完成后生成如下代码:

@WebServlet("/Upload")
public class Upload extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Upload() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.getWriter().append("Served at: ").append(request.getContextPath());
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}

 然后在doGet()方法中编写代码。

5.session和application用如下代码获得:

ServletContext application = this.getServletContext();
HttpSession session = request.getSession();

 

posted @ 2018-08-29 19:26  annofyf  阅读(171)  评论(0编辑  收藏  举报