zoukankan      html  css  js  c++  java
  • 怎么在一个servlet中实现多个功能 ?如何使一个Servlet处理多个请求?

    自学javaweb一直不知道一个servelt可以有多个功能!看了别人代码才知道这个可以有!

    平时你建立servelt时候你会吧doget和dopost这两个勾上,要想实现多个功能,你不必要勾选doget和dopost方法只勾选service即可!此时你复写service方法就行!

    你不写doget和dopost,写service  ,这个servelt被调用会先执行service  ,即使你同时写了post和get和service调用这个servelt也会先调用service方法!service不管何时就会被先调用!

    下面是一个简单的例子(简单的过分)!!!!

    就是一个登陆和注册用同一个servelt怎么实现?

    大白话 意思就是jsp页面在写的时候传入一个值在servelt页面根据传入的值的不同调用不同的方法,selvelt 里面写所有你想写的方法!!!不清楚看代码!!!小例子别指望直接运行

     

     

    先说jsp代码:

    这里面有一些ajax代码,不用关注这些细节,意思就是登陆使用了ajax发送到servelt中的(当然只要你能传值过去就行,不管用什么),注册按钮使用了form表单提交,其实思路很简单就是这两个按钮被点击跳转页面时分别传入了一个让服务器知道调用哪个方法的参数,这个你随便定义你传1或2都行,传过去你要分的清楚。
    <html>
    <head>
    <script type="text/javascript" src="${pageContext.request.contextPath}/scripts/jquery-3.3.1.js"></script>
    <script type="text/javascript">
        function tiJiao(){
            var username = $("#username").val();//获取登录的名字
            var password = $("#password").val();//获取登陆的密码
            if(username == null || username.length == 0 || password == null || password.length == 0 ){
                alert("填写不完整");//判断是不是账号密码为空!
                return false;}
        
        var url="${pageContext.request.contextPath}/all";//这个地址是你要判断用户是否存在的后台
        var args={"method":"login","username":username,"password":password,"time":new Date()};//这个参数是把编辑框里的内容传过去给后台了
        $.post(url,args,function(data){$("#message").html(data);});//登录按钮被点击使用ajax传值到后台传值login
        }
    </script>
    </head>
    <body>
    <form action="${pageContext.request.contextPath}/all?method=zhuce" method="post">//注册按钮被点击使用form表单提交传值zhuce
     <div align="center"> 账号:<input type="text" id="username" name="username" style="200px; height:25px;" ><label id="message"></label></div><br>
    <div align="center">密码:<input type="password" id="password" name="password" style="200px; height:25px;"></div>&nbsp;
    <div align="center"><input type="button" value="登陆" onclick=" tiJiao()" style="70px; height:30px;" />
    <input type="submit" value="注册"  style="70px; height:30px;" />
    </div> 
    </form>
    </body>
    </html>
    servelt代码...................................................................................................................................................................................................

    protected
    void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub String method=request.getParameter("method");//得到传入的值下面根据传入的值执行不同的方法!! System.out.println("method"+method); if(method.equals("login")) { login(request, response);//执行login代码 } else { zhuce(request, response);//执行注册代码 } } private void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //这里写有关登录的代码 } private void zhuce(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //这里写有关注册的代码 } }

     

  • 相关阅读:
    Linux 内核中的 Device Mapper 机制
    阿里云 Angular 2 UI框架 NG-ZORRO介绍
    Docker容器 暴露多个端口
    修改docker容器的端口映射
    Ubuntu Docker安装
    Docker容器技术的PaaS云平台架构设计***
    scala 学习笔记三 闭包
    scala 学习笔记二 方法与函数
    scala 学习笔记一 列表List
    Python3 写Windows Service服务程序
  • 原文地址:https://www.cnblogs.com/xuexidememeda/p/8343428.html
Copyright © 2011-2022 走看看