JavaWeb三大组件:
实际应用中,不会实现Servlet接口,而是用它的子类HttpServlet
概念,作用:
Servlet是运行在服务器上的一个Java小程序,它可以接收客户端发送过来的请求,并响应数据给客户端
实现:
1,编写一个类实现Servlet接口
2,实现service方法,处理请求,并响应数据
3,到web.xml种配置servlet程序的访问地址
生命周期:
1,执行Servlet构造器方法
2,执行init初始化方法
//1,2在第一次访问时调用
3,执行service方法:每次访问都会调用
4,执行destroy销毁方法:在web工程停止时调用
编写一个类实现Servlet接口,实现service方法,处理请求,并响应数据
public class OneClass implements Servlet {
public OneClass(){
System.out.println("OneClass");
}
@Override
public void init(ServletConfig servletConfig) throws ServletException {
System.out.println("init");
}
@Override
public ServletConfig getServletConfig() {
return null;
}
/*
service方法是专门用来处理请求和响应的
*/
@Override
public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
System.out.println("service");
}
@Override
public String getServletInfo() {
return null;
}
@Override
public void destroy() {
System.out.println("destroy");
}
}
到web.xml种配置servlet程序的访问地址
<servlet>
<servlet-name>OneClassservlet-name>
<servlet-class>One.OneClassservlet-class>
servlet>
<servlet-mapping>
<servlet-name>OneClassservlet-name>
<url-pattern>/hellourl-pattern>
servlet-mapping>
在web目录下写一个html文件对servlet工程进行访问:
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Titletitle>
head>
<body>
<form action="http://localhost:8080/WebDemo_war/hello" method="post">
<input type="submit">
form>
body>
html>
编写service方法:
/*
service方法是专门用来处理请求和响应的
*/
@Override
public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
System.out.println("service");
//类型转换(因为子类有getMethod()方法)
HttpServletRequest httpServletRequest= (HttpServletRequest) servletRequest;
//获取请求方法
String method = httpServletRequest.getMethod();
if("GET".equals(method)){
System.out.println("method=Get");
}else if("POST".equals(method)){
System.out.println("method=Post");
}
}
在实际开发中一般都是继承Servlet的子类HttpServlet的方式去实现Servlet程序
public class TwoClass extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("doGet");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("doPost");
}
}
三大作用:
public class OneClass implements Servlet {
@Override
public void init(ServletConfig servletConfig) throws ServletException {
System.out.println("init");
//获取Servlet程序的别名servlet-name的值
System.out.println("ServletConfig Name:"+servletConfig.getServletName());
//获取初始化参数init-param
System.out.println("ServletConfig Param:"+servletConfig.getInitParameter("url"));
//获取ServletContext对象
System.out.println("ServletConfig Param:"+servletConfig.getServletContext());
}
@Override
public ServletConfig getServletConfig() {
return null;
}
@Override
public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
}
@Override
public String getServletInfo() {
return null;
}
@Override
public void destroy() {
}
}
web.xml参数配置:
<servlet>
<servlet-name>OneClass</servlet-name>
<servlet-class>One.OneClass</servlet-class>
<init-param>
<param-name>url</param-name>
<param-value>http://baidu.com</param-value>
</init-param>
</servlet>
ServletContext(通过ServletConfig对象获取):
四个作用:
1,获取web.xml中配置的上下文参数context-param
2,获取当前的工程路径,格式:/工程路径
3,获取工程部署后在服务器硬盘上的绝对路径
4,像Map一样存储数据
存数据:setAttribute()
取数据:getAttribute()
删除数据:removeAttribute()
public class ThreeClass extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ServletContext servletContext = getServletConfig().getServletContext();
//1,获取上下文参数context-param
System.out.println("ServletContext Param:"+servletContext.getInitParameter("name"));
//2,获取当前的工程路径
System.out.println("ServletContext Path:"+servletContext.getContextPath());
//3,获取服务器硬盘上的绝对路径(获得web文件的绝对路径)
// “/”被服务器解析为:http://ip:port/工程名/
System.out.println("ServletContext RealPath"+servletContext.getRealPath("/"));
//操作数据
servletContext.setAttribute("age",17);
System.out.println(servletContext.getAttribute("age"));
}
}