当前位置: 首页>编程语言>正文

Servlet(一)——Servlet开发

目录

1.Servlet引入

2.简单的Servlet程序

3.Servlet生命周期


1.Servlet引入

百度百科:Servlet简介

主要功能在于交互式地浏览和生成数据,生成动态Web内容;

从原理上讲,Servlet可以响应任何类型的请求,但绝大多数情况下Servlet只用来扩展基于HTTP协议的Web服务器。

 

2.简单的Servlet程序

先建立一个HelloWorldServlet类:

这个类继承HttpServelt,且alt+shift+s里面的重写方法选中doGet()和doPost();

package com.java.web;

import java.io.IOException;

import javax.net.ssl.HttpsURLConnection;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class HelloWorldServlet extends HttpServlet{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//用户请求doGet时交给doPost去处理
		this.doPost(request, response);
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
	}

	
}

这里还要将配置文件web.xml里面修改一下:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>HelloWorldServlet</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>
     
  <servlet>
  	<servlet-name>HelloWorldServlet</servlet-name>
  	<servlet-class>com.java.web.HelloWorldServlet</servlet-class>
  </servlet>
  
  <servlet-mapping>
  	<servlet-name>HelloWorldServlet</servlet-name>
  	<url-pattern>HelloWorld</url-pattern>
  </servlet-mapping>
</web-app>

这时再编辑原先类里面的代码:

package com.java.web;

import java.io.IOException;
import java.io.PrintWriter;

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

public class HelloWorldServlet extends HttpServlet{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//用户请求doGet时交给doPost去处理
		this.doPost(request, response);
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//设置默认编码为gbk
		response.setCharacterEncoding("gbk");
		PrintWriter out=response.getWriter();
		out.println("<html>");
		out.println("<head><title>简单的Servlet程序</title></head>");
		out.println("HelloWorld!你好!");
		out.println("</html>");
		out.close();
	}

	
}

我们在浏览器运行时其实是运行HelloWorld:

http://localhost:8080/HelloWorldServlet/HelloWorld

运行结果:

Servlet(一)——Servlet开发,Servlet(一)——Servlet开发_JavaScript,第1张

原理:首先浏览器请求HelloWorld,然后找到匹配的name(即HelloWorldServlet);然后name去找Servlet里面相匹配的name,请求其中的class,然后根据返回的get请求,找到doPost()方法,运行其中的代码就完成了!

 

3.Servlet生命周期

Servlet 的生命周期,简单的概括这就分为四步:servlet 类加载--->实例化--->服务--->销毁。

我们建一个测试类LifeServlet:

package com.java.web;

import java.io.IOException;

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

public class LifeServlet extends HttpServlet{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		System.out.println("service");
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		System.out.println("service");
	}

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

	@Override
	public void init() throws ServletException {
		System.out.println("Servlet初始化");
	}
	
}

当然在web.xml里面也要添加配置:

Servlet(一)——Servlet开发,Servlet(一)——Servlet开发_Servlet生命周期_02,第2张

运行http://localhost:8080/HelloWorldServlet/Life

Servlet(一)——Servlet开发,Servlet(一)——Servlet开发_Servlet开发_03,第3张

可以看到Servlet的生命周期分为四步:

1.调用LifeServlet时会进行类加载;

2.Servlet初始化时会进行实例化,并调用init()方法;

3.请求要求时会有服务service;

4.长时间不用或者关闭LifeServlet就会销毁Servlet!并调用destroy()方法;

 

 

 


https://www.xamrdz.com/lan/53x1924498.html

相关文章: