01Servlet
1.1Servlet简介
- Servlet就是sun公司开发动态web的一门技术
- sun在这些API中提供一个接口叫做:Servlet,如果你想开发为一个Servlet程序,只需要完成两个步骤:
- 编写一个类,实现Servlet接口
- 把开发好的java类部署到web服务器中
把实现了Servlet接口的java程序叫做,Servlet
1.2HelloServlet
构建一个Maven项目
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
- 1.编写一个普通了类
- 2.实现Servlet接口,这里我们直接继承HttpServlet
package com.wzw.Servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
public class HelloServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
PrintWriter writer = resp.getWriter();//响应流
writer.println("Helloword");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
}
}
-
-
编写Servlet的映射
为什么需要映射:我们写的java程序,但是要通过浏览器访问,而浏览器需要连接web服务器,所以我们需要web服务中注册我们写的Servlet,还需给他一个浏览器能够访问的路径;
<!-- 注册Servlet--> <servlet> <servlet-name>Hello</servlet-name> <servlet-class>com.wzw.Servlet.HelloServlet</servlet-class> </servlet> <!-- Servlet的请求路径--> <servlet-mapping> <servlet-name>Hello</servlet-name> <url-pattern>/hello</url-pattern> </servlet-mapping>
-
-
4.配置tomcat
-
7.启动测试
2Servlet原理
2.1原理
Servlet是由Web服务器调用,web服务器在收到浏览器请求后会:
2.2 Mapping问题
1.一个Servlet可以指定一个映射路径
<servlet-mapping>
<servlet-name>Hello</servlet-name>
<url-pattern>/hello</url-pattern>
</servlet-mapping>
2.一个Servlet可以指定多个映射路径
<servlet-mapping>
<servlet-name>Hello</servlet-name>
<url-pattern>/hello</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Hello</servlet-name>
<url-pattern>/hello1</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Hello</servlet-name>
<url-pattern>/hello2</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Hello</servlet-name>
<url-pattern>/hello3</url-pattern>
</servlet-mapping>
3.一个Servlet可以指定通用映射路径
<servlet-mapping>
<servlet-name>Hello</servlet-name>
<url-pattern>/hello/*</url-pattern>
</servlet-mapping>
4.制定一些前缀或者后缀等等。。。
<servlet-mapping>
<servlet-name>Hello</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
6.优先级问题
指定了固有的映射路径的优先级最高,如果找不到就会走默认的请求路径
3 ServletContext
web容器在启动时,他会为每个 web程序都创建一个对应的Servlet对象,他代表了当前的web应用
3.1共享数据
我在这个Servlet中保存的数据,可以在另一个Servlet中拿到
package com.wzw.Servlet;
public class ServletHello extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ServletContext Context = req.getServletContext();
String username="wzw大帅";//数据
//将一个数据保存在ServletContext中,名字为:"username",值username
Context.setAttribute("username",username);
}
}
package com.wzw.Servlet;
public class ReadInfo extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ServletContext Context = req.getServletContext();
String info = (String) Context.getAttribute("username");//读取
resp.setContentType("text/html");
resp.setCharacterEncoding("utf-8");
resp.getWriter().println("我的名字: "+info);
}
}
<servlet>
<servlet-name>hello</servlet-name>
<servlet-class>com.wzw.Servlet.ServletHello</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>hello</servlet-name>
<url-pattern>/hello</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>read</servlet-name>
<servlet-class>com.wzw.Servlet.ReadInfo</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>read</servlet-name>
<url-pattern>/read</url-pattern>
</servlet-mapping>
3.2 获取初始化
<!--配置一些web应用初始化参数-->
<context-param>
<param-name>url</param-name>
<param-value>jdbc:mysql://localhost3306/mybatis</param-value>
</context-param>
<servlet>
<servlet-name>jdbc</servlet-name>
<servlet-class>com.wzw.Servlet.ServletDemo01</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>jdbc</servlet-name>
<url-pattern>/jdbc</url-pattern>
</servlet-mapping>
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ServletContext Context = req.getServletContext();
String url = context.getInitparameter("url");
resp.getWrite(),print(url)
}
输出结果:jdbc:mysql://localhost3306/mybatis
3.3 请求转发
- 转发:
- A要钱找B,B没有,B去找C,C给B,B给A
- 重定向:
- A要钱找B,B让A找C,A去找C
请求和转发:
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ServletContext Context = req.getServletContext();
Context.getRequestDispatcher("/jdbc").forward(req,resp);
}
<servlet>
<servlet-name>forword</servlet-name>
<servlet-class>com.wzw.Servlet.ServletDemo02</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>forword</servlet-name>
<url-pattern>/forword</url-pattern>
</servlet-mapping>
3.4 读取资源文件
properties
- 在java目录下新建properties
- 在resource目录下新建properties
发现:都被打包到了同一个路径下:class,我们俗称这个路径为classpath
思路:需要一个文件流
package com.wzw.Servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Properties;
public class Servletdemo03 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
InputStream is = req.getServletContext().getResourceAsStream("/WEB-INF/classes/com/wzw/servlet/aa.properties");
Properties properties = new Properties();
properties.load(is);
String user=properties.getProperty("username");
String pwd=properties.getProperty("password");
resp.getWriter().println(user+""+pwd);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
username=123
password=123
=properties.getProperty(“password”);
resp.getWriter().println(user+""+pwd);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
```properties
username=123
password=123