JSP中Request对象的使用
概述:request对象主要用于接收客户端发送来的请求信息,客户端的请求信息被封装在request对象中,通过它可以了解到客户的需求,然后做出响应。主要封装了用户提交的信息,在用户注册中使用的较多,
实例1:设定以及获取值
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>展示信息</title>
</head>
<body>
<%
//设置值
request.setAttribute("username", "小红");
request.setAttribute("pwd", "12345678");
%>
<!-- 取值 -->
用户名:<%=request.getAttribute("username")%><br>
密码 :<%=request.getAttribute("pwd")%>
</body>
</html>
实例2:简单的注册以及显示注册信息
注册界面(html)编写
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>注册界面</title>
</head>
<body>
<form action="successlogin.jsp" method="post">
<h1 align="center">欢迎来到注册页面</h1>
<table align="center" border="1">
<tr>
<td>用户名:</td>
<td><input type="text" name="username"></td>
</tr>
<tr>
<td>密码:</td>
<td><input type="password" name="pwd"></td>
</tr>
<tr>
<td>性别:</td>
<td><input type="radio" value="男" name="gender"
checked="checked">男 <input type="radio" value="女"
name="gender">女</td>
</tr>
<tr>
<td>爱好:</td>
<td><input type="checkbox" value="钓鱼" name="hobbies"
checked="checked">钓鱼 <input type="checkbox" value="跑步"
name="hobbies">跑步 <input type="checkbox" value="喝茶"
name="hobbies">喝茶 <input type="checkbox" value="看书"
name="hobbies" checked="checked"> 看书</td>
</tr>
<tr>
<td>学历:</td>
<td><select name="education">
<option value="小学">小学</option>
<option value="初中">初中</option>
<option value="高中">高中</option>
<option value="大学">大学</option>
</select></td>
</tr>
<tr>
<td>备注:</td>
<td><textarea name="remark" rows="3" cols="13"></textarea></td>
</tr>
<tr>
<td colspan="2" align="center"><button type="submit">注册</button>
<button type="reset">重置</button></td>
</tr>
</table>
</form>
</body>
</html>
展示界面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>信息展示</title>
</head>
<body>
<h1>注册成功</h1>
<%
request.setCharacterEncoding("utf-8");
//获取参数
String username=request.getParameter("username");
String pwd=request.getParameter("pwd");
String gender=request.getParameter("gender");
String remark=request.getParameter("remark");
String education=request.getParameter("education");
String [] hobbies=request.getParameterValues("hobbies");//数组保存兴趣信息
%>
用户名:<%=username %><br>
密码:<%=pwd %><br>
性别:<%=gender %><br>
<%
out.print("爱好:");
for(int i=0;i<hobbies.length;i++){
out.print(hobbies[i]+" ");//循环输出兴趣
}
%><br>
学历:<%=education %><br>
备注:<%=remark %>
</body>
</html>
结果:
输入、输出对象
out对象用于向客户端输出数据,out对象提供了输出以及处理缓冲区问题的方法
例子
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>向界面输出字符串</title>
</head>
<body>
<%
out.print("hello jsp");
out.print("</br>");
//</br>不能直接写在java代码框里
out.println("你好啊");//println代表换行符,由于浏览器的原因可能不会换行
%>
缓冲区大小:<%=out.getBufferSize()%></br>
缓冲区剩余大小:<%=out.getRemaining()%></br>
是否自动关机清除缓冲区:<%=out.isAutoFlush()%></br>
</body>
</html>