Servlet相关

目录

简介

1、最简单的servlet

2、继承HttpServlet

3、Servlet的生命周期

4、让servlet创建实例的时机提前

5、ServletConfig读取servlet配置中的信息

6、在项目中导入外部jar文件


简介

Servlet(Server Applet)是Java Servlet的简称,称为小服务程序或服务连接器,用Java编写的服务器端程序,主要功能在于交互式地浏览和修改数据,生成动态Web内容。

接收客户端请求及响应客户端。

1、最简单的servlet

在src下面写一个Servlet类实现Servlet接口

package com.zhujunwei.servlet;

import java.io.IOException;

import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

/**
 * 
 * @author Administrator
 * 创建一个Servlet,使该Servlet实现Servlet接口:实现5个抽象方法
 *
 */
public class HelloServlet implements Servlet
{

	@Override
	public void service(ServletRequest arg0, ServletResponse arg1) throws ServletException, IOException
	{
		System.out.println("Hello Servlet...");
	}
	
	@Override
	public void destroy()
	{

	}

	@Override
	public ServletConfig getServletConfig()
	{
		return null;
	}

	@Override
	public String getServletInfo()
	{
		return null;
	}

	@Override
	public void init(ServletConfig arg0) throws ServletException
	{

	}

}

写完java文件后需要在WebContent/WEB-INF/web.xml配置servlet

... 
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  <servlet>
  	<servlet-name>HelloServlet</servlet-name>
  	<servlet-class>com.zhujunwei.servlet.HelloServlet</servlet-class>
  </servlet>
  <servlet-mapping>
  	<servlet-name>HelloServlet</servlet-name>
  	<url-pattern>/HelloServlet</url-pattern>
  </servlet-mapping>
</web-app>

打开服务器,将项目部署到服务器上,在浏览中输入

http://localhost:8080/Hello/HelloServlet

可以在控制台看到

Hello Servlet...

2、继承HttpServlet

继续在src下面创建新的类,这次不是实现Servlet接口,而是继承HttpServlet类(Servlet的子类)

package com.zhujunwei.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
 * 
 * @author Administrator
 * 创建一个Servlet,使该Servlet继承HttpServlet(Servlet接口的子类)
 * 
 * HttpServlet类实现了Servlet的方法
 *
 */
public class HelloServlet02 extends HttpServlet
{
	/*@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
	{
		System.out.println("HelloServlet02----");
	}*/
	private static final long serialVersionUID = 1L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
	{
		System.out.println("doGet...");
	}
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
	{
		System.out.println("doPost...");
	}
}

接着配置web.xml

...
  <servlet>
  	<servlet-name>HelloServlet02</servlet-name>
  	<servlet-class>com.zhujunwei.servlet.HelloServlet02</servlet-class>
  </servlet>
  <servlet-mapping>
  	<servlet-name>HelloServlet02</servlet-name>
  	<url-pattern>/HelloServlet02</url-pattern>
  </servlet-mapping>
</web-app>

重启服务器,在地址栏输入

http://localhost:8080/Hello/HelloServlet02

可以在控制台看到

doGet...

3、Servlet的生命周期

Servlet只会在第一次调用时创建,调用init()方法(就像类的构造器一样),而在以后的调用中只会调用service方法。

destroy()方法只会在服务器关闭或者项目从服务器中移除的时候调用。

实例演示:

创建Servlet类

package com.zhujunwei.servlet;

import java.io.IOException;

import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
/**
 * 
 * @author Administrator
 * 演示生命活动的生命周期
 *
 */
public class HelloServlet03 implements Servlet
{

	@Override
	public void destroy()
	{
		System.out.println("Servlet destroy()....");
	}

	@Override
	public ServletConfig getServletConfig()
	{
		return null;
	}

	@Override
	public String getServletInfo()
	{
		return null;
	}

	@Override
	public void init(ServletConfig config) throws ServletException
	{
		System.out.println("servlet init()....");
		
	}

	@Override
	public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException
	{
		System.out.println("servlet service()....");
	}

}

多次运行结果

servlet init()....
servlet service()....
servlet service()....
servlet service()....
servlet service()....

4、让servlet创建实例的时机提前

为了让servlet创建的时间提前,我们需要额外配置web.xml

首先创建servlet

package com.zhujunwei.servlet;

import java.io.IOException;

import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
/**
 * 让servlet创建实例的时机提前
 * @author Administrator
 *
 */
public class HelloServlet04 implements Servlet
{

	@Override
	public void destroy()
	{
		System.out.println("Servlet04 destroy()....");
	}

	@Override
	public ServletConfig getServletConfig()
	{
		return null;
	}

	@Override
	public String getServletInfo()
	{
		return null;
	}

	@Override
	public void init(ServletConfig config) throws ServletException
	{
		System.out.println("servlet04 init()....");
		
	}

	@Override
	public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException
	{
		System.out.println("servlet04 service()....");
	}

}

配置web.xml文件需要额外配置load-on-startup的值为2,1已经被tomcat使用了

<load-on-startup>2</load-on-startup>
...
  <servlet>
  	<servlet-name>HelloServlet04</servlet-name>
  	<servlet-class>com.zhujunwei.servlet.HelloServlet04</servlet-class>
  	<load-on-startup>2</load-on-startup>
  </servlet>
  <servlet-mapping>
  	<servlet-name>HelloServlet04</servlet-name>
  	<url-pattern>/HelloServlet04</url-pattern>
  </servlet-mapping>
</web-app>

这样项目在一启动的时候就会创建该Servlet

....
十二月 25, 2018 12:01:20 下午 org.apache.catalina.core.StandardEngine startInternal
信息: Starting Servlet Engine: Apache Tomcat/9.0.8
servlet04 init()....
十二月 25, 2018 12:01:21 下午 org.apache.catalina.startup.HostConfig deployDirectory
信息: Deploying web application directory [E:\apache\Tomcat 9.0\webapps\docs]
十二月 25, 2018 12:01:21 下午 org.apache.catalina.startup.HostConfig deployDirectory
信息: Deployment of web application directory [E:\apache\Tomcat 9.0\webapps\docs] has finished in [31] ms
十二月 25, 2018 12:01:21 下午 org.apache.catalina.startup.HostConfig deployDirectory
...

5、ServletConfig读取servlet配置中的信息

我们可以在servlet配置中添加一些参数,ServletConfig可以读取这些参数

servlet类的编写

package com.zhujunwei.servlet;

import java.io.IOException;
import java.util.Enumeration;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class HelloServletConfig extends HttpServlet{

	private static final long serialVersionUID = 1L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
	
		//ServletConfig  可以获取servlet在配置的一些信息
		
		//1. 得到servlet配置对象 专门用于在配置servlet的信息
		ServletConfig config = getServletConfig();
		
		//获取到的是配置servlet里面servlet-name 的文本内容
		String servletName = config.getServletName();
		System.out.println("servletName="+servletName);
		
		
		//2、。 可以获取具体的某一个参数。 
		String address = config.getInitParameter("address");
		System.out.println("address="+address);
		
		System.out.println("----------------------------------");
		//3.获取所有的参数名称
		Enumeration<String> names = config.getInitParameterNames();
		//遍历取出所有的参数名称
		while (names.hasMoreElements()) {
			String key = (String) names.nextElement();
			String value = config.getInitParameter(key);
			System.out.println("key==="+key + "   value="+value);
		}
	}
	
	//来了post请求,就让它去执行doGet方法
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doGet(req, resp);
	}
}

web.xml中的配置

...
 <servlet>
  	<servlet-name>HelloServletConfig</servlet-name>
  	<servlet-class>com.zhujunwei.servlet.HelloServletConfig</servlet-class>
  	<!-- 可以添加初始化参数 -->
  		<init-param>
  			<param-name>name</param-name>
  			<param-value>zhangsan</param-value>
  		</init-param>
  		<init-param>
  			<param-name>age</param-name>
  			<param-value>18</param-value>
  		</init-param>
  		<init-param>
  			<param-name>address</param-name>
  			<param-value>bejing...</param-value>
  		</init-param>
  </servlet>
  <servlet-mapping>
  	<servlet-name>HelloServletConfig</servlet-name>
  	<url-pattern>/HelloServletConfig</url-pattern>
  </servlet-mapping>
</web-app>

运行后得到参数的

servletName=HelloServletConfig
address=bejing...
----------------------------------
key===address   value=bejing...
key===name   value=zhangsan
key===age   value=18

6、在项目中导入外部jar文件

在WebContent/WEB-INF下创建lib目录,将需要的jar文件直接拷贝进去即可。

 

posted @ 2018-12-25 12:10  雨中遐想  阅读(4)  评论(0编辑  收藏  举报  来源