美文网首页JAVA学习过程
servlet常用调用方式

servlet常用调用方式

作者: _String_ | 来源:发表于2017-10-09 11:05 被阅读0次

    servlet常用调用方式实例代码

    <html>
      <head>
        <title>TestHtml.html</title>
        
        <meta name="keywords" content="keyword1,keyword2,keyword3">
        <meta name="description" content="this is my page">
        <meta name="content-type" content="text/html; charset=UTF-8">
        
        <!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
        <script type="text/javascript">
        function test()
        {
            var f = document.forms[0];
            f.action="servlet/helloworod";
            f.submit();
        }
        </script>
        
      </head>
      
      <body>
        <form name="f1" id="f1" action="servlet/helloworod" method="post">
          <table>
            <tr>
              <td>Login:</td>
              <td><input type="text" name="login" id="login"></td>
            </tr>
            <tr>
              <td>Password:</td>
              <td><input type="password" name="password" id="password"></td>
            </tr> 
            <tr>
              <td colspan="2"><input type="submit" value="post submit"></td>
              <td colspan="2"><input type="button" value="script submit" onclick="test();"></td>
            </tr>
            <tr>
              <td colspan="2"><a href="servlet/helloworod">Link...</a></td>
            </tr>
          </table>
        </form>
      </body>
    </html>
    

    测试servlet代码

    package servletStudent;
    
    import java.io.IOException;
    import java.io.PrintWriter;
    
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    public class helloworod extends HttpServlet {
    
        /**
             * Constructor of the object.
             */
        public helloworod() {
            super();
        }
    
        public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    
            response.setContentType("text/html");
            String url = request.getRequestURL().toString();
            
            System.out.println("=============================");
            System.out.println(url);
            PrintWriter out = response.getWriter();
            out.println("=======get========");
            out.println("Hello world!!! ");
    
        }
        public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    
            response.setContentType("text/html");
            String url = request.getRequestURL().toString();
            System.out.println("=============================");
            System.out.println(url);
            PrintWriter out = response.getWriter();
            out.println("=======post========");
            out.println("Hello world!!! ");
            
        }
    
    }
    

    相关文章

      网友评论

        本文标题:servlet常用调用方式

        本文链接:https://www.haomeiwen.com/subject/rvbuyxtx.html