Servlet概述
1. Servlet(Server Applet)是Java Servlet的简称,称为小服务程序或服务连接器,用Java编写的服务器端程序,具有独立于平台和协议的特性,主要功能在于交互式地浏览和生成数据,生成动态Web内容等。
2.Servlet是和平台无关的服务器端组件,它运行在Servlet容器中(如Tomcat,weblogic等)。Servlet容器负责Servlet和客户的通讯以调用Servlet的相关方法,并且Servlet和客户端采用的是"请求/响应"的模式。
3.Servlet作为javaWeb开发的基石,具有比较重要的意义。作为来自 Web 浏览器或其他 HTTP 客户端的请求和 HTTP 服务器上的数据库或应用程序之间的中间层。可以作为数据库(db)以及页面显示(html,jsp)之间数据传输的中间件(控制层)。
Servlet在web应用程序中间的位置
Servlet能够实现的功能:
1.获取客户端发送的数据内容,包括表单、链接参数、 cookies、媒体类型和浏览器能理解的压缩格式等
2.处理相关的 数据并生成结果,创建能够嵌入页面中的部分html代码片段。(实现动态的数据返回)
3.与其他的服务器资源通信,如数据库等
编写第一个Servlet应用程序
1.环境要求
由于Servlet是运行在服务器端的组件,所以想要使用它,需要先配置好我们的容器,也就是Tomcat,我们选用Myeclipse2017作为开发工具,并配置Tomcat8.0(尽管Myeclipse自带有Tomcat,但是建议还是自己下载一个Tomcat,配置好以后在使用,方便调整)。
2.创建web项目
项目位置空白处右键——>new——>web project——>填写相关的内容,如下,然后finish即可
完成以后该web Project项目的结构如下:
3.编写第一个Servlet应用程序(HelloWorld)
(1).方法1:通过开发工具直接创建一个Servlet
首先在src下面创建一个名为com.cn.test的包,并右键该包,new一个Servlet(如果没有Servlet,可以点击other,然后再搜索栏中输入"servlet"查找即可),效果如下
填写完该Servlet名称以后点击next,然后finish即可,其中如果需要配置其他内容,自己选填即可。这一个时候开发工具就默认给我们创建好了一个Servlet代码,效果如下:
代码部分:
配置文件部分:
(2).方法2:先编写一个一般的java类,然后该类和Servlet建立关系。
右键选中包com.cn.test,然后new一个class,效果如下:
然后,开发工具就给我们创建了一个名称为HelloWorld1的java类,然后实现Servlet接口。代码如下:
配置访问该Servlet的映射等信息:
4.配置编写的Servlet
(1).方法1:在web.xml文件中配置Servlet(旧版本的配置,较为麻烦)
<!--只给出必要部分代码-->
<!--配置-->
<servlet>
<!--servlet注册的名字-->
<servlet-name>HelloWorld</servlet-name>
<!--servlet全类名-->
<servlet-class>com.cn.test.HelloWorld</servlet-class>
</servlet>
<!--映射-->
<servlet-mapping>
<!--servlet需要和某一个Servlet节点名称一样-->
<servlet-name>HelloWorld</servlet-name>
<!--"/表示的是项目的根目录(http://localhost:8080/ServletTest/),这一个就是访问该Servlet的时候需要的url"-->
<url-pattern>/HelloWorld</url-pattern>
</servlet-mapping>
(2).方法2:通过注解的方式配置Servlet(Servlet3.0以后支持注解)
@WebServlet(name="HelloWorld1",urlPatterns="/HelloWorld1")
public class HelloWorld1 implements Servlet{
//...其他代码
}
5.运行并访问该Servlet
(1).运行
右键项目——>run as ——>Myeclipse Server Application——>选择你需要使用的服务器——>finish
即可在console窗口看到运行的部署的情况:
同时可以查看由哪一些项目是正在运行在Tomcat服务器上的.
注意:在部署的时候需要确保你的Tomcat服务器是处于停止状态,不然会报出错误。
(2).访问
直接在浏览器或者myeclipse的web browser中输入地址:
访问HelloWorld这一个Servlet:http://localhost:8080/ServletTest/HelloWorld
访问HelloWorld1这一个Servlet:http://localhost:8080/ServletTest/HelloWorld1
控制台输出我们在每一个Servlet里面编写的输出内容:
测试使用的俩个Servlet较为完整代码如下:
HelloWorld.java
public class HelloWorld extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* Constructor of the object.
*/
public HelloWorld() {
System.out.println("HellWorld service");
}
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("HellWorld service");
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
// Put your code here
System.out.println("HellWorld service");
}
}
HelloWord1.java
@WebServlet(name="HelloWorld1",urlPatterns="/HelloWorld1")
public class HelloWorld1 implements Servlet{
@Override
public void destroy() {
// TODO Auto-generated method stub
System.out.println("HellWorld1 destory");
}
@Override
public ServletConfig getServletConfig() {
// TODO Auto-generated method stub
return null;
}
@Override
public String getServletInfo() {
// TODO Auto-generated method stub
return null;
}
@Override
public void init(ServletConfig arg0) throws ServletException {
// TODO Auto-generated method stub
System.out.println("HellWorld1 init");
}
@Override
public void service(ServletRequest arg0, ServletResponse arg1) throws ServletException, IOException {
// TODO Auto-generated method stub
System.out.println("HellWorld1 service");
}
}