• servlet3.0 JQuary Ajax基本使用


    servlet3.0 没有web.xml文件,需要使用注解进行配置。

    js:

    $(document).ready(function(){
    	$("#btn").click(function(){
    		$.ajax({
    			type:'post',
    			url:'/testAjax/testAjax1',
    			data:{'name':'postmethod'},
    			success: function (data) {
                           alert(data);
                },
    		});
    	})
    });    
    

     java代码如下:

    // @WebServlet(name="testAjax5",value="/testAjax1")
    @WebServlet(name="testAjax111111",value="/testAjax1")
    // @WebServlet("/testAjax1")
    public class testAjax extends HttpServlet {
    
        private static final long serialVersionUID = 6801951545229974083L;
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp)
                throws ServletException, IOException {
            resp.setCharacterEncoding("utf-8");
            String data = req.getParameter("name");
            resp.setCharacterEncoding("utf-8");
            resp.getWriter().write("get " + data);
            
        }
        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp)
                throws ServletException, IOException {
            String data = req.getParameter("name");
            resp.setCharacterEncoding("utf-8");
            resp.getWriter().write("post " + data);
        }
    }
    @WebServlet 注解比较关键,在测试时不知什么原因
    @WebServlet(name="AnnotationServlet",urlPatterns="/AnnotationServlet")

    该方式会失败。

    最后,注意,每次更改注解时,需要重启tomcat。

  • 相关阅读:
    Key and Certificate Conversion
    openssl
    python http通信实现
    鼠标右键添加cmd
    好文章
    wireshark里无网络接口解决办法
    python垃圾回收
    终于有人把 Docker 讲清楚了
    mongodb的监控与性能优化
    mongodb创建超级用户和普通用户(对应数据库的用户)
  • 原文地址:https://www.cnblogs.com/LessNull/p/4273596.html
Copyright © 2020-2023  润新知