JSP内置对象

来源:互联网 发布:mac air摄像头打不开 编辑:程序博客网 时间:2024/06/11 04:57

一、out对象:

<%@ 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><%out.println("fdasfa");out.print("fjakl");out.newLine();out.flush();//out.clearBuffer();out.clear();out.println("<br/>");out.println("当前缓冲区大小:"+out.getBufferSize());out.println("<br/>");out.println("当前缓冲区剩余字节数目"+out.getRemaining());%></body></html>

二、request对象:

(1)

<%@ 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>request对象实例</title></head><body><form action="" method=post><input type="text" name="username"/><input type="submit" value="提交"/></form><br/>请求方法名:<%=request.getMethod() %><br/>请求的资源:<%=request.getRequestURI() %><br/>请求的协议:<%=request.getProtocol()%><br/>请求的服务器IP<%=request.getServerName() %><br/>请求的服务器端口:<%=request.getServerPort() %><br/>客户端的IP地址:<%=request.getRemoteAddr() %><br/>客户端的主机名:<%=request.getRemoteHost() %><br/>表单提交过来的值:<%=request.getParameter("username") %><br/></body></html>

(2)三个页面文件,分别为register.jsp。do_register.jsp用来处理register.jsp提交的数据,welcome.jsp用来显示数据。用getParameter方法获取用户输入信息,用setAttribute和getAttribute这两个方法在用于在web组件间共享信息:

register.jsp代码:

<%@ 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><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="pyhton"/>python<input type="checkbox" name="skills" value="C#"/>C#<input type="checkbox" name="skills" value="C++"/>C++<br/><input type="submit" value="提交">  <input type="reset" value="重置"></form></body></html>


do_register.jsp页面代码:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"    pageEncoding="ISO-8859-1"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><%String username=request.getParameter("username");String skills="";String[] skillArr=request.getParameterValues("skills");if(skillArr!=null && skillArr.length>0){for(String skill: skillArr){skills=skills+skill+" ";}}request.setAttribute("username", username);request.setAttribute("skills", skills);%><jsp:forward page="welcome.jsp"></jsp:forward>  

welc.jsp页面代码如下:

<%@ 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>信息展示页面<br/><br/>用户名:<%=request.getAttribute("username") %><br/>技能:<%=request.getAttribute("skills") %></body></html>



0 0