java request,response解析及中文编码 , jsp注释 xml中文编码,getsession()使用方法

1.
request和response(请求和响应) 当Web容器收到客户端的发送过来http请求,会针对每一次请求,分别创建一个用于代表此次请求的HttpServletRequest对象(request)对象、和代表响应的HTTPServletResponse对象(response)。 request负责获取客户机提交过来的数据。 response负责向客户机输出数据。


2.

response.setContentType("text/html;charset=utf-8");   //设置 网页内容和网页语言格式

 

request.setCharacterEncoding("utf-8");


SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
String curtime=format.format(new Date());


=======================================================================================

3.

一般可以用getParameter得到页面参数。。。字符串。。。

getAttribute()可以得到对象。。。


getParameter可以得到页面传来的参数如?id=123之类的。

getAttribute()常用于servlet页面传递参数给jsp


===========================================================================================


4.<!--html注释--> <%--<jsp:forward page="/01.hello.jsp"></jsp:forward>--%> <%-- jsp注释-- %>



===========================================================================================

5.

<?xml version="1.0" encoding="utf-8"?>
<contactList>
<contact id="001" name="eric">
<name>张三</name>
<age>20</age>
<phone>134222223333</phone>
<email>zhangsan@qq.com</email>
<qq>432221111</qq>
</contact>
<contact id="002" name="jacky">
<name>eric</name>
<age>20</age>
<phone>134222225555</phone>
<email>lisi@qq.com</email>
<qq>432222222</qq>
</contact>
</contactList>


=====================================================================


6.

Jsp中的四个域对象

                          四个域对象:

                                            pageContext      page域                          

                                            request          request域

                                            session          session域

                                            application       context域

                          1)域对象作用:

                                            保存数据  和 获取数据 ,用于数据共享。

                          2)域对象方法:

                                            setAttribute("name",Object) 保存数据

                                            getAttribute("name")  获取数据

                                            removeAttribute("name") 清除数据

                          3)域对象作用范围:

                                            page域: 只能在当前jsp页面中使用(当前页面)

                                            request域: 只能在同一个请求中使用(转发)

                                            session域: 只能在同一个会话(session对象)中使用(私有的)

                                       context域: 只能在同一个web应用中使用。(全局的)


=====================================================================================




7.

getSession(boolean create)意思是返回当前reqeust中的HttpSession ,如果当前reqeust中的HttpSession 为null,当create为true,就创建一个新的Session,否则返回null; 
简而言之: 
HttpServletRequest.getSession(ture)等同于 HttpServletRequest.getSession() 
HttpServletRequest.getSession(false)等同于 如果当前Session没有就为null; 

   使用

当向Session中存取登录信息时,一般建议:HttpSession session =request.getSession();

当从Session中获取登录信息时,一般建议:HttpSession session =request.getSession(false);






原文地址:https://www.cnblogs.com/yimian/p/7348710.html