Web程序的请求响应模式
用户发送请求(request)
服务器给用户相响应(response)
JSP内置对象
什么是缓冲区
缓冲区:Buffer 所谓缓冲区就是内存的一块区域用来保存临时数据
IO输出最原始的就是一个字节一个字节的输出 效率太差了
out对象
<h1>out内置对象</h1>
<%
out.println("xixixixi<br>");
out.println("hahahah<br>");
out.println("hehehe<br>");
out.println("heihei<br>");
%>
缓冲区大小:<%=out.getBufferSize() %> byte<br>
缓冲区剩余大小:<%=out.getRemaining() %> byte<br>
是否自动清空缓冲区:<%=out.isAutoFlush() %><br>
执行flush后是不可以支持clear的要记住这个 会抛出异常
get和post提交方式的区别
使用get提交的话那么在地址栏会显示出用户来
用post提交的话在地址栏不会显示出用户来
login.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 = "post">
<table>
<tr>
<td>用户名:</td>
<td><input type = "text" name = "username"/></td>
</tr>
<tr>
<td>密码</td>
<td><input type = "password" name = "username"/></td>
</tr>
<tr>
<td colspan = "2"><input type = "submit" value = "登录"></td>
</tr>
</table>
</form>
</body>
</html>
dologin.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>
</body>
</html>
显示出这样子的情况拉
request对象
用户注册
request.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>Request内置对象</h1>
<%
request.setCharacterEncoding("utf-8"); //解决中文乱码 但是无法解决url传参中文乱码问题
request.setAttribute("password", "123456");
%>
用户名:<%= request.getParameter("username")%><br>
爱好:<%
if(request.getParameterValues("favorite") != null)
{
String[] favorite = request.getParameterValues("favorite");
for(int i = 0 ; i < favorite.length; i++)
out.println(favorite[i] + " ");
}
%>
密码:<%=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>
rigister.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 = "registerForm" 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 = "favorite" value = "read">读书 <input type = "checkbox" name = "favorite" value = "music">音乐 <input type = "checkbox" name = "favorite" value = "moive">电影 <input type = "checkbox" name = "favorite" value = "sport">运动</td> </tr> <tr> <td colspan = "2"><input type = "submit" value = "登录"></td> </tr> </table> <br><br> <a href = "request.jsp?username=李四">测试URL传参数</a> </form> </body> </html>
response对象
<%@ page language="java" import = "java.util.*,java.io.*" contentType="text/html; charset=utf-8"%> <% response.setContentType("text/html;charset=utf-8"); out.println("<h1>response内置对象</h1>"); out.println("<hr>"); out.flush(); PrintWriter outer = response.getWriter(); //pw提前与内置的out对象 outer.println("大家好,我是response对象生成的输出流outer对象"); response.sendRedirect("request.jsp");//请求重定向 %>
请求转发与请求重定向
<%@ page language="java" import = "java.util.*,java.io.*" contentType="text/html; charset=utf-8"%> <% response.setContentType("text/html;charset=utf-8"); out.println("<h1>response内置对象</h1>"); out.println("<hr>"); //out.flush(); PrintWriter outer = response.getWriter(); //pw提前与内置的out对象 outer.println("大家好,我是response对象生成的输出流outer对象"); //请求重定向 //response.sendRedirect("request.jsp"); //请求转发 //request.getRequestDispatcher("request.jsp").forward(request, response); %>