out是内置对象,不用new来创建。
把饭盛到碗里,碗相当于程序中的缓冲区。
<%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>JSP内置对象</title>
</head>
<body>
<h1>out内置对象</h1>
<%
out.println("<h2>打马赛克<h2>");
//<h1> - <h6> 标签可定义标题。<h1> 定义最大的标题。<h6> 定义最小的标题。
out.println("打马赛克<br>");
out.println("打马赛克<br>");
out.println("打马赛克<br>");
out.println("打马赛克<br>");
%>
缓冲区大小:<%=out.getBufferSize() %><br>
缓冲区剩余大小:<%=out.getRemaining() %><br>
是否自动清空缓冲区:<%=out.isAutoFlush() %><br>
</body>
</html>
<%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>JSP内置对象</title>
</head>
<body>
<h1>out内置对象</h1>
<%
out.println("<h2>打马赛克<h2>");
//<h1> - <h6> 标签可定义标题。<h1> 定义最大的标题。<h6> 定义最小的标题。
out.println("打马赛克<br>");
out.println("打马赛克<br>");
out.flush(); //新增
out.println("打马赛克<br>");
out.println("打马赛克<br>");
%>
缓冲区大小:<%=out.getBufferSize() %><br>
缓冲区剩余大小:<%=out.getRemaining() %><br>
是否自动清空缓冲区:<%=out.isAutoFlush() %><br>
</body>
</html>
新增out.flush方法结果:
<%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>JSP内置对象</title>
</head>
<body>
<h1>out内置对象</h1>
<%
out.println("<h2>打马赛克<h2>");
//<h1> - <h6> 标签可定义标题。<h1> 定义最大的标题。<h6> 定义最小的标题。
out.println("打马赛克<br>");
out.println("打马赛克<br>");
out.flush();
out.clearBuffer(); //新增
out.println("打马赛克<br>");
out.println("打马赛克<br>");
%>
缓冲区大小:<%=out.getBufferSize() %><br>
缓冲区剩余大小:<%=out.getRemaining() %><br>
是否自动清空缓冲区:<%=out.isAutoFlush() %><br>
</body>
</html>
不会抛出异常。
get方式:
新建两个jsp
<%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h1>用户登录</h1>
<hr>
<form action="dologin.jsp" name="loginForm" method="get">
<table>
<tr>
<td>用户名:</td>
<td><input type="text" name="username"/><td/>
</tr>
<tr>
<td>密码:</td>
<td><input type="password" name="password"/><td/>
</tr>
<tr>
<td colspan="2"><input type="submit" value="登录"/><td/>
</tr>
</table>
</body>
</html>
<%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h1>登陆成功</h1>
<hr>
</body>
</html>
get方式在地址栏上传递用户的信息,在url地址当做可以看到用户提交的信息,安全性较差。
post方式把表单的信息封装到HTML HEADER数据包里。
<%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h1>用户登录</h1>
<hr>
<form action="dologin.jsp" name="loginForm" method="post"> \\get改为post
<table>
<tr>
<td>用户名:</td>
<td><input type="text" name="username"/><td/>
</tr>
<tr>
<td>密码:</td>
<td><input type="password" name="password"/><td/>
</tr>
<tr>
<td colspan="2"><input type="submit" value="登录"/><td/>
</tr>
</table>
</body>
</html>
地址栏上没有显示用户信息。
通过表单传递用户名:
<%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h1>用户注册</h1>
<hr>
<form name="regForm" action="request.jsp" method="post">
<table>
<tr>
<td>用户名:</td>
<td><input type="text" name="username"/><td/>
</tr>
<tr>
<td>爱好:</td>
<td>
<input type="checkbox" name="hobby" value="reading"/>读书
<input type="checkbox" name="hobby" value="singing"/>唱歌
<input type="checkbox" name="hobby" value="swimming"/>游泳
<input type="checkbox" name="hobby" value="cooking"/>做饭
</td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="登录"/><td/>
</tr>
</table>
</form>
</body>
</html>
<%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h1>request内置对象</h1>
用户名:<%=request.getParameter("username") %>
爱好:<%
String[] hobby=request.getParameterValues("hobby");
for(int i=0;i<hobby.length;i++){
out.println(hobby[i]+" ");
}
%>
</body>
</html>
解决中文乱码问题:
<%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h1>request内置对象</h1>
<%
request.setCharacterEncoding("utf-8");//解决中文乱码问题
%>
用户名:<%=request.getParameter("username") %>
爱好:<%
String[] hobby=request.getParameterValues("hobby");
for(int i=0;i<hobby.length;i++){
out.println(hobby[i]+" ");
}
%>
</body>
</html>
通过url地址、参数传递用户名:
<%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h1>request内置对象</h1>
<%
request.setCharacterEncoding("utf-8");//解决中文乱码问题
%>
用户名:<%=request.getParameter("username") %>
爱好:<%
if(request.getParameterValues("hobby")!=null)
{
String[] hobby=request.getParameterValues("hobby");
for(int i=0;i<hobby.length;i++){
out.println(hobby[i]+" ");
}
}
%>
</body>
</html>
<%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h1>用户注册</h1>
<hr>
<form name="regForm" action="request.jsp" method="post">
<table>
<tr>
<td>用户名:</td>
<td><input type="text" name="username"/><td/>
</tr>
<tr>
<td>爱好:</td>
<td>
<input type="checkbox" name="hobby" value="reading"/>读书
<input type="checkbox" name="hobby" value="singing"/>唱歌
<input type="checkbox" name="hobby" value="swimming"/>游泳
<input type="checkbox" name="hobby" value="cooking"/>做饭
</td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="提交"/><td/>
</tr>
</table>
</form>
<br>
<br>
<a href="request.jsp?username=lisi">测试url传参数</a>
</body>
</html>
<a> 标签定义超链接,用于从一张页面链接到另一张页面。
通过url传参要用户名和爱好一起传递,否则会空指针异常。
解决通过url传递参数的中文乱码问题:
重启tomcat服务器。
保存一个名为password的密码
<%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h1>request内置对象</h1>
<%
request.setCharacterEncoding("utf-8");//解决中文乱码问题,无法解决URL传递中文出现的乱码问题
request.setAttribute("password","123456");
%>
用户名:<%=request.getParameter("username") %><br>
爱好:<%
if(request.getParameterValues("hobby")!=null)
{
String[] hobby=request.getParameterValues("hobby");
for(int i=0;i<hobby.length;i++){
out.println(hobby[i]+" ");
}
}
%><br>
密码:<%=request.getAttribute("password") %><br>
</body>
</html>
常用方法:
<%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h1>request内置对象</h1>
<%
request.setCharacterEncoding("utf-8");//解决中文乱码问题,无法解决URL传递中文出现的乱码问题
request.setAttribute("password","123456");
%>
用户名:<%=request.getParameter("username") %><br>
爱好:<%
if(request.getParameterValues("hobby")!=null)
{
String[] hobby=request.getParameterValues("hobby");
for(int i=0;i<hobby.length;i++){
out.println(hobby[i]+" ");
}
}
%><br>
密码:<%=request.getAttribute("password") %><br>
请求体的MIME类型:<%=request.getContentType() %><br>
协议类型及版本号:<%=request.getProtocol() %><br>
服务器主机名:<%=request.getServerName() %><br>
服务器端口号:<%=request.getServerPort() %><br>
服务器文件长度:<%=request.getContentLength() %><br>
请求客户端的IP地址:<%=request.getRemoteAddr() %><br>
请求的真实路径:<%=request.getRealPath("request.jsp") %><br>
请求的上下文路径:<%=request.getContextPath() %><br>
</body>
</html>
本机IP地址为动态路径 。
新建response.jsp
<%@ page language="java" import="java.util.*,java.io.*" contentType="text/html; charset=utf-8"%>
<%
response.setContentType("text/html;charset=utf-8");//设置响应的MIME类型
out.println("<h1>response内置对象</h1>");
out.println("<hr>");
PrintWriter outer=response.getWriter();//获得输出对象
outer.println("大家好,我是response对象生成的输出流outer对象");
%>
说明response对象获得的writer输出流对象的类型PrintWriter的打印输出时总是提前于内置out对象。
把out输出内容提前:
<%@ page language="java" import="java.util.*,java.io.*" contentType="text/html; charset=utf-8"%>
<%
response.setContentType("text/html;charset=utf-8");//设置响应的MIME类型
out.println("<h1>response内置对象</h1>");
out.println("<hr>");
out.flush();//flush方法强制清空缓冲区,在清空的同时强制把缓冲区的内容写到浏览器上
PrintWriter outer=response.getWriter();//获得输出对象
outer.println("大家好,我是response对象生成的输出流outer对象");
%>
请求重定向:让客户端定义一个新的请求,让这个请求重定向到登录界面(新的资源)。
<%@ page language="java" import="java.util.*,java.io.*" contentType="text/html; charset=utf-8"%>
<%
response.setContentType("text/html;charset=utf-8");//设置响应的MIME类型
out.println("<h1>response内置对象</h1>");
out.println("<hr>");
//out.flush();//flush方法强制清空缓冲区,在清空的同时强制把缓冲区的内容写到浏览器上
PrintWriter outer=response.getWriter();//获得输出对象
outer.println("大家好,我是response对象生成的输出流outer对象");
response.sendRedirect("reg.jsp");//请求重定向
%>
访问response.jsp界面:
请求重定向:
response.jsp:
<%@ page language="java" import="java.util.*,java.io.*" contentType="text/html; charset=utf-8"%>
<%
response.setContentType("text/html;charset=utf-8");//设置响应的MIME类型
out.println("<h1>response内置对象</h1>");
out.println("<hr>");
//out.flush();//flush方法强制清空缓冲区,在清空的同时强制把缓冲区的内容写到浏览器上
PrintWriter outer=response.getWriter();//获得输出对象
outer.println("大家好,我是response对象生成的输出流outer对象");
//response.sendRedirect("reg.jsp");//请求重定向
response.sendRedirect("request.jsp");//请求重定向
%>
reg.jsp:
<%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h1>用户注册</h1>
<hr>
<form name="regForm" action="response.jsp" method="post">
<table>
<tr>
<td>用户名:</td>
<td><input type="text" name="username"/><td/>
</tr>
<tr>
<td>爱好:</td>
<td>
<input type="checkbox" name="hobby" value="reading"/>读书
<input type="checkbox" name="hobby" value="singing"/>唱歌
<input type="checkbox" name="hobby" value="swimming"/>游泳
<input type="checkbox" name="hobby" value="cooking"/>做饭
</td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="提交"/><td/>
</tr>
</table>
</form>
<br>
<br>
<a href="request.jsp?username=星">测试url传参数</a>
</body>
</html>
输出结果:
response.jsp请求重定向到request.jsp, 因为request.jsp是一个新的请求,所以上一次请求中提交的用户名和爱好这些参数无法保存(前一次请求不会保存,地址栏url发生改变)。
请求转发:
<%@ page language="java" import="java.util.*,java.io.*" contentType="text/html; charset=utf-8"%>
<%
response.setContentType("text/html;charset=utf-8");//设置响应的MIME类型
out.println("<h1>response内置对象</h1>");
out.println("<hr>");
//out.flush();//flush方法强制清空缓冲区,在清空的同时强制把缓冲区的内容写到浏览器上
PrintWriter outer=response.getWriter();//获得输出对象
outer.println("大家好,我是response对象生成的输出流outer对象");
//response.sendRedirect("reg.jsp");//请求重定向
//response.sendRedirect("request.jsp");//请求重定向
request.getRequestDispatcher("request.jsp").forward(request,response);//请求转发
%>
输出结果:
地址栏未发生变化。