Servlet,HttpServletRequest 和 HttpServletResponse

Today is Saturday, I work overtime, but also it,

Ha ha ha ha ha ha ha ha ha ha ha ha ha

The updated content is the HttpServletRequest and HttpServletResponse

Part of the review yesterday, again today to read the rest.

Free or much knocked Code

 

##HttpServletRequest 和 HttpServletResponse


A, ### the Servlet configuration

servlet config low usage
* 1. The full path to match

> A / start / a / aa / bb

> Localhost: 8080 / Project Name / aa / bb

2. * path matching, matching first half

> To / start, but in the end * / a / * / *

> * Is actually a wildcard, matches any character

> Localhost: 8080 / Project Name / aa / bb

* 3. extension matches

> Wording: no / to * start * extension * .aa * .bb


二、###ServletContext

> Servlet context

> Each web project is only one ServletContext object. To put it plainly is, no matter what the servlet inside, get to the objects of this class are the same.

How to get ### objects

// get the object 1
ServletContext context = getServletContext ();

### what's the effect

1. Obtain global configuration parameters
2. Get Resource Project web
3. The data access, sharing of data between fields servlet objects

#### You can get global configuration parameters


Get Global Parameters



#### can obtain Web application resources

1. absolute path in access to resources inside tomcat

First get the path, and then himself new InpuStream

context.getRealPath ( "") // here is that the project root directory in tomcat inside.

D:\tomcat\apache-tomcat-7.0.52\apache-tomcat-7.0.52\wtpwebapps\Demo03\

String path = context.getRealPath("file/config.properties");

D:\tomcat\apache-tomcat-7.0.52\apache-tomcat-7.0.52\wtpwebapps\Demo03\file\config.properties

  


2. getResourceAsStream access to resources flow object

Directly opposite to the path, and then obtaining the stream object.


### to obtain resources under the web project by classloader


### access data using ServletContext.

 

1. Define a landing page html, form definition of a form

2. Define a Servlet, called LoginServlet

3. In response to the success or failure, to judge, then jump to a different page

### ServletContext Access Value Analysis


##detail:

<-!
A path: the path Servlet
HTTP: // localhost: 8080 / Demo4 / Login

B path: the current path of html:
HTTP: // localhost: 8080 / Demo4 / the login.html ->

<form action="login" method="get">
账号:<input type="text" name="username"/><br>
密码:<input type="text" name="password"/><br>
<input type="submit" value="登录"/>
</form>

  


###ServletContext 何时创建, 何时销毁?

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

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

* ServletContext 的作用范围

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


三、##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. 获取客户端提交过来的数据

 

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

  

-------------------------------------------------

//name=zhangsan&name=lisi&name=wangwu 一个key可以对应多个值。

Map<String, String[]> map = request.getParameterMap();

Set<String> keySet = map.keySet();
Iterator<String> iterator = keySet.iterator();
while (iterator.hasNext()) {
String key = (String) iterator.next();
System.out.println("key="+key + "--的值总数有:"+map.get(key).length);
String value = map.get(key)[0];
String value1 = map.get(key)[1];
String value2 = map.get(key)[2];

System.out.println(key+" ======= "+ value + "=" + value1 + "="+ value2);
}

  

3. 获取中文数据

> 客户端提交数据给服务器端,如果数据中带有中文的话,有可能会出现乱码情况,那么可以参照以下方法解决。

* 如果是GET方式

1. 代码转码

String username = request.getParameter("username");
String password = request.getParameter("password");

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编码。

2. 可以在tomcat里面做设置处理 conf/server.xml 加上URIEncoding="utf-8"

<Connector connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443" URIEncoding="UTF-8"/>

  


* 如果是POST方式

这个说的是设置请求体里面的文字编码。 get方式,用这行,有用吗? ---> 没用
request.setCharacterEncoding("UTF-8");

这行设置一定要写在getParameter之前。


##HttpServletResponse

> 负责返回数据给客户端。

* 输出数据到页面上


//以字符流的方式写数据
//response.getWriter().write("<h1>hello response...</h1>");

//以字节流的方式写数据
response.getOutputStream().write("hello response2222...".getBytes());


### 响应的数据中有中文,那么有可能出现中文乱码

* 以字符流输出

> response.getWriter()


//1. 指定输出到客户端的时候,这些文字使用UTF-8编码
response.setCharacterEncoding("UTF-8");

//2. 直接规定浏览器看这份数据的时候,使用什么编码来看。
response.setHeader("Content-Type", "text/html; charset=UTF-8");

response.getWriter().write("我爱黑马训练营...");

 

* 以字节流输出

> response.getOutputStream()




//1. 指定浏览器看这份数据使用的码表
response.setHeader("Content-Type", "text/html;charset=UTF-8");

//2. 指定输出的中文用的码表
response.getOutputStream().write("我爱深圳黑马训练营..".getBytes("UTF-8"));


--------------------------------------------

###不管是字节流还是字符流,直接使用一行代码就可以了。

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

然后在写数据即可。


###演练下载资源。

1. 直接以超链接的方式下载,不写任何代码。 也能够下载东西下来。


让tomcat的默认servlet去提供下载:<br>
<a href="download/aa.jpg">aa.jpg</a><br>
<a href="download/bb.txt">bb.txt</a><br>
<a href="download/cc.rar">cc.rar</a><br>

> 原因是tomcat里面有一个默认的Servlet -- DefaultServlet 。这个DefaultServlet 专门用于处理放在tomcat服务器上的静态资源。

 

##总结

1. Servlet注册方式

2. ServletContext【重点】

作用:

1. 获取全局参数

2. 获取工程里面的资源。

3. 资源共享。 ServletContext 域对象

有几个 一个

什么时候创建 ? 什么时候销毁

服务器启动的时候给每一个应用都创建一个ServletContext对象, 服务器关闭的时候销毁

简单登录

3. HttpServletRequest【重点】

1. 获取请求头

2. 获取提交过来的数据


4. HttpServletResponse【重点】

负责输出数据到客户端,其实就是对之前的请求作出响应

5. 中文乱码问题。【重点】

6. 下载

 

Guess you like

Origin www.cnblogs.com/toufajiantuzhongbuhui/p/11141772.html