JSP 内置对象request常见用法

如图1所示,访问请求参数

 

<a href="login.jsp?name=张三&sex=man&id=">传递参数</a>

 

login.jsp关键代码

 
  1. <%= "name:"+new String(request.getParameter("name").getBytes("ISO-8859-1"),"utf-8") %><br>

  2. <%= "sex:"+request.getParameter("sex") %><br>

  3. <%= "id:"+request.getParameter("id") %><br>

  4. <%= "pwd:"+request.getParameter("pwd") %><br>


说明:如果指定的参数不存在,将返回NULL;如果指定了参数名,但未指定参数值,将返回空的字符串“。

因为所有的请求请求都是ISO-8859-1的,而在页面采用的是UTF-8编码方式,所以在遇到中文时,将获取到的数据通过字符串的构造方法使用指定的编码类型重新构造一个字符串对象。

 

2,在作用域中管理属性

在进行请求转发时,需要把一些数据传递到转发后的页面进行处理,这时,需要用请求对象的的setAttribute方法将数据保存在请求范围内的变量中。

 

 
  1. <%

  2. try {

  3. int money = 100;

  4. int number = 0;

  5. request.setAttribute("result", money / number);

  6. } catch (Exception e) {

  7. request.setAttribute("result", "很抱歉,页面产生错误!");

  8. }

  9. %>

  10.  
  11. <jsp:forward page="deal.jsp" />

 

<%= request.getAttribute("result").toString() %><br>


由于的getAttribute方法返回值是对象,需要调用的toString方法转换为字符串。

 

3,获取饼干

饼干是小段文本信息,在网络服务器上生成,并发送给浏览器。通过饼干可以标识用户身份,记录用户名和密码,跟踪重复用户等。以键值对形式保存在客户机的某个目录下。

 

 
  1. <%

  2. Cookie[] cookies = request.getCookies();

  3. String user = "";

  4. String date = "";

  5. if (cookies != null) {

  6. for (int i = 0; i < cookies.length; i++) {

  7. if (cookies[i].getName().equals("mrCookie")) {

  8. user = URLDecoder.decode(cookies[i].getValue().split("#")[0]);

  9. date = cookies[i].getValue().split("#")[1];

  10. }

  11. }

  12. }

  13. if ("".equals(user) && "".equals(date)) {

  14. %>

  15. 游客您好,欢迎您初次光临!

  16. <form action="deal.jsp" method="post">

  17. 请输入姓名:<input name="user" type="text" value=""> <input

  18. type="submit" value="确定">

  19. </form>

  20.  
  21. <% } else { %>

  22.  
  23. 欢迎

  24. <b> <%=user%></b> 再次光临

  25. <br>

  26.  
  27. <% }%>


deal.jsp:

 

 
  1. <%

  2. request.setCharacterEncoding("GB18030");

  3. String user = URLEncoder.encode(request.getParameter("user"), "utf-8");

  4. Cookie cookie = new Cookie("mrCookie", user + "#" + new Date().toLocaleString());

  5. cookie.setMaxAge(60 * 60 * 24 * 30);

  6. response.addCookie(cookie);

  7. %>

  8.  
  9. <script type="text/javascript">

  10. window.location.href = "index.jsp"

  11. </script>


4,获取客户端信息


 

 

 
  1. <br> 客户提交信息的方式:<%=request.getMethod() %>

  2. <br> 使用的协议:<%=request.getProtocol() %>

  3. <br> 客户端地址:<%=request.getRequestURL() %>

  4. <br> 客户端ip地址:<%=request.getRemoteAddr() %>

  5. <br> 服务器端口号:<%=request.getServerPort() %>

  6. <br> 服务器名称:<%=request.getServerName() %>

  7. <br> 客户端主机名:<%=request.getRemoteHost() %>

  8. <br> 客户端所请求的脚本文件的文件路径:<%=request.getServletPath() %>

  9. <br> Http协议定义的文件头信息Host的值:<%=request.getHeader("host") %>

  10. <br> Http协议定义的文件头信息User-Agent的值:<%=request.getHeader("user-agent") %>

  11. <br> Http协议定义的文件头信息accept-language的值:<%=request.getHeader("accept-language") %>

  12. <br> 请求文件的绝对路径:<%=request.getRealPath("index.jsp") %>


5,显示国际化信息

浏览器可以通过接受语言的HTTP报头向网络服务器指明它所使用的本地语言.java.util.Local类型对象封装了一个国家和国家所使用的一种语言示例如下:

 

 
  1. <%

  2. Locale locale = request.getLocale();

  3. String str = "";

  4. if (locale.equals(Locale.US)) {

  5. str = "Hello,welcome to access our company's web!";

  6. }

  7. if (locale.equals(Locale.CHINA)) {

  8. str = "您好,欢迎访问我们公司网站!";

  9. }

  10. %>

  11.  
  12. <%=str%>

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值