一、servlet
1、自己写一个serlvet程序
- 新建day04\WEB-INF\classes目录(D:\apache-tomcat-7.0.54\webapps\day04\WEB-INF\classes)
- 在classes目录中新建一个FirstServlet.java
//在服务器写程序,这个程序一定要有包名。这个程序的权限一定要是public package cn.itcast; import javax.servlet.*; import java.io.OutputStream; public class FirstServlet extends GenericServlet { public void service(ServletRequest req, ServletResponse res) throws ServletException, java.io.IOException { OutputStream out = res.getOutputStream(); out.write("Hello servlet!".getBytes()); } } cmd: >set classpath=%classpath%;D:\apache-tomcat-7.0.54\lib\servlet-api.jar >javac -d . Firstservlet.java- 在WEB-INF目录中新建一个web.xml文件,配置servlet对外访问路径(抄头抄屁股,抄tomcat里面的web.xml)
<?xml version="1.0" encoding="ISO-8859-1"?> <!-- Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. --> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"> <servlet> <servlet-name>FirstServlet</servlet-name> <servlet-class>cn.itcast.FirstServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>FirstServlet</servlet-name> <url-pattern>/FirstServlet</url-pattern> </servlet-mapping> </web-app> - 启动tomcat,访问。结果如图:
2、servlet的调用过程和生命周期
- 连上web服务器
- 发送http请求(用谷歌浏览器可以查看)
GET /day04/FirstServlet HTTP/1.1 Host: localhost:8080 Connection: keep-alive Accept-Encoding: gzip,deflate,sdch Accept-Language: zh-CN,zh;q=0.8 - 解析出想访问的主机名
- 解析出想访问的web应用
- 解析出想访问的web资源
- 只有第一次访问才创建servlet实例对象
- 调用servlet的init方法完成对象初始化
- 创建代表请求的request和代表响应的response,然后调用servlet的service方法响应客户端的请求
- service方法执行,向代表客户端响应的respon对象写入了向客户机输出的数据
OutputStream out = res.getOutputStream(); out.write("Hello servlet!".getBytes()); - service方法返回
- 服务器从respons中取出数据,构建出一个http响应,回写给客服机
- 回写http响应
HTTP/1.1 200 OK Server: Apache-Coyote/1.1 Content-Length: 14 Date: Sat, 16 Aug 2014 02:05:43 GMT Hello servlet! - 浏览器解析http响应,提取数据显示
3、使用eclipse开发servlet
4、HttpServlet
- 编写servlet时,通常httpservlet,尽量避免继承servlet
- 复写了service方法(eclipse查看源码快捷键:Ctrl + Shift + T)
- myeclipse:新建servlet,web.xml也自动配好了。虽然在eclipse中,web.xml里面没看到新建servlet的映射,但是为什么在浏览器中依然可以看到结果呀:因为eclipse中的ServletDemo2.java中的这句@WebServlet("/ServletDemo2")就代表了web.xml中的映射。
- 修改Servlet.java模版,没找到?
- doGet()和doPost()有什么区别?同时覆盖这两个方法的时候,为什么会调用doGet()而不是doPost()呢?:
提交方式不一样。默认是get。
表单提交的时候,注明post,就是post方式提交了,<form action="" method="post" ></form>
5、线程安全问题
- 线程安全问题:同步synchronized(不好) --> 实现SingleThreadModel接口(创建新的线程,不好)
6、servlet对象-ServletConfig
- servletConfig对象,用于封装servlet的配置信息
- web.xml: (给servlet配初始化参数)。
<init-param> <param-name>date</param-name> <param-value>xxxx</param-value> </init-param>
- 实际开发中,有一些东西不适合在servlet中写死,这类数据就可以通过配置方式配给servlet,例如servlet采用哪个码表、servlet连接哪个库、servlet哪个配置文件
7、servlet对象-SerletContext
//得到servletContext的方式1 ServletContext context = this.getServletConfig().getServletContext(); //得到servletContext的方式2 context = this.getServletContext();- 也称为context域对象(web开发中还有request域、session域、page域)
- servletContext域:这是一个容器;说明了这个容器的作用范围,也就是应用程序范围
- 共享数据
- 可以用于聊天室
- 通过servletContext实现请求转发到1.jsp(不能通过context域,要通过request域 什么意思呀?)
- 利用servletContext对象读取资源文件:
- 配置的数据没有关系,用properties;配置文件与数据有关系的,用xml(不知道是不是这么拼的)文件 什么叫有没有关系呀?
本文详细介绍了如何创建和配置Servlet程序,包括新建Servlet类、编写服务方法、配置web.xml文件以实现对外访问,以及如何在Eclipse环境中开发Servlet。此外,还探讨了Servlet的生命周期、与HTTP请求的交互过程,并解释了如何使用Eclipse简化开发流程。最后,文章涵盖了Servlet对象与相关组件(如ServletConfig、ServletContext)的交互,以及如何在实际项目中运用这些概念。
4921

被折叠的 条评论
为什么被折叠?



