文章目录
一 MVC设计模式:
Model 模型:是应用程序中用于处理应用程序数据逻辑的部分。通常模型对象负责在数据库中存取数据。( 使用JavaBean实现。)
View 视图:是应用程序中处理数据显示的部分。通常视图是依据模型数据创建的。( 用于展示,以及和用户进行交互,使用html,css,js,jsp,jquery等前端技术实现。)
Controller 控制器:是应用程序中处理用户交互的部分。通常控制器负责从视图读取数据,控制用户输入,并向模型发送数据。( 接收请求,将请求跳转到模型进行处理,模型处理完毕后,再将处理的结果返回给请求处,建议使用 Servlet 实现)
二 Servlet 详解:
1 Servlet 定义:
Servlet 是符合一定规范的Java类:
(1) 必须继承 javax.servlet.http.HttpServlet
(2) 重写其中的doGet () 方法, doPost () 方法
doGet( ) :接收并处理 所有的get提交方式的请求
doPost( ):接收并处理 所有的post提交方式的请求
2 Servlet 配置:
(1) Servlet 2.5 :
若为Servlet 2.5 :配置 web.xml 文件
步骤: (可以通过新建Servlet 自动生成)
① 编写一个 class ,继承 HttpServlet
② 重写doGet () 方法,doPost () 方法
③ 编写web.xml 中的servlet 映射关系
流程:
请求(项目的根目录) ➡ url-pattern ➡ 根据 servlet-mapping 中的 servlet-name 匹配 servlet 中的servlet-name ,然后寻找到 servlet-class ,最终将请求交由该 servlet-class 执行代码实现:
index.jsp: (在 mothod=get 和 pos t分别运行)
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="WelcomeServlet" method="get"> //get方法
<input type="submit" value="提交">
</form>
</body>
</html>
WelcomeServlet.java:
package org.lanqiao.servlet;
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 WelcomeServlet extends HttpServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("this is doGet!!!");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("this is doPost!!!");
}
}
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>Servlet25Project</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>WelcomeServlet</servlet-name>
<servlet-class>org.lanqiao.servlet.WelcomeServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>WelcomeServlet</servlet-name>
<url-pattern>/WelcomeServlet</url-pattern>
</servlet-mapping>
</web-app>
运行结果 (jsp 的 method 分别用 get 和 post 运行):
(2) Servlet 3.0 :
若为Servlet 3.0 :@WebServlet 注解
区别:
不需要在 web.xml 中配置,但需要在Servlet 类中编写注解 @WebServlet(“url-pattern”)
匹配流程:
请求地址 与 @WebServlet 中的值进行匹配,若匹配成功,则说明请求的就是该注解所对应的类。
代码实现:
index.jsp文件 (不变):
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="WelcomeServlet" method="get">
<input type="submit" value="提交">
</form>
</body>
</html>
WelcomeServlet.java
package org.lanqiao.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;
/**
* Servlet implementation class WelcomeServlet
*/
@WebServlet("/WelcomeServlet")
public class WelcomeServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("this is Servlet3.0");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
web.xml (不用配置servlet和servlet-mapping)
<?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_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>Servlet30Project</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>
运行结果:
3 Servlet 生命周期(5个阶段):
1 加载 (servlet 自动执行)
2 初始化:init() ,该方法会在 Servlet 被加载并实例化后执行
(1) 默认在第一次访问Servlet时被执行 ( 只执行一次 )
(2) 可以修改为Tomcat 启动时自动执行:
① 在Servlet 2.5中 web.xml中的Servlet配置加入
<load-on-startup>1</load-on-startup>
② 在Servlet 3.0中注解修改为:
@WebServlet(value="/WelcomeServlet" , loadOnStartup=1)
3 服务:service()➡doGet() doPost() : 调用几次就执行几次
4 销毁:destroy() , 该方法在Servlet 被系统回收时执行
5 卸载 (servlet 自动执行)
运行结果:
4 Servlet API :
由两个软件包组成:对应于HTTP协议的软件包,对应于除了HTTP协议以外的其他软件包。(适用于任何通信协议)
5 Servlet继承关系
(1)ServletConfig:接口
ServletContext getServletContext():获取Servlet上下文对象
String getInitParamter(String name):在当前Servlet范围内,获取name的参数值 (初始化参数)
(2)ServletContext中的常用方法(application是对象):
① getContextPath() :相对路径
② getRealPath() : 绝对路径
③ setAttribute() :
④ getAttribute():
⑤ String getInitParameter(String name) : 在当前Web容器范围内,获取name的参数值(初始化参数)
(3)HttpServletRequest中的方法(request):
( jsp中的request就是HttpServletRequest,所以方法一致)
例如:setAttribute(),getCookies() ,getMethod()
(4)HttpServletResponse中的方法 (response):
(同jsp中的response)