day31 1 tomcat介绍与创建web项目 & 2 继承HttpServlet类、配置webxml全局配置文件 & 3 servlet生命周期 & 4 请求对象HttpServletRequest与响应对象HttpServletResponse

Servlet

Java Servlet是运行在Web服务器或应用服务器上的程序,作为客户端(Web浏览器或其他HTTP客户端)和服务端(HTTP服务器上的数据库或应用程序)之间的中间层。
使用Servlet可以接收来自网页表单的用户输入数据,呈现(返回给客户端)来自数据库或其他源的数据,还可以动态创建网页image

Servlet主要任务

1)读取客户端发送的显式(GET)数据。
2)读取客户端发送的隐式(POST)HTTP请求数据。
3)处理数据并产生结果
4)发送显式的数据(文档)到客户端
5)发送隐式的HTTP响应到客户端

Java Servlet可以使用 javax.servlet和javax.servlet.http包创建,它是java企业版(JavaEE)的标准组成部分

3 Tomcat

Tomcat是一个免费的开源的Web应用服务器,属于轻量级应用服务器,在中小型系统并发访问低的场合下被普遍使用,是开发和调试web应用程序的首选
它和IIS等web服务器一样,具有处理HTML页面的功能,另外它还是一个Servlet和jsp容器,独立的Servlet容器是Tomcat的默认模式。

容器

一个容器包含完整的运行时的环境:除了应用程序本身之外,这个应用所需的全部依赖、类库、其他二进制文件、配置文件等,都统一被打入了一个称为容器镜像的包中。
通过将应用程序本身和其依赖的资源环境容器化,避免因为操作系统发行版本和其他基础环境存在差异造成的错误。

tomcat结构

eclipse配置tomcat

window=》preferences=》server=》runtime enviroments=》add=>tomcat版本=》next=》弹出窗口指定本地tomcat安装目录和运行java项目需要的jre

eclipseservlet项目配置

创建dynamic web project项目 ,右键项目名=》properties
web deployment assembly
部署项目时,设置文件发布的路径以及jar包发布的路径
Java build path
项目构建,可以指定项目编译的文件路径、引入第三方jar包等
web project settings
项目部署到服务器上的项目名

部署项目

window=》servers=》点击servers窗口内带有蓝色下划线的一行字=》对应tomcat版本=》next=》点击左侧项目后add到右侧=》finish=》右键点击刚创建的tomcat服务器,弹出的菜单点击start启动项目,stop停止项目

2servlet 创建servlet类

servlet对象是单例的

每一次请求到达服务器时,服务器(tomcat)会检查有没有目标对象(servlet对象)﹐如果没有则创建,如果有,则拿来直接使用。
由于servlet是单例的,所有的用户共享的是一个对象,所以在servlet实现类中不建议定义成员变量,在高并发情况下Servlet是线程不安全的

注意
service()方法,是执行实际业务的主要方法,由servlet容器调用service方法
作用
处理来自客户端的请求,并把格式化的响应写回给客户端
1.service()方法由容器调用;
2.service()方法会检查HTTP请求类型(GET/POST/PUT/DELETE等),然后根据请求类型的不同,掉哦给你对应的方法,如GET请求调用doGet()方法
3.综上,不需要对service进行任何修改,只需要根据客户端的请求类型来重写doGet()和doPost()方法
destroy方法

//MyServlet
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 MyServlet extends HttpServlet {

	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		//service()接收来自客户端的HTTP请求,然后根据请求方式的不同,调用相对应的doGet()或者doPost()
		String method = req.getMethod();
		System.out.println(method);
		
		if ("get".equals(method.toLowerCase())) {
			doGet(req, resp);
		}else if ("post".equals(method.toLowerCase())) {
			doGet(req, resp);
		}
	}

	@Override
	public void init() throws ServletException {
		//init()初始化方法,只会调用一次;在创建setvlet对象时调用,在后续每次用户请求时不再调用
		System.out.println("-----创建servlet对象-----");
	}

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		//通过浏览器地址栏发送请求都是doget方法
		//doget方法会接收来自客户端的get方式的HTTP请求		
		
		String name = req.getParameter("name");
		
		System.out.println("-----doGet-----"+name);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//		
		
		String name = req.getParameter("name");
		System.out.println("-----dopost------"+name);
	}

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

<!-- web.xml内 -->
<?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>servletDemo02</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  <servlet>
  	<servlet-name>my</servlet-name>
  	<servlet-class>code.servlet.MyServlet</servlet-class>
  </servlet>
  <servlet-mapping>
  	<servlet-name>my</servlet-name>
  	<url-pattern>/m</url-pattern>
  </servlet-mapping>
</web-app>

浏览器窗口http://localhost:8080/servletDemo02/m 运行两次得到下图,说明单例
image

继承关系

Servlet生命周期

生命周期

可以被定义为servlet对象从创建到销毁的整个过程:
1)在创建servlet对象时,通过调用init()方法进行初始化
2)通过service()方法来接收客户端的请求,根据请求的方式不同转发给对应的doGet()或doPost()方法
3)停止服务时,通过调用destory()方法销毁servlet对象
4)servlet对象被JVM垃圾回收器回收

请求与响应

//获取请求方式
String m = req.getMethod();
System.out.println(m);//get or post
//设置请求编码
req.setCharacterEncoding("UTF-8");
//获取请求参数
String name = req.getParameter("name");
//获取所有请求参数名的结果集
Enumeration<String> params = req.getParameterNames();
//遍历参数名的结果集,获取所有参数值
//hasMoreElements判断参数名集合中是否有元素
while(params.hasMoreElements()){
	//nextElement获取下一个元素
	String key = (String)params.nextElement();
	System.out.print(key+":"+req.getParameter(key));
}
//向请求对象中添加参数
//req.setAttrubute("user_list",list);

HttpServletResponse响应

//设置响应数据的编码
resp.setCharacterEncoding("gb2312");
//创建字符输出流,向客户端响应数据
printWriter out = null;
out = resp.getWriter();
out.write("success");
out.flush();
if(out == null){
	out.close();
}

本文作者:小彤在努力

本文链接:https://www.cnblogs.com/xiaoto9426/p/16890119.html

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   小彤在努力  阅读(38)  评论(0编辑  收藏  举报
💬
评论
📌
收藏
💗
关注
👍
推荐
🚀
回顶
收起
  1. 1 Scarborough Fair Sarah Brightman
Scarborough Fair - Sarah Brightman
00:00 / 00:00
An audio error has occurred.

Are you going to Scarborough Fair

Parsley, sage, rosemary and thyme

Remember me to one who lives there

Remember me to one who lives there

He once was a true love of mine

Tell him to make me a cambric shirt

Parsley, sage, rosemary and thyme

Without no seams nor needle work

Without no seams nor needle work

Then he'll be a true love of mine

Tell him to find me an acre of land

Parsley, sage, rosemary and thyme

Between salt water and the sea strands

Then he'll be a true love of mine

Then he'll be a true love of mine

Tell him to reap it with a sickle of leather

Tell him to reap it with a sickle of leather

Parsley, sage, rosemary and thyme

Parsley, sage, rosemary and thyme

And gather it all in a bunch of heather

And gather it all in a bunch of heather

Then he'll be a true love of mine

Then he'll be a true love of mine

Are you going to Scarborough Fair

Parsley, sage, rosemary and thyme

Parsley, sage, rosemary and thyme

Remember me to one who lives there

Remember me to one who lives there

He once was a true love of mine

He once was a true love of mine

点击右上角即可分享
微信分享提示