Servlet
1.Servlet简介
- servlet就是sun公司开发动态web的一门技术
- sun在这些API中提供一个接口叫做:
servlet
,如果想开发一个servlet
程序,只需要完成两个小步骤:- 编写一个类,实现
servlet
接口 - 把开发好的
java
类部署到web服务器中
- 编写一个类,实现
2.HellowServlet
servlet接口sun公司有两个默认的实现类:HttpServlet
和GenericServlet
(1).构建项目
构建一个普通的maven
项目,删除src
目录,之后在该项目离建立module
;这个空的工程是主工程
(2).关于maven
父子工程:
父项目中:
<modules>
<module>HelloServlet</module>
</modules>
子项目会有:
<parent>
<artifactId>servlet</artifactId>
<groupId>com.young</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
父项目中的java子项目可以直接使用
son extends father
(3).maven环境优化
①.修改web.xml
为最新的
②.将maven的结构搭建完整
(4).编写一个Servlet程序
①.编写一个普通类
②.实现servlet
接口,直接继承HttpServlet
.
public class HelloServlet extends HttpServlet {
//由于get和post只是请求实现的不同的方式,可以相互调用,业务逻辑一样
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
PrintWriter writer = resp.getWriter();//响应流
writer.print("hello servlet.");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
(5).编写servlet
的映射
我们写的是java程序,但是要通过浏览器访问,而浏览器需要连接web服务器,所以我们需要在web服务中注册我们写的servlet
,还需要给他一个能够访问的路径;
<!--注册Servlet-->
<servlet>
<servlet-name>hello</servlet-name>
<servlet-class>com.young.servlet.HelloServlet</servlet-class>
</servlet>
<!--Servlet的请求路径-->
<servlet-mapping>
<servlet-name>hello</servlet-name>
<url-pattern>/hello</url-pattern>
</servlet-mapping>
(6).配置tomcat
(7).启动测试
3.Servlet原理
servlet
是由web服务器调用,web服务器在收到浏览器请求后,
4.Mapping
(1).一个Servlet可以指定一个映射路径
<servlet-mapping>
<servlet-name>hello</servlet-name>
<url-pattern>/hello</url-pattern>
</servlet-mapping>
(2).一个Servlet可以指定多个映射路径
<!--第一个hello1-->
<servlet-mapping>
<servlet-name>hello</servlet-name>
<url-pattern>/hello1</url-pattern>
</servlet-mapping>
<!--第二个hello2-->
<servlet-mapping>
<servlet-name>hello</servlet-name>
<url-pattern>/hello2</url-pattern>
</servlet-mapping>
<!--第三个hello3-->
<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>/*</url-pattern>
</servlet-mapping>
(5).指定后缀或者前缀
<!--可以自定义后缀实现请求映射;*前不能加项目映射的路径;-->
<servlet-mapping>
<servlet-name>hello</servlet-name>
<url-pattern>*.yangyi</url-pattern>
</servlet-mapping>
(6).优先级问题
指定了固有的映射路径优先级最高,如果找不到就会走默认的处理请求
<!--注册ErrorServlet-->
<servlet>
<servlet-name>error</servlet-name>
<servlet-class>com.young.servlet.ErrorServlet</servlet-class>
</servlet>
<!--ErrorServlet的请求路径-->
<servlet-mapping>
<servlet-name>error</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
5.ServletContext
web容器在启动的时候,它会为每个web程序都创建一个对应ServletContext
对象,它代表了当前的web应用:
(1)共享数据
在一个servlet
中保存的数据,可以在另一个servlet
中拿到
//存放数据
public class HelloServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
PrintWriter writer = resp.getWriter();
writer.print("sc_hello servlet");
ServletContext servletContext = this.getServletContext();//上下文
String username = "杨一";//数据
servletContext.setAttribute("username",username);//将一个数据保存在了servletcontext中
}
}
//获取存放的数据
public class GetServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setContentType("text/html");
resp.setCharacterEncoding("utf-8");
ServletContext servletContext = this.getServletContext();
String username = (String) servletContext.getAttribute("username");
resp.getWriter().print("用户名:"+username);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req,resp);
}
}
<servlet>
<servlet-name>hello</servlet-name>
<servlet-class>com.young.servlet.HelloServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>hello</servlet-name>
<url-pattern>/hello</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>getname</servlet-name>
<servlet-class>com.young.servlet.GetServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>getname</servlet-name>
<url-pattern>/getname</url-pattern>
</servlet-mapping>
测试访问结果:
①、先进入http://localhost:8080/sc/hello
存放数据,否则http://localhost:8080/sc/getname
页面获取不到数据会显示为空
②、获取数据后,进入http://localhost:8080/sc/getname
,显示:
(2)获取初始化参数
<!--配置初始化参数-->
<context-param>
<param-name>url</param-name>
<param-value>jdbc:mysql://localhost:3306/servlet</param-value>
</context-param>
<servlet>
<servlet-name>gp</servlet-name>
<servlet-class>com.young.servlet.GPServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>gp</servlet-name>
<url-pattern>/gp</url-pattern>
</servlet-mapping>
public class GPServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setContentType("text/html");
resp.setCharacterEncoding("utf-8");
ServletContext servletContext = this.getServletContext();
String url = servletContext.getInitParameter("url");
resp.getWriter().print(url);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req,resp);
}
(3)请求转发
public class RDServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ServletContext servletContext = this.getServletContext();
System.out.println("查看是否进入该路径/rd");
RequestDispatcher requestDispatcher = servletContext.getRequestDispatcher("/gp");//转发的请求路径
requestDispatcher.forward(req,resp);//调用forward方法实现请求转发
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req,resp);
}
}
(4)读取资源文件
Properties
- 在java目录下新建properties
- 在resources目录下新建properties
都被打包到同一路径下:classes,俗称这个路径为类路径;
思路:需要一个文件流;
public class PServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//java目录下相对应的类路径
InputStream resourceAsStream = this.getServletContext().getResourceAsStream("/WEB-INF/classes/db.properties");
Properties properties = new Properties();
properties.load(resourceAsStream);
String username = properties.getProperty("username");
String password = properties.getProperty("password");
resp.getWriter().print(username);
resp.getWriter().println(password);
//properties目录下相对应的类路径
InputStream rs = this.getServletContext().getResourceAsStream("/WEB-INF/classes/com/young/servlet/aa.properties");
Properties ps = new Properties();
ps.load(rs);
String uname = ps.getProperty("username");
String pword = ps.getProperty("password");
resp.getWriter().print(uname);
resp.getWriter().print(pword);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
<servlet>
<servlet-name>p</servlet-name>
<servlet-class>com.young.servlet.PServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>p</servlet-name>
<url-pattern>/p</url-pattern>
</servlet-mapping>
运行测试:启动项目,进入路径http://localhost:8080/sc/p
,成功获取到properties
文件下的数据。