JSP数据交互(一)


什么是JSP内置对象

JSP内置对象是 Web 容器创建的一组对象,不需要做任何声明就可以直接使用的对象。

例如:

<%

        int[ ] value = { 60, 70, 80 };

        for  (int i : value) {

                out.println(i);//没有进行声明和创建,但却可以使用out对象

        }

%>

JSP常用的内置对象

out

request

response

session

application

JSP内置对象request

request对象主要用于处理客户端请求

request对象常用方法

方法名称

 说明

String getParameter(String name)

根据表单组件名称获取提交数据

String[ ] getParameterValues(String name)

获取表单组件对应多个值时的请求数据 

void setCharacterEncoding(String charset)

指定每个请求的编码

RequestDispatcher getRequestDispatcher(String path)

返回一个RequestDispatcher对象,该对象的forward( )方法用于转发请求

例如:

学员注册页面

<form name="form1" method="post" action="reginfo.jsp">

    	<table  border="0" align="center">

     	 <tr>

        	<td>用户名:</td>

        	<td><input type="text" name="name"></td>

      	</tr>

<tr>

        <td height="19">密码:</td>

        <td height="19"><input type="password" name="pwd"></td>

</tr>

<tr>

        	<td>信息来源:</td>

        	<td>

<input type="checkbox" name="channel" value="报刊">报刊

        <input type="checkbox" name="channel" value="网络">网络<br/>

        <input type="checkbox" name="channel" value="朋友推荐">朋友推荐

        <input type="checkbox" name="channel" value="电视">电视

       	</td>

      	</tr>

      	<!-- 以下是提交、取消按钮 -->

      	<tr >

        	<td colspan="2" align="center" >

          	<input type="submit" name="Submit" value="提交">

          	<input type="reset" name="Reset" value="取消">

        	</td>

      	</tr>

    	</table>

  	</form>

信息读取显示页面

<body>

<%

    request.setCharacterEncoding("UTF-8");

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

    String pwd = request.getParameter("pwd");

    String[] channels = request.getParameterValues("channel");

%>

<div align="center">你输入的注册信息

<table border="0" align="center">

    <tr>

        <td width="80" height="20">用户名:</td>

        <td><%=name%></td>

    </tr>

    <tr>

        <td height="20">密码:</td>

        <td><%=pwd%></td>

    </tr>

    <tr>

        <td height="20">信息来源:</td>

        <td >

        <%

            if (channels != null) {

                for (String channel: channels) {

                    out.print(channel+" ");

                }

            }

        %>

        </td>

    </tr>

</table>

</div>

</body>

在页面设置支持中文字符的字符集,如:UTF-8

获取数据时解决中文乱码问题

POST方式提交数据时

<%   

        // 设置读取请求信息的字符编码为UTF-8

        request.setCharacterEncoding("UTF-8");

        // 读取用户名和密码

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

        String pwd = request.getParameter("pwd");

%>

GET方式提交数据时

<%   

        // 读取用户名和密码

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

        // 对请求数据进行字符编码

        name = new String(name.getBytes("ISO-8859-1"), "UTF-8");

%>

Tomcat目录结构\conf\server.xml中设置字符集

<Connector  port="8080"  protocol="HTTP/1.1"

connectionTimeout="20000"

redirectPort="8443"  URIEncoding="UTF-8"

/>

JSP内置对象response

response对象用于响应客户请求并向客户端输出信息

response的常用方法

方法名称

说明

void addCookie(Cookie cookie)

向客户端添加Cookie

void setContentType(String type)

设置HTTP响应的contentType类型

void setCharacterEncoding(String charset)

设置响应所采用的字符编码类型

void sendRedirect(String location)

将请求重新定位到一个新的URL

页面重定向

void sendRedirect(String location)

客户端将重新发送请求到指定的URL

例如:

实现登录验证,并在验证成功后跳转至欢迎页面

<%

request.setCharacterEncoding("UTF-8");

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

String pwd = request.getParameter("pwd");

if ("sa".equals(name) && "sa".equals(pwd))       

response.sendRedirect("welcome.jsp");//跳转到欢迎页面

%>

如何才能实现页面跳转后,请求信息不丢失呢?

使用转发取代重定向实现页面跳转

转发的作用

在服务器端,将请求发送给服务器上的其他资源,以共同完成一次请求的处理

转发的实现

RequestDispatcher对象的forward()方法

例如:

<%

RequestDispatcher rd = request.getRequestDispatcher("welcome.jsp");

rd.forward(request, response);

%>

注:在多个页面交互过程中请求中的数据可以共享

转发

重定向

转发是在服务器端发挥作用,将同一请求在服务器资源之间进行传递

重定向是在客户端发挥作用,通过发送一个新的请求实现页面跳转

客户端浏览器的地址栏不会显示转向后的地址

在地址栏中可以显示转向后的地址

什么是会话

一个会话就是在一段时间内,一个客户端与Web服务器的一连串相关的交互过程

JSP内置对象session

session对象常用方法

 方法名称

说明

String getId()

获取sessionid

void setMaxInactiveInterval(int interval)

设定session的非活动时间

int getMaxInactiveInterval()

获取session的有效非活动时间(以秒为单位)

void invalidate()

设置session对象失效

void setAttribute(String key, Object value)

key/value的形式保存对象值

Object getAttribute(String key)

通过key获取对象值 

void removeAttribute(String key)

session中删除指定名称(key)所对应的对象

session与窗口的关系

每个session对象都与一个浏览器窗口对应 ,重新开启一个浏览器窗口,可以重新创建一个session对象(不同版本浏览器可能有所差别)

通过超链接打开的新窗口,新窗口的session与其父窗口的session相同

使用session实现访问控制

在控制页面获取用户请求的登录信息进行验证

例如:

<%

if ("admin".equals(name) && "admin".equals(pwd)) {  // 如果是已注册用户

session.setAttribute("login", name);//在session中存放用户登录信息

 // 设置session过期时间

session.setMaxInactiveInterval(10*60);

request.getRequestDispatcher("admin.jsp").forward(request, response);

} else {

response.sendRedirect("index.jsp");

}

%>

 

在新闻发布系统新闻发布页面增加登录验证

例如:

<%

String login = (String) session.getAttribute("login");//如果session中不存在用户的登录信息,转入登录页面

if (login == null) {

response.sendRedirect("index.jsp");

return;

} %>

session对象的失效

语法:

手动设置失效:invalidate()

超时失效:通过setMaxInactiveInterval( )方法,单位是秒

例如:

<%

session.setAttribute("login","admin");

session.setMaxInactiveInterval(600);

response.sendRedirect("admin.jsp");

%>

通过设置项目的web.xmlTomcat目录下的/conf/web.xml文件,单位是分钟

<session-config>

    <session-timeout>10</session-timeout>

</session-config>


include指令

可以将一些共性的内容写入一个单独的文件中,然后通过include指令引用该文件

例如:

创建登录验证文件 loginControl.jsp

<%

String login = (String) session.getAttribute("login");

if (login == null) {

response.sendRedirect("index.jsp");

return;

} %>

在后台首页面中使用include指令引用登录验证文件

<%@  include file="loginControl.jsp" %>

猜你喜欢

转载自blog.csdn.net/weixin_40544803/article/details/80393207
今日推荐