HttpServletRequest和HttpServletResponse

1. servlet的注册方式

在3.0版本的web工程默认是不创建web.xml配置文件的,需要一直点next最后一项勾选上

web.xml里的配置格式,EncodingServlet是原本EncodingServlet.java,右击new一个servlet会在web.xml生成以下

<servlet>
    <description></description>
    <display-name>EncodingServlet</display-name>
    <servlet-name>EncodingServlet</servlet-name>
    <servlet-class>com.liuyi.servlet.EncodingServlet</servlet-class>//类的路径
  </servlet>
  <servlet-mapping>
    <servlet-name>EncodingServlet</servlet-name>
    <url-pattern>/EncodingServlet</url-pattern>//浏览器访问的路径
  </servlet-mapping>

浏览器访问的路径可以自定

 1. 全路径匹配

以 / 开始   /a /aa/bb

 localhost:8080/项目名称/aa/bb 

2.  路径匹配 , 前半段匹配

以  / 开始 , 但是以 * 结束  /a/* /*  

 其实是一个通配符,匹配任意文字

localhost:8080/项目名称/aa/bb 

3.  以扩展名匹配

写法: 没有/  以 * 开始   *.扩展名    *.aa *.bb 

 

2. servletContext作用

1. 获取全局配置参数

web.xml里存了信息,可取出值为北京

2. 获取web工程中的资源

1. 获取资源在tomcat里面的绝对路径

目录结构如左,默认读取文件是在WebContent下

ServletContext context=getServletContext();
		
String path=context.getRealPath("/config.properties");
//path=G:\计算机学习\apache-tomcat-9.0.12\webapps\HelloServlet\config.properties

2. getResourceAsStream,直接用IputSteam接收

ServletContext context=getServletContext();
InputStream is=context.getResourceAsStream("/config.properties");

3. 存取数据,servlet间共享数据

ServletContext context=getServletContext();
context.setAttribute("count");
context.getAttribute("count");

 

3. ServletContext 何时创建, 何时销毁?

服务器启动的时候,会为托管的每一个web应用程序,创建一个ServletContext对象

从服务器移除托管,或者是关闭服务器。

  • ServletContext 的作用范围

只要在这个项目里面,都可以取。 只要同一个项目。 A项目 存, 在B项目取,是取不到的? ServletContext对象不同。

 

4 HttpServletRequest

1. 获取请求头信息

//得到一个枚举集合  
Enumeration<String> headerNames = request.getHeaderNames();
while (headerNames.hasMoreElements()) {
    String name = (String) headerNames.nextElement();
    String value = request.getHeader(name);
    System.out.println(name+"="+value);

}

2. 获取客户端传来的信息,需要客户端有这个名字的信息,name和address

String name = request.getParameter("name");
String address = request.getParameter("address");
System.out.println("name="+name);
System.out.println("address="+address);

3. 获取中文信息

get方式请求

System.out.println("userName="+username+"==password="+password);

//get请求过来的数据,在url地址栏上就已经经过编码了,所以我们取到的就是乱码,
//tomcat收到了这批数据,getParameter 默认使用ISO-8859-1去解码

//先让文字回到ISO-8859-1对应的字节数组 , 然后再按utf-8组拼字符串
username = new String(username.getBytes("ISO-8859-1") , "UTF-8");
System.out.println("userName="+username+"==password="+password);

//直接在tomcat里面做配置,以后get请求过来的数据永远都是用UTF-8编码。 可以在tomcat里面做设置处理 conf/server.xml 加上URIEncoding="utf-8"

post方式请求

request.setCharacterEncoding("UTF-8");
//写在getParameter

 

4. HttpServletReponse

输出信息到页面上

response.getWriter.write("你好");

response.getOutputStream.write("你好".getBytes());

//有中文时可能会乱码,在最前面设置这个

response.setContentType("text/html;charset=utf-8");

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值