JAVAWEB学习——JSP九大内置对象

request :java

  服务器端接受客户端以http方式传过来的数据。
  经常使用方法:+getParameter(String args):String ,+getParameterValues():String[], +getParameterNames():Enumeration;web

  request其实是服务器接受客户端请求的信息的信息以后作进一步处理的。数组

  
一、解决乱码的问题  浏览器

hello:<%=request.getParameter("name")%>

  这么直接写出现乱码的问题:hello:鏁�安全

这个时候就要使用服务器

public void setCharacterEncoding(String env)
          throws UnsupportedEncodingException来设置赞成的编码格式。cookie

:这里设置的请求编码还要跟你JSP中编码格式同样才不会出现乱码。session

<%
	request.setCharacterEncoding("UTF-8");
%>
		hello:<%=request.getParameter("name")%>

  结果:hello:敢jsp

二、传递参数:编码

获取表单的一个参数:+getParameter(String args):String(隐藏的表单表单域)

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

获取表单的多个参数:+getParameterNames():Enumeration;

  Enumeration<String> enume = getParameterNames();

获取传递内容是数组:+getParameterValues():String[](用于下拉列表、复选框等等)

  String [] param = getParameterValues();

总结:
只要是客户端信息,在服务器端均可以经过 request 对象取得

response:服务器想客户端发送的消息,http的头信息,Cookie等等

主要使用:

  重定向response.sendDirection(loc):response.sendRedirect("firstCookie.jsp");
  设置响应头: 使用这个来刷新页面,两秒后跳转等等。
  设置cookies:response.addCookie(cookie);

总结:服务器想客户端发送的消息经过response去设置。

session:http协议是无状态的,他不知道每一次请求的浏览器是否是同一个浏览器。

  使用:保存用户的各类信息,直到他的生命周期超时or被释放。

session对象属于javax.servlet.http.HttpSession 接口的实例化对象。

session的主要方法:

  方法:session.getId()  获取一个id,这个id由服务器分配。

     public boolean isNew() 是否是新的session

      设置属性:session.setsetAttribute(String name ,string value); request.getSession().setAttribute("token", tokenValue);

      取得属性: public Object getAttribute(String name)   request.getSession().getAttribute("message")

     删除属性:public Object removeAttribute(String name)  session.removeAttribute("message");

session失效:

  若是 session 失效,则在session 所保留的所有操做也会消失
  public void invalidate():使session 失效(手工)

session的其余方法能够查看servlet API。

  session跟cookie的区别:

  session存在于服务器端、cookie存在客户端。

  session比cookie跟安全,可是比cookie更加浪费空间、资源。

  session 要尽可能少使用—— 尽可能少向session 中保存信息
  session 使用了cookie 的机制,若是cookie 被禁用,则session 也没法使用

Application:同session同样也是用来保存消息的,可是它保存的消息是全部人都能共享的,而session的私有的。(在线人数)

   只要做用:保存公共消息

   属性操做的三个方法:
    setAttribute()、getAttribute()、removeAttribute()

out:向JSP页面中动态的输出一些内容。

  our.print("hello:"+name);可是这个应该较少使用,可使用hello:<%=name  %>来代替。

config:处理web.xml中的配置信息。

 config的方法能够经过查看API去实现。javax.servlet.ServletConfig

取得初始化参数的方法:public String getInitParameter(String name)

取得所有参数:public Enumeration getInitParameterNames();

其余较少使用的不在说明;

相关文章
相关标签/搜索