Servlet简介
- Servlet是sun公司开发动态web资源的一门技术,它提供了一个接口类Servlet
- 如何使用Servlet
1.编写一个类,实现servlet接口
2.把开发好的Java类部署到web服务器中
- 总而言之:Servlet就是实现了Servlet接口的Java程序
继承关系:Servlet->Generic->HttpServlet,我们的类只需要继承HttpServlet即可
public interface Servlet {
void init(ServletConfig var1) throws ServletException;
ServletConfig getServletConfig();
void service(ServletRequest var1, ServletResponse var2) throws ServletException, IOException;
String getServletInfo();
void destroy();
}
Servlet原理
- Servlet是由web服务器调用,web服务器在收到浏览器请求之后,会:
Mapping问题
1.一个Servlet映射一个路径
<servlet>
<servlet-name>hello</servlet-name>
<servlet-class>com.lxd.servlet.HelloServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>hello</servlet-name>
<url-pattern>/hello</url-pattern>
</servlet-mapping>
2.一个Servlet映射多个路径
- 访问hello和hello1都可以访问到HelloServlet
<servlet>
<servlet-name>hello</servlet-name>
<servlet-class>com.lxd.servlet.HelloServlet</servlet-class>
</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>
3.通用映射路径
- *为通配符,表示hello路径下的所有路径都能访问HelloServlet
<servlet>
<servlet-name>hello</servlet-name>
<servlet-class>com.lxd.servlet.HelloServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>hello</servlet-name>
<url-pattern>/hello/*</url-pattern>
</servlet-mapping>
4.默认请求路径
- 启动服务器后会直接访问HelloServlet而不是index.jsp
<servlet>
<servlet-name>hello</servlet-name>
<servlet-class>com.lxd.servlet.HelloServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>hello</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
5.指定后缀:*前面不能加映射的路径
<servlet>
<servlet-name>hello</servlet-name>
<servlet-class>com.lxd.servlet.HelloServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>hello</servlet-name>
<url-pattern>*.hello</url-pattern>
</servlet-mapping>
- 确定的映射路径优先级最高,如果找不到就会走默认的处理请求/*
ServletContext对象:上下文对象
1.共享数据
- setAttribute():设置属性和属性值
- getAttribute():获取属性对应的属性值
public class HelloServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("utf-8");
//上下文对象
ServletContext context = this.getServletContext();
String username = "你好";
context.setAttribute("username",username);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
public class GetServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ServletContext context = this.getServletContext();
resp.setContentType("text/html;charset=utf-8");
String username = (String) context.getAttribute("username");
resp.getWriter().print(username);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
2.获取初始化参数
- getInitParameter():获取初始化参数
web.xml中
<context-param>
<param-name>url</param-name>
<param-value>www.baidu.com</param-value>
</context-param>
public class ServletDemo3 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ServletContext context = this.getServletContext();
String url = (String) context.getInitParameter("url");
resp.getWriter().print(url);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
3.请求转发
- 转发:跳转到指定的Servlet,但是url路径不会改变
- 也就是说,A请求B,B跳转到了C,A是看不到C的
public class ServletDemo4 extends HelloServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ServletContext context = this.getServletContext();
//sd3为ServletDemo3的映射路径
context.getRequestDispatcher("/sd3").forward(req,resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
4.读取资源文件
- Properties
1.在java目录下新建properties
2.在resources目录下新建properties
- 他们都会被打包到target/项目名(模块名)/WEB-INF/classes下
- 需要通过文件流来读取资源文件
db.properties文件中:
username=lxd
password=123456
public class PropertiesServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
InputStream is = this.getServletContext().getResourceAsStream("WEB-INF\\classes\\db.properties");
Properties prop = new Properties();
prop.load(is);
String username = prop.getProperty("username");
String password = prop.getProperty("password");
resp.getWriter().print(username+":"+password);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}