JSP内置对象

系列文章目录


例如:第四章JSP内置对象的总结


目录

系列文章目录

文章目录

前言

4.1 request 对象

4.1.1 获取用户提交的信息

4.1.2 处理汉字信息

4.1.3 request对象的一些常用方法

4.1.4 处理HTML标记

4.1.5 处理超链接

4.2 response 对象的重定向

4.3 session 对象(存用户信息)

4.4 application 留言板

总结


前言

例如:response和request 对象是JSP内置对象中较为重要的两个,这两个对象提供了对服务器和浏览器通信方法的控制。


4.1 request 对象

HTTP 通信协议是用户与服务器之间一种提交(请求)信息与响应信息(request/response)的通信协议。在JSP中,内置对象 request 封装了用户提交的信息。那么该对象调用相应的方法可以获取封装的信息,即使用该对象可以获取用户提交的信息。

tips:用户通常使用HTML的form表单请求访问服务器的某个jsp页面,并提交必要的信息给所请求的jsp页面(servlet)。表单的一般格式:

<form action = "请求访问页面或servlet" method = get | psot >

      提交手段

</form>
 

4.1.1 获取用户提交的信息

request 对象获取用户提交的信息的最常用的方法是getParameter(String s )

下面的example4_2.jsp 通过表单向当前页面提交购物小票,当前页面负责计算购物小票的价格总和。

代码如下:

<%@ page contentType="text/html" %>
<%@ page pageEncoding = "utf-8" %> 
<%@ page import="java.util.regex.Pattern" %>
<%@ page import="java.util.regex.Matcher" %>
<style>
   #tom{
      font-family:宋体;font-size:22;color:blue 
   }
</style> 
<HTML><body id ="tom" bgcolor = #ffccff>
输入购物小票内容(显示的是默认内容):
<%
   String content = "牛奶:12.68元,面包:6.6元,"
                    +"苹果:28元,香皂:6.58元";
%>
<form  action =""  method="post" id ="tom">
   <textArea  name="shopping" rows=5 cols=32 id ="tom">
      <%= content %>
   </textArea>
   <input type="submit" id ="tom" name="submit" value="提交"/>
</form>  
<%   String shoppingReceipt=request.getParameter("shopping");
     if(shoppingReceipt==null) {
        shoppingReceipt="0";
     }
     Pattern pattern;          //模式对象
     Matcher matcher;          //匹配对象
     String regex="-?[0-9][0-9]*[.]?[0-9]*" ;//匹配数字,整数或浮点数的正则表达式。
     pattern = Pattern.compile(regex); //初试化模式对象。
     matcher =
     pattern.matcher(shoppingReceipt); //matcher检索shoppingReceipt。
     double sum = 0;
     while(matcher.find()) {
       String str = matcher.group(); 
       sum += Double.parseDouble(str);
     } 
     out.print("购物小票消费总额:"+sum);
%>
</body></HTML>

 运行结果:

tips:如果程序使用了空对象,Java解释器就会提示出现NullPointerReception异常。这个时候就需要使用如下代码:

 <%   String shoppingReceipt=request.getParameter("shopping");
     if(shoppingReceipt==null) {
         shoppingReceipt="0";
     }

4.1.2 处理汉字信息

JSP页面文件的编码方式为utf-8编码,只要让request对象在获取对象之前调用setCharacterEncoding方法设置编码为utf-8就可以避免乱码的现象。 

代码如下(示例):request.setCharacterEncoding("utf-8");

4.1.3 request对象的一些常用方法

我们可以通过下面例题example4_4.jsp.看看一些常用的方法

<%@ page contentType="text/html" %>
<%@ page pageEncoding = "utf-8" %> 
<HTML><body bgcolor = #ffccff>
<p style="font-family:宋体;font-size:36;color:blue"> 
   <% request.setCharacterEncoding("utf-8");
      String jsp=request.getServletPath();    //请求的JSP页面
      jsp = jsp.substring(1); //去掉JSP页面名称前面的目录符号/
      String webDir = request.getContextPath();//获取当前Web服务目录的名称
      webDir = webDir.substring(1); //去掉Web服务目录的名称前面的目录符号/
      String  clientIP=request.getRemoteAddr();//用户的IP地址
      int serverPort=request.getServerPort(); // 服务器的端口号
      out.print("<br>shenme:"+request.getServerName());
    %> 
用户请求的页面:<%= jsp %>
<br>Web服务目录的名字:<%= webDir %>
<br>用户的IP地址:<%= clientIP %>
<br>服务器的端口号:<%= serverPort %>
</p></body></HTML>

4.1.4 处理HTML标记

(1) form标记/form表单 :用于对象提交数据

一般格式:

< form action = "请求访问的页面或者servlet" method = get|post>

   各种提交手段

   提交键

</form>

其中form表单经常把 文本框,下拉列表,滚动列表等作为form表单的子标记

<input ...../>文本框

<select...></select>下拉框

<option...></option>滚动框

(2) input 标记:用来指定form表单中数据的输入方式以及form表单的提交键

基本格式:

<input type = "GUI对象" name = "GUI对象的名字" value= "GUI中的默认值"/>

(3)select,option标记

下拉式列表和滚动列表通过select和option标记来定义,经常作为form的子标记

下拉标记格式:

<select name = "myName">

  <option value= "item1">文本描述</option>

  <option value= "item2">文本描述</option>

    ...

</select>

滚动标记格式(在select中增加size属性的值):

<select name = "myName" size= "正整数">

  <option value= "item1">文本描述</option>

  <option value= "item2">文本描述</option>

    ...

</select>

example4_6.jsp展示了下拉列表和滚动标记的用法

<%@ page contentType="text/html" %>
<%@ page pageEncoding = "utf-8" %>
<style>
   #tom{
      font-family:宋体;font-size:26;color:blue 
   }
</style> 
<%   String music = request.getParameter("music");
     String pic = request.getParameter("pic");
     String onOrOff=request.getParameter("R"); 
     if(music==null) music = "";
     if(pic==null)   pic = "";
     if(onOrOff==null) onOrOff = "off";
%>
<HTML><body id=tom background="image/<%= pic %>" >
<form action=""  method=post >
   <b>选择音乐:<br>
   <select id=tom name="music" >
      <Option selected value="back1.mp3">绿岛小夜曲
      <Option value="back2.mp3">我是一片云</option>
      <Option value="back3.mp3">红河谷</option>
   </select> 
   <input type="radio" name="R" value="on" />打开 
   <input type="radio" name="R" value="off" />关闭 
   <br><b>选择背景图像:<br>
   <select id=tom name="pic" size = 2>
      <option value="back1.jpg">荷花图</option>
      <option value="back2.jpg">玫瑰图</option>
      <option value="back3.jpg">校园图</option>
   </select> <br> 
   <input id=tom type="submit"  name="submit" value="提交"/>
</form> 
<%   if(onOrOff.equals("on")) {
%>       <br><embed src="sound/<%= music %>" height=50 />
<%   } 
%>
</body></HTML> 

后面还有textArea标记(多行文本框),style样式标记,table标记。就不做太详细的介绍了。

4.1.5 处理超链接

HTML的超链接标记

<a href = 链接的页面地址> 文字说明</a>

超链接所链接的页面,使用request对象调用getParameter("参数")方法获取超链接参数传递过来的参数的值,即字符串(不允许含有ASCII字符,例如汉字等)。

String idStr = request.getParameter("id")

4.2 response 对象的重定向

用户----------->(request)服务器

服务器-------->(response)用户

我们可以使用response对象的sendRedirect(URL url)方法实现用户的重定向,即可以让用户从一个页面跳转到sendRedirect(URL url)中url指定的页面,即所谓的客户端跳转。

在example4_12.jsp的form表单中输入姓名,提交给example4_12_receive.jsp页面。如果未输入姓名就提交form表单,则会重定向到example4_12.jsp中。

 example4_12.jsp:

<%@ page contentType="text/html" %>
<%@ page pageEncoding = "utf-8" %> 
<style>#textStyle
   { font-family:宋体;font-size:36;color:blue 
   }
</style>
<HTML><body bgcolor=#ffccff> 
<p id="textStyle">
填写姓名(<%= (String)session.getAttribute("name")%>):<br>
<form action="example4_12_receive.jsp" method="post" name=form>
   <input type="text" id="textStyle"  name="name">
   <input type="submit" id="textStyle" value="确定"/>
</form>
</body></HTML>

example4_12_receive.jsp

<%@ page contentType="text/html" %>
<%@ page pageEncoding = "utf-8" %> 
<HTML><body bgcolor = #DDEEFF> 
<%  request.setCharacterEncoding("utf-8"); 
    String name=request.getParameter("name");
    if(name==null||name.length()==0) {
       response.sendRedirect("example4_12.jsp"); 
       String str =(String)session.getAttribute("name");//这个仍然会被执行。
       session.setAttribute("name","李四"+str);//这个仍然会被执行。
    }
%> 
<b>欢迎<%= name %>访问网页。
</body></HTML>

运行结果:

提交后:

4.3 session 对象(存用户信息)

我们可以简单的看下面这幅图:

当一个用户首先访问Wed服务目录中的一个JSP页面时候,Tomcat服务器产生一个session对象存取各种信息。这个session对象被分配了一个String类型的id,tomcat服务器同时把这个id发给客户端,存放在浏览器的Cookie中。

tips: 同一个用户在不同的Web服务目录中的session对象是互不相同。

tips: 同一个用户在相同的Web服务目录中不同页面的session对象是相同的。 

4.4 application 留言板

使用application内置对象,让其担任留言板的角色,同时设置留言板最多可以留言99999条,即application对象用1到99999之间的一个整数作为关键字(key)存放一条留言。

 example4_16.jsp:

<%@ page contentType="text/html" %>
<%@ page pageEncoding = "utf-8" %>
<HTML>
<style>
   #textStyle{
      font-family:宋体;font-size:18;color:blue 
   }
</style>
<body id='textStyle' bgcolor = #ffccff>
<form action='example4_16_pane.jsp' method='post' >
留言者:<input  type='text' name='peopleName' size = 40/>
<br>标题:<input  type='text' name='title' size = 42/>
<br>留言:<br>
<textArea name='contents' id='textStyle' rows='10' cols=36 wrap='physical'>
</textArea>
<br><input type='submit' id='textStyle' value='提交留言' name='submit'/>
</form>
<a href='example4_16_show.jsp'>查看留言</a>
<a href='example4_16_delete.jsp'>删除留言</a>
</body></HTML>

留言结果:

 

 


总结

例如:这就是第四章的JSP内置对象的总结。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值