2-2-1 Java Web基础-Servelt

J2EE是什么

Java 2 Paltform Enterprise Edition(Java2企业版)

B/S模式开发应用就是J2EE最核心的功能

J2EE由13个模块组成,最主要的是Servlet(Web服务器小程序),JSP(服务器页面),JDBC(数据库交互模块),XML(XML交互模块)等12个模块

Tomcat是什么

Tomcat是Apache软件基金会旗下一款免费的开放源代码Web应用服务器程序.

Tomcat是运行Servelt(服务器小程序)的容器

J2EE与Tomcat关系

J2EE是一组技术规范与指南,具体实现由软件厂商决定.

Tomcat是J2EE Web (Servlet与JSP)标准实现者

J2EE是J2EE运行的基石,运行Tomcat离不开J2EE

标准Java Web工程结构

组织结构 描述
tomcat安装目录/webapps/ Tomcat应用根目录
/web应用目录/ Java Web应用目录
/web应用目录/index.html|.jsp 默认首页
/WEB-INF WEB应用的安全目录,用于存放配置文件
/WEB-INF/web.xml web.xml是部署描述符文件,是该项目核心配置文件
/WEB-INF/classes 存放编译后的classes文件
/WEB-INF/lib 用于存放web应用依赖的jar文件
/META-INF/MANITEST.MF 包含应用的版本等信息

Servlet开发步骤

1.创建Servlet类,继承HttpServlet

2.重写service方法,编写程序代码

 1 import java.io.IOException;
 2 import java.io.PrintWriter;
 3 import javax.servlet.ServletException;
 4 import  javax.servlet.http.HttpServlet;
 5 import javax.servlet.http.HttpServletRequest;
 6 import javax.servlet.http.HttpServletResponse;
 7 
 8 public class FirstServlet extends HttpServlet {
 9 
10     @Override
11     protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
12         //接收请求参数
13         String name = req.getParameter("name");
14         String html = "<h1 style='color:red'>"+name+"</h1><hr/>";
15         PrintWriter out = resp.getWriter();
16         //将html返回浏览器
17         out.print(html);
18     }
19 }

3.配置web.xml,绑定URL

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
 3          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
 5          version="4.0">
 6 
 7     <!--声明servlet-->
 8     <servlet>
 9         <!-- servlet别名 -->
10         <servlet-name>first</servlet-name>
11         <servlet-class>com.mingm.FirstServlet</servlet-class>
12     </servlet>
13 
14     <!--将servlet与url绑定-->
15     <servlet-mapping>
16         <servlet-name>first</servlet-name>
17         <url-pattern>/hi</url-pattern>
18     </servlet-mapping>
19 </web-app>

Servlet访问方法

http://IP地址:端口/context-path/url-mapping

context-path为"上下文路径",默认工程名

Servlet接收请求参数

request.getParameter()-接收单个请求参数

request.getParameterValues()-接收多个同名参数

Get与Post请求

Get方式是将数据通过在URL附加数据显性向服务器发送数据.

例 http://localhost:8080/FirstServer/sample?name=zhangsan

Post方法会将数据存放在"请求体"中隐性向服务器发送数据

例 http://localhost:8080/FirstServer/sample

     请求体: name=zhangsan

处理方式

所有请求 - service()方法

Get请求 - doGet()方法

Post请求 - doPost()方法

 1 public class RequestMethodServlet extends HttpServlet{
 2     //处理get请求
 3     public void doGet(HttpServletRequest request , HttpServletResponse response) throws IOException{
 4         String name = request.getParameter("name");
 5         response.getWriter().println("<h1 style='color:green'>" + name + "</h1>");
 6     }
 7     //处理post请求
 8     public void doPost(HttpServletRequest request , HttpServletResponse response) throws IOException{
 9         String name = request.getParameter("name");
10         response.getWriter().println("<h1 style='color:red'>" + name + "</h1>");
11     }
12 }

Servlet生命周期

1.装载 - web.xml

2.构建 - 构造函数

3.初始化 - init()

4.提供服务 - service()

5.销毁 - destroy()

使用注解简化配置

Servlet3.x引入了注解

注解用于简化Web应用程序的配置过程

Servlet核心注解: @WebServlet

 1 import java.io.IOException;
 2 
 3 import javax.servlet.ServletException;
 4 import javax.servlet.annotation.WebServlet;
 5 import javax.servlet.http.HttpServlet;
 6 import javax.servlet.http.HttpServletRequest;
 7 import javax.servlet.http.HttpServletResponse;
 8 
 9 @WebServlet("/anno")
10 public class AnnotationServlet extends HttpServlet{
11 
12     @Override
13     protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
14         // TODO Auto-generated method stub
15         resp.getWriter().println("I'm annotation servlet!");
16     }
17     
18 }

启动时加载Servlet

web.xml使用<load-on-startup>设置进行加载

<load-on-startup>0~9999</load-on-startup>

启动时加载常用于系统的预处理

 1 import java.io.IOException;
 2 import java.io.PrintWriter;
 3 import javax.servlet.ServletException;
 4 import  javax.servlet.http.HttpServlet;
 5 import javax.servlet.http.HttpServletRequest;
 6 import javax.servlet.http.HttpServletResponse;
 7 
 8 public class FirstServlet extends HttpServlet {
 9 
10     @Override
11     public void init() throws ServletException {
12         System.out.println("启动时加载");
13     }
14 
15     @Override
16     protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
17         //接收请求参数
18         String name = req.getParameter("name");
19         String html = "<h1 style='color:red'>"+name+"</h1><hr/>";
20         PrintWriter out = resp.getWriter();
21         //将html返回浏览器
22         out.print(html);
23     }
24 }
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
 3          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
 5          version="4.0">
 6 
 7     <!--声明servlet-->
 8     <servlet>
 9         <!-- servlet别名 -->
10         <servlet-name>first</servlet-name>
11         <servlet-class>com.mingm.FirstServlet</servlet-class>
12         <load-on-startup>0</load-on-startup>
13     </servlet>
14 
15     <!--将servlet与url绑定-->
16     <servlet-mapping>
17         <servlet-name>first</servlet-name>
18         <url-pattern>/hi</url-pattern>
19     </servlet-mapping>
20 
21 </web-app>

注解用法

 1 import java.io.IOException;
 2 import java.io.PrintWriter;
 3 import javax.servlet.ServletException;
 4 import javax.servlet.annotation.WebServlet;
 5 import  javax.servlet.http.HttpServlet;
 6 import javax.servlet.http.HttpServletRequest;
 7 import javax.servlet.http.HttpServletResponse;
 8 
 9 @WebServlet(urlPatterns = "/first", loadOnStartup = 1)
10 public class FirstServlet extends HttpServlet {
11 
12     @Override
13     public void init() throws ServletException {
14         System.out.println("启动时加载");
15     }
16 
17     @Override
18     protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
19         //接收请求参数
20         String name = req.getParameter("name");
21         String html = "<h1 style='color:red'>"+name+"</h1><hr/>";
22         PrintWriter out = resp.getWriter();
23         //将html返回浏览器
24         out.print(html);
25     }
26 }

 

上一篇:在IDEA中编写一个简单的Servlet


下一篇:Servlet 学习笔记