Servlet_1th_第一个Servlet程序

来源:互联网 发布:苹果mac管理员密码忘记 编辑:程序博客网 时间:2024/04/30 20:23
一、Servlet简介
1)Servlet是在Web服务器上的一个Java应用程序,当用户访问Web服务器的时候,它为用户提供服务(为用户传递一个html文档)。
2)用来完成B/S架构下,客户端请求的响应处理。
3)平台独立,性能优良,能以线程方式运行。
4)Servlet API为Servlet提供了统一的编程接口。
5)Servlet一般在容器中运行。
6)常见的Servlet容器:
Tomcat是一个Web Server,也是一个Servlet容器。


二、Servlet运行过程

1)浏览器发送请求到服务器
2)服务器根据请求的url,去调用相应的servlet类。
3)通过servlet中的打印流对象将生成的HTML数据输出给服务器。
4)服务器将servlet生成的数据再输出给客户端浏览器。

三、Servlet程序演示效果:

1、建立web项目
最好切换到Package Explorer的视图,然后新建一个Dynamic Web Project,名字随便起,这里起为first。

2、建包
在建好的Web项目的src目录下建包com.hpe.servlet,在该包下新建一个Java文件:FirstServlet.java。
写下如下代码:

package com.hpe.servlet;import java.io.IOException;import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;public class FirstServlet extends HttpServlet {@Overrideprotected void service(HttpServletRequest arg0, HttpServletResponse arg1) throws ServletException, IOException {System.out.println("我的第一个Servlet程序运行啦!");}}


3、配置servlet
1)为什么要配置?
服务器根据客户端请求的url去调用相应的servlet类。虽然在第2步中已经写好了一个servlet类,但服务器并不知道这个类的存在,所以配置是为了将这个servlet类告诉服务器。

2)配置过程
①在WEB-INF目录下,有一个web.xml,这是servlet程序的配置文件,通过这个文件服务器就能找到servlet程序。
内容如下所示:
<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">  <display-name>first</display-name>  <welcome-file-list>    <welcome-file>index.html</welcome-file>    <welcome-file>index.htm</welcome-file>    <welcome-file>index.jsp</welcome-file>    <welcome-file>default.html</welcome-file>    <welcome-file>default.htm</welcome-file>    <welcome-file>default.jsp</welcome-file>  </welcome-file-list></web-app>

②将其中的内容进行删减如下:
<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">  </web-app>

③接下来要配置一个servlet,让服务器知道我们写的这个servlet类
<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">    <!-- 1、配置某一个servlet程序(让服务器知道servlet程序的位置) -->  <servlet>  <servlet-name>one</servlet-name><!-- 这是servlet程序在本配置文件中的名字,便于在本配置文件中的其它地方引用, -->  <servlet-class>com.hpe.servlet.FirstServlet</servlet-class><!-- 类名,一定要加上包名 -->  </servlet>  </web-app>

④让服务器知道哪个url是对应这个servlet程序的:
<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">    <!-- 1、配置某一个servlet程序(让服务器知道servlet程序的位置) -->  <servlet>  <servlet-name>one</servlet-name><!-- 这是servlet程序在本配置文件中的名字,便于在本配置文件中的其它地方引用, -->  <servlet-class>com.hpe.servlet.FirstServlet</servlet-class><!-- 类名,一定要加上包名 -->  </servlet>    <!-- 2、指定url,让服务器知道该servlet程序与哪一个url匹配 -->  <servlet-mapping>  <servlet-name>one</servlet-name>  <url-pattern>/111</url-pattern><!-- 一个servlet程序可以对应多个url -->  <url-pattern>/222</url-pattern>  <url-pattern>/oneoneone</url-pattern>  </servlet-mapping>  </web-app>

客户端只要发一个"/111"请求,服务器就知道找one这个servlet,然后就能找到这个名字对应的类。

注意:一个web.xml文件中可以写多个<servlet></servlet>标签,即可以配置多个servlet类。

配置过程完成。


4、部署Servlet程序

第一种:命令行启动Tomcat服务器方式。

第二种:eclipse中启动Tomcat服务器。

下面先介绍第一种:

1)命令行启动Tomcat服务器方式:

①将Web项目

在C:\apache-tomcat-8.0.17\webapps\目录下新建一个文件夹test用于存放servlet程序和web.xml配置文件。

将eclispe中Package Explorer视图下的META-INF和WEB-INF文件拷贝到C:\apache-tomcat-8.0.17\webapps\test目录下;

将eclispe中Package Explorer视图下的build文件下classes文件拷贝到C:\apache-tomcat-8.0.17\webapps\test\WEB-INF目录下。

然后启动Tomcat服务器:


如下启动完成:


可以看到服务器端口号变成了8888,默认是8080,这里已经在C:\apache-tomcat-8.0.17\conf中的server.xml文件中修改了端口号。

在浏览器中输入如下url:


当服务器会根据url,在C:\apache-tomcat-8.0.17\webapps\test\WEB-INF\web.xml中去找名字为"111"的servlet程序,并调用其servlet类中的service方法,所以在控制台会打印如下信息:


每刷新一次页面,都会向服务器发送请求,服务器就会根据url调用servlet程序从而打印这句话。

此外,在浏览器输入http://localhost:8888/test/222、http://localhost:8888/test/oneoneone同样会调用到servlet程序,这是因为在web.xml的<servlet-mapping>标签中给名字为"one"的servlet程序指定了<url-pattern>/111</url-pattern>、<url-pattern>/222</url-pattern>和<url-pattern>/oneoneone</url-pattern>。

总结:

①同一个servlet程序可以对应多个url地址

②<url-pattern>/111</url-pattern>中的"/"是相对于webapps的根路径而言的,

③要写一个Servlet程序,需要继承Servlet接口,而Servlet有3个已知的实现类FacesServlet、GenericServlet、HttpServlet。但我们是要写一个在HTTP服务器端运行的Servlet程序,所以没必要直接实现Servlet接口,可以从其实现类HttpServlet继承即可。


下面将介绍在eclipse中启动Tomcat服务器的方式,可以对之前的FirstServer.java做一些改动,便于观察效果:

package com.hpe.servlet;import java.io.IOException;import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;public class FirstServlet extends HttpServlet {private static int count = 0; @Overrideprotected void service(HttpServletRequest arg0, HttpServletResponse arg1) throws ServletException, IOException {//System.out.println("我的第一个Servlet程序运行啦!");count++;System.out.println("我的第一个Servlet程序运行第" + count +"次啦!");}}


第二种:从eclipse中启动Tomcat服务器:

在前一篇博文的基础上,将Web项目first添加到Tomcat服务器,然后启动:


服务器启动完成,我们使用的依然是8888端口:



右键Run As — Run On Server,运行servlet程序,或直接点击运行按钮,运行效果如下:


多次刷新页面:


这是在eclipse中将web项目发布到Tomcat服务器中,然后启动Tomcat服务器,手动运行servlet的例子。






1 0
原创粉丝点击