JSP的第一个对象为out对象 为向客户端输出信息
//out表示向客户端输出各种数据 //对输出缓冲区进行管理 可以通过page属性来改变缓冲区 out.println("操"); out.print("cao"); out.newLine(); out.println("<br/>"); out.print("cao");
//强制刷新缓冲区的数据 out.flush();
//清空缓冲区中的数据 out.clearBuffer(); out.clear();
out.println("获取当前的缓冲区大小:" + out.getBufferSize()); out.println("当前缓冲区剩余字节数目:" + out.getRemaining());
request对象 用于封装客户端的请求
请求的方法名 <%= request.getMethod() %> <br/> 请求的url <%= request.getRequestURI() %> 获取请求的协议<%=request.getProtocol() %> 获取服务器的IP<%=request.getServerName() %> 请求的服务器端口<%=request.getServerPort() %> 客户端的IP地址<%= request.getRemoteAddr() %> 客户端的主机名<%= request.getRemoteHost() %> <!--获取表单数据 参数参数跟表单名称一致-->
首先创建一个提交的表单
<pre name="code" class="java"><form action = "do_register.jsp" method = "post"> 用户名 <input type = "text" name = "userName"/></br> 技能 <input type = "checkbox" name = "skills" value = "java">java <input type = "checkbox" name = "skills" value = "OC">OC <input type = "checkbox" name = "skills" value = "Swift">Swift </br> <input type = "submit" value = "提交"/> <input type = "reset" value = "重置"/> </form>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <% //getParameter方法获得用户名和技能数组 String userName = request.getParameter("userName"); String skills = ""; String skillArr[] = request.getParameterValues("skills"); for(int i = 0; i < skillArr.length; i++){ skills += skillArr[i]; } //setAttribute方法 request.setAttribute("userName",userName); request.setAttribute("skills",skills); %> <!-- forward跳转指令 跳转到新的界面 --> <jsp:forward page="welcome.jsp"></jsp:forward>这里是处理注册是否成功,成功后跳转到welcome.jsp文件
用getParameter来获取
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="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=UTF-8"> <title>信息展示</title> </head> <body> 用户名<%= request.getParameter("userName")%> 技能<%=request.getParameter("skills")%> </body> </html>这里要区分一下 getParameter来处理客户端的请求 而setAttribute来表示webPages之间的传值
response方法,用于返回客户端的请求
<% //设置头部的信息 设置网页数据不缓存 response.setHeader("cache-Control","no-cache"); //设置每两秒刷新一次 response.setIntHeader("Refresh",2); out.println("date is " + new java.util.Date().toString()); %>
//实现页面的跳转 response.sendRedirect("http://www.baidu.com");session对象 用于设置会话 在浏览器向服务器请求时,有唯一的一个sessionID 服务器会进行判定 相同的才给予响应
<%@page import="java.util.Date"%> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="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=UTF-8"> <title>Session</title> </head> <body> session 的唯一标示符 32位长字符<%=session.getId() %> session的创建时间<%= new java.util.Date( session.getCreationTime()).toString() %> session的最后访问时间<%=session.getLastAccessedTime() %> session的失效时间 <%=session.getMaxInactiveInterval()%> </body> </html>
实现一个登陆验证 首先在Login创建一个表单
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="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=UTF-8"> <title>Insert title here</title> </head> <body> <form acton = "do_login.jsp" method = "post"> userName:<input type = "text" name = "userName"/> password:<input type = "password" name = "password"/> <input type = "submit" value = "submit"> <input type = "reset" value = "reset"> </form> </body> </html>随后在验证表单do_login.jsp中进行登陆的验证
session中配置属性
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <% String userName = request.getParameter("userName"); String password = request.getParameter("password"); //验证是否登录成功 if(userName != null && password != null){ //会话中配置java对象 session.setAttribute("userName",userName); //随后刷新跳转到欢迎界面 response.setHeader("refresh","2;URL=welcome.jsp"); } %>随后在欢迎界面根据session获得的不同值来输出不同的值
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="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=UTF-8"> <title>欢迎界面</title> </head> <body> <% if(session.getAttribute("userName") != null) { %> 欢迎 <%=session.getAttribute("userName") %> <a href = "logout.jsp">注销</a> </br> <%else { %> 请先登录 <a href = "login.jsp">登录</a> <%} %> <%if(session.isNew()) {%> <br/> 欢迎新用户 <%}else{ %> 欢迎老用户 <%} %> </y> </html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <% //退出的代码 session.invalidate(); response.setHeader("refresh","2;URL=welcome.jsp"); %>
application属性 用于设置全局的变量
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="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=UTF-8"> <title>application</title> </head> <body> 服务器信息<%=application.getServerInfo() %></br> 应用名称<%=application.getServletContextName()%><br/> 主机名称<%=application.getVirtualServerName() %></br> </body> </html>可以做一个简单的计数器 如果页面是第一次访问的话 就输出值,随后保存到application中,如果是第二次开始访问的,那么就从application中取出值,随后进行++和存值得操作
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="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=UTF-8"> <title>页面访问计数器</title> </head> <body> <% Object obj = application.getAttribute("counter"); //判断是否为空 空为第一次 if(obj == null){ application.setAttribute("counter",new Integer(1)); out.println("该页面被访问了1次<br/>"); }else{ int counterValue = Integer.parseInt(obj.toString()); counterValue++; out.println("该页面被访问了" + counterValue + "次<br/>"); application.setAttribute("counter",counterValue); } %> </body> </html>page属性相当于java中的this指针,用于指向本页面,其是继承自object类的
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="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=UTF-8"> <title>Insert title here</title> </head> <body> page对象的字符串<%=page.toString() %> 当前的object类<%=page.getClass() %> 当前的hashCode值<%=page.hashCode() %> 继承自object类 </body> </html>异常类必须配置error_page属性
在throw_error.jsp中人为的指定一个错误,随后进行抛出,指定错误界面为handle_error.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@page errorPage="handle_error.jsp" %> <!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=UTF-8"> <title>抛出异常类</title> </head> <body> <% int a = 10; int b = a / 0; %> </body> </html>随后在handle_error.jsp中 指定isErrorPage = true 随后输出一些异常的信息即可
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@page isErrorPage="true" %> <!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=UTF-8"> <title>异常处理</title> </head> <body> 异常描述信息 <% out.println(exception.getMessage()); %> exception对象的字符串描述 <%=exception.toString() %> 异常堆栈信息 <%=exception.printStackTrace() %> </body> </html>page_Context可以说是一个集大成者,他可以转换为out 但是系统默认已经带有一个out了,所以自己命名的话只能命名为其他名称
<% JspWriter myOut = pageContext.getOut(); myOut.println("hello world");也可以在session 中设置值,随后进行取值和输出
pageContext.setAttribute("dakim","cao",pageContext.SESSION_SCOPE); String value = session.getAttribute("dakim").toString(); out.println(value);
http://blog.csdn.net/yenange/article/details/5949518