tomcat和servlet

  1. 网页开发技术

1.静态网页开发技术   html

2.动态网页开发技术   servlet/jsp

  1. 服务器
    1. 什么是服务器

性能强大计算机(硬件)

操作系统(windows/linux)

服务器软件(部署资源供外部访问)

web应用程序(处理浏览器端请求)

    1. 服务器软件

IIS 服务器: microsoft大型服务软件

weblogic: oracle服务器软件(支持13种动态网页开发技术)

websphere: IBM 大型服务器软件

apache: apache开源软件基金会, 开源

nginx: web服务器/反向代理服务器(分布式)

tomcat: apache下项目,开源免费web应用服务器

    1. tomcat

tomcat开源免费web服务器,主要由apache,sun以及其他进行开发和维护。

tomcat需要jdk环境,servlet,jsp最新技术都是支持。

    1. 下载和安装
      1. 下载: www.apache.org

http://archive.apache.org/dist/

      1. 安装

1.解压非中文路径下

2.目录

         bin:可执行文件(启动,关闭tomcat)

         conf:配置文件

         lib: 依赖jar

         logs:tomcat日志信息

temp:临时文件

webapps:web应用程序的存放目录

work:工作目录(jsp文件)

    1. 启动测试

http://localhost:8080

C:\Windows\System32\drivers\etc\hosts

  

<Connector port="8080" protocol="HTTP/1.1"

               connectionTimeout="20000"

     redirectPort="8443" />

 

  1. web应用程序
  1. eclipse集成tomcat
    1. 配置server 环境
    2. 创建tomcat服务器
    3. 配置tomcat的目录和应用程序目录
  2. 开发web应用程序
  3. webcontent:资源文件根目录
    1. WEB-INF:该目录下所有资源不能被外部直接访问
      1. lib:依赖jar
      2. web.xml:应用程序配置文件
  1. servlet

运行于服务器端应用程序,必须实现servlet接口。

接收和处理用户请求(http);对客户端进行响应(html)。

  1. 开发servlet

      创建类,实现servlet接口(HttpServlet)

  1. 配置servlet(servlet请求路径)

  web.xml

  1. 测试
  1. HttpservletRequest
  1. 请求对象(请求行 请求头 请求体

    //获取请求参数

       String username = req.getParameter("username");

       String password = req.getParameter("password");

       //获取其他信息

       String method = req.getMethod();  //请求方式

       String remoteAddr = req.getRemoteAddr(); //客户端的ip

       int remotePort = req.getRemotePort();  //客户端端口

 

  1. 域(实现数据共享)

request域: 

作用域: 一次请求内有效

setAttribute(name,value)  设置

getAttribute(name)   获取

removeAttribute(name) 删除

  1. HttpServletResponse

PrintWriter writer = resp.getWriter();

writer.println("success");

writer.close();

1.页面内容较多时,使用printwriter不方便?   jsp

2.乱码问题

       原因: 编码不一致(utf-8)

       请求:

       get请求没有乱码: 8.0+ tomcat默认编码改为utf-8(tomcat7àLatin1)

                    

<Connector connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443" URIEncoding="utf-8"/>

 

              post请求乱码:

req.setCharacterEncoding("utf-8");

       响应编码:

                    

      //修改响应编码

      resp.setCharacterEncoding("utf-8");

      //设置数据的类型(响应头)

      resp.setContentType("text/html;charset=utf-8");

  1. jsp

jsp形式: html+java

本质:  servlet

 

 

 

 

 

 

猜你喜欢

转载自blog.csdn.net/qq_42676052/article/details/82532335