zoukankan      html  css  js  c++  java
  • [Jweb] 第一个通过 tomcat 配置,浏览器访问的 web 界面

    http://127.0.0.1:8888/my/index.html


    web.xml 文件最简易的配置 (截图版)

    (文字版)

    <?xml version="1.0" encoding="ISO-8859-1"?>
    <web-app xmlns="http://java.sun.com/xml/ns/javaee"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
       version="2.5"> 
    </web-app>

    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 HelloWorldServlet extends HttpServlet {
    
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp)   // tomcat 帮忙调用这个 doGet这个方法
                throws ServletException, IOException {
            // TODO Auto-generated method stub
            PrintWriter out = resp.getWriter(); // 这个打印流,自动 flush
            out.println("<html><head><title></title></head><body>hello world !!!</body></html>");
            out.flush();
            out.close();
               /*
                * HttpServletRequest req 在这个对象里面封装了客户端到服务器端一切的请求  ,  比方说是 客户端的IP是什么,参数什么的。。。
                  HttpServletResponse resp 服务器端给客户端发的东西,都封装在这,就都能发给客户端啦 */
            
        }
        // 有 tomcat 提供这个 doGet 方法,咱们省事多了,要不还得自己实现 socket...
    }
    

    web.xml 的灵活配置 conf 下的 context.xml 配置自动 reload = true
         <servlet>
          <servlet-name>myfirstservlet</servlet-name>
          <servlet-class>HelloWorldServlet</servlet-class>
        </servlet>
    <servlet-mapping>
            <servlet-name>myfirstservlet</servlet-name>
            <url-pattern>/HelloWorldServlet</url-pattern>    <!--  HelloWorldServlet 前面这个 /  代表 http://127.0.0.1:8888/my/   --> 
        </servlet-mapping>
    <servlet-mapping>
            <servlet-name>myfirstservlet</servlet-name>
            <url-pattern>/ggg/hhh/kkk</url-pattern>
        </servlet-mapping>

        此时在浏览器中访问 : {
           http://127.0.0.1:8888/my/HelloWorldServlet
           http://127.0.0.1:8888/my//ggg/hhh/kkk
        }

    1, 启动tomcat     2, 编写servlet将class文件放入 classes 中  3, 配置 web.xml    4,在浏览器中访问

    package com.bjsxt.test;
    
    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 HelloWorldServlet extends HttpServlet {
    
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    
            resp.setContentType("text/html;charset=gbk");
    
            PrintWriter out = resp.getWriter(); // 打印字符, 扩展  PrintStream 打印字节。  它们优点 : 不会抛出异常,自动 flush.
    
            out.println("<html><head> HEAD—01 <title>Hello Robby_Chan First</title> HEAD-02 </head><body>Body_你好  hello world! 世界 !!!_Body</body></html>");
    
            // out.flush();
    
            out.close();
        }
    }
    

  • 相关阅读:
    java 传入多个参数时报"Parameter 'XXX' not found. Available parameters are [arg1, arg0, param1,..." 解决方案
    java 判断int类型为空
    scp 传输下载
    自己开发的网页在跳转至微信公众号文章后,点击微信的返回,无法返回原网页
    nginx下Thinkphp 隐藏index.php
    ubuntu常见错误–Could not get lock /var/lib/dpkg/lock解决
    apt-get update 和 upgrade 的区别
    php 取某一日期的前一天
    PHP 统计数组中所有的值出现的次数 array_count_values 函数
    pandas之表格样式
  • 原文地址:https://www.cnblogs.com/robbychan/p/3786566.html
Copyright © 2011-2022 走看看