文章目录
Servlet应用-HttpServlet
在开发中,通常浏览器访问web服务器端的资源,是带有协议的访问,比如说http协议、https协议等,所以说
在创建servlet时。需要使用带有协议的servlet,那么咱们程序员在创建servlet时,通常用继承HttpServlet的方式来实现。
查看HttpServlet源码得到的结论:
在HttpServlet里面重写service方法时,因为在service方法里面,最终调用doGet或者doPost方法,
所以说在重写服务方法时,一般情况下不会重写service方法,而是直接重写doGet和doPost方法
在客户端发送get请求,在服务器端就用doGet方法接收、
在客户端发送Post请求,在服务器端就用doPost方法来接收
代码如下:
public class MyHttpServlet extends HttpServlet{
/**
* 接收客户端发送的get请求
*/
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
String method = req.getMethod();
System.out.println(method);
System.out.println("这是发送的get请求");
}
/**
* 接收客户端发送的post请求
*/
public void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
String method = req.getMethod();
System.out.println(method);
System.out.println("这是发送的Post请求");
}
}
注意的细节:
1.在使用对象时,使用带有协议的请求和响应对象HttpServletRequset和HttpServletResponse
2.在开发时,继承HttpServlet是,直接重写doGet和doPost方法,而不是重写service方法
访问servlet的具体执行过程:见图分析。
使用Eclipse工具开发servlet
通过开发工具创建servlet有两种方式:
1.先创建一个普通的java类,需要去实现Servlet接口,或者继承对应的HttpServlet(GenericServlet)类,
咱们普通的java类就具有了servlet功能,所以需要手动在web.xml配置文件中添加访问servlet的路径信息。
2.直接创建servlet,会自动在web.xml文件中添加访问servlet的路径信息。
步骤:
1.新建web项目:
比如说:创建一个web项目名称:chapter03
2.创建一个servlet程序:
比如说:先创建一个包,名称cn.itcast.servlet ,servlet的名称:TestServlet01
3.部署和访问servlet:
通过开发工具部署web项目,如chapter03,访问它下面的servlet资源:
访问的地址:http://localhost:8080/chapter03/testServlet01
注意:一般情况下,为了简化开发,咱们会在doPost方法里面调用doGet方法。
具体见下代码:
/**
* doGet方法,用来接收浏览器发送的get请求
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
PrintWriter out = response.getWriter();
out.print("this servlet is created by eclipse");
}
/**
* doPost方法,用来接收浏览器发送的post请求
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request, response);
}
需求: 可以发送get请求和post请求得到1到100之间的和
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
int sum=0;
for(int i=0;i<=100;i++){
sum=sum+i;
}
------
}
/**
* doPost方法,用来接收浏览器发送的post请求
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request,response);
}
实现Servlet的虚拟路径映射
1.Servlet的多重映射:一个Servlet,可以对应多个访问路径。
比如:一个servlet对应的访问路径:/test02 或者 /test03
</servlet-mapping>
<servlet-mapping>
<servlet-name>TestServlet01</servlet-name>
<url-pattern>/test02</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>TestServlet01</servlet-name>
<url-pattern>/test03</url-pattern>
</servlet-mapping>
.Servlet的映射路径可以使用通配符
servlet的映射路径有三种方式:、
完全目录匹配: 比如 /hello
目录匹配: 比如 /abc/* 或者 /*
扩展名匹配: *.do ,*.action 注意 /*.do这种写法是错误的。
优先级:完全目录匹配>目录匹配>扩展名匹配
3.缺省Servlet
什么是缺省Servlet:它的映射路径 “/” ,代表这是一个缺省的servlet.
缺省的servelt作用:处理请求的资源找不到的问题(404 代表请求的资源找不到)
ServletConfig接口
什么是ServletConfig: 它是servlet的配置对象,作用就是获取与servlet的初始化参数。
它的子类是GenericServlet和HttpServlet.
它被实列化是通过web服务器实现的。
得到servletConfig对象:getServletConfig()
ServletConfig常用的方法:
1. 获取servlet的初始化参数的值:
getInitParameter(String name):根据encoding获取utf-8
2. 获取servlet初始化参数的名称:
getInitParameterNames() :获取encoding和username等名称
3. 得到servletContext对象:
getServletContext()
4. 获取servlet的名称:
getServletName():对应web.xml里面TestServlet02
初始化参数的配置:
<!-- servlet初始化参数的配置 -->
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
<init-param>
<param-name>username</param-name>
<param-value>x</param-value>
</init-param>
.ServletContext接口
ServletContext对象是在web服务器启动时就创建了,是web服务器创建的servletContext对象,
每一个web项目只有一个ServletContext对象。
ServletContext对象的作用:获取web应用程序的初始化参数,在web应用程序能共享数据,获取web项目下的资源文件。
得到ServletContext对象:
1.通过ServletConfig对象: config.getServletContext()
2.直接得到: getServletContext();
获取web应用程序的初始化参数(web应用程序就是web项目)
1. 获取全局初始化参数的值:getInitParameter(String name)
2. 获取全局初始化参数的名称:getInitParameterNames()
比如:
<!-- 配置全局的初始化参数 -->
<context-param>
<param-name>AAA</param-name>
<param-value>aaa</param-value>
</context-param>
<context-param>
<param-name>BBB</param-name>
<param-value>bbb</param-value>
</context-param>
实现多个servlet对象之间共享数据:
因为一个web项目只有一个ServletContext对象。
ServletContext对象就是域对象:域对象在一定范围内能存值和取值。
域对象相关的方法:
* 存值:void setAttribute(String key,Object obj);
* 取值:Object obj = getAttribute(key);
* 删除值:void removeAttribute(key);
比如:在TestServlet04里面存值:
//1.得到ServletContext对象
ServletContext context = this.getServletContext();
//2.存值
context.setAttribute("key", "servlet04里面存的值----");
在TestServletO5里面获取值:
//1.得到servletContext对象
ServletContext context = this.getServletContext();
//2.从ServletContext取值
Object obj =context.getAttribute("key");
String value=obj.toString();
response.setContentType("text/html;charset=utf-8");
response.getWriter().print(value);
.获取Web下面的资源文件
通过ServletContext对象能够获取Web项目下面文件的字节输入流,或者文件的绝对路径。
路径问题:
普通java项目:直接使用jdk编译java文件,所以说文件的路径是相对于工作空间。
比如:
Properties por = new Properties();
InputStream inStream = new FileInputStream("src/config.properties");
por.load(inStream);
String value =por.getProperty("key");
System.out.println(value);
web项目:运行在tomcat服务器上面的,所有说文件的路径是相对于tomcat服务器。
服务器端是没有src这个目录,写路径不能写src,必须相对于tomcat服务器端的路经。
1.获取文件的自己输入流:路径是相对于tomcat服务器的路径
InputStream in =getResourceAsStream(String path)
比如:
// 获取文件的字节输入流
ServletContext context = this.getServletContext();
String path="/WEB-INF/classes/config.properties";
InputStream in = context.getResourceAsStream(path);
Properties pro= new Properties();
pro.load(in);
//获取配置文件的值
String v1 = pro.getProperty("Company");
String v2= pro.getProperty("Address");
response.getWriter().print("company name :"+v1+" company address :"+v2);
2.获取文件的绝对路径:路径是相对于服务器端的路径
String realPath = getRealPath(path);
比如:
//1.得到ServletContext对象
ServletContext context = this.getServletContext();
//2.获取文件的绝对路径
String path="/WEB-INF/classes/config.properties";
//3.调用getRealPath
//E:\soft\apache-tomcat-7.0.55\webapps\chapter03\WEB-INF\classes\config.properties
String realPath = context.getRealPath(path);
System.out.println(realPath);
InputStream in = new FileInputStream(realPath);