Java Servlet 基础使用(二)

189 阅读2分钟

JSP

  jsp初步理解
  1.jsp是以.jsp结尾的页面文件
  2.jsp是在页面编写java代码
  3.jsp一般用于动态生成网页
  4.jsp本质上java文件,jsp本质上是servlet
  5.jsp页面-->jsp.java文件(tomcat完成)-->编译 jsp.class-->
    执行servlet-->响应浏览器,打印出整个页面

  jsp的组成部分
  1.脚本指令 <%@ %> 声明一些页面的元素,page,includ,taglib
    page 声明相应打开的方式和页面编码 当前使用的语言
    includ
    taglib
  2.脚本声明 <%! int i = 1; %> 相当于给jsp(servlet)定义一个全局变量
  3.脚本表达式 <%=变量 %> 编译到_jspService, out.print(变量) 相当于在页面打印一个变量
  4.脚本片段 <% java代码 %>  编译到_jspService,页面所有代码生成的方法

  jsp指令
  page指令 language="java" import="" 页面使用的java的类的引用
  pageEncoding="utf-8"  contentType="text/html;charset=UTF-8" 设定页面的编码
  errorPage 指定服务器异常的错误页面

  include指令页面引用其他网页  <%@ include file="top.jsp" %>

  taglib指令页面引用第三方的库 <%@ taglib  prefix=""%>

  jsp的4大容器,9大内置对象

  4大容器
  application 上下文容器
  session 会话容器

  request 请求容器(重点)
  一般页面的jsp文件放置在WEB-INF 下方 因为这个目录是web项目的隐藏目录不能直接访问
  WEB-INF下的页面只能通过后台servlet转发访问
  request共享 通过request带后台的数据到前台去

  page 当前页面

  9大内置对象
  application,session,request,page (4大容器)
  config,out,exception,response
  pageContext 这个页面的工具类

  out  ==  response.getWriter() 打印输出流 一般用于页面输出内容(字符串)
  exception 页面的错误信息 isErrorPage=true
  response 页面的相应信息

  pageContext
  1.获取其他的8大对象
  2.往4大容器中设值,或者获取4大容器中的值
    设值 pageContext.setAttribute(key,value,相应容器的常量)
    取值 pageContext.getAttribute(key,相应容器的常量)

  jsp的动作标签
  防止过多的java代码
  jsp:include 和jsp的指令 include效果一样
  区别 jsp:include最后会编译成多个java文件
       jsp的指令 include 最后只会编译成一个java文件
  jsp:forward 转发 request.getRequestDispatcher("").forward(request,response);

EL表达式

<%-- 用传统的jsp的表达式取值 --%>
	<%=request.getAttribute("msg") %>
	-------------------------------<br/>
	<!-- 用el表达式来获取msg的值 -->
	${requestScope.msg}<br/>
	${msg}<br/>
	${requestScope.succ}<br/>
	${succ}<br/>
	
${requestScope}<br/>

<!-- el表达式用11个内置对象
	requestScope   取得 request.setAttribute方法设置的值
	sessionScope    取得session.setAttribute方法设置的值
	applicationScope  取得servletContext/application.setAttribute方法设置的值
	pageScope  取得pageContext.setAttribute方法设置的值
	
 -->
 
 
 <!-- 取得值是实体对象 -->
 <%-- 用传统的jsp的表达式取值 --%>
 <%-- <%=((Student)request.getAttribute("student")).getSno() %><br/> --%>
 <%=((Student)request.getAttribute("student")).getSname() %><br/>
 <br/>
 <input type="text" name="sex" value="<%=CommonMethod.isNotNull(((Student)request.getAttribute("student")).getSex())%>"><br/>
 <%=((Student)request.getAttribute("student")).getAge() %><br/>
 
 <%-- 用el表达式获取的是实体对象的属性值   ${属性key.实体类的属性名称} ${requestScope.student.sno}  实际上调用的是getSno方法取值--%>
 
 ${student.sno}
 ${student.sname}
 ${requestScope.student.sno}<br/>
 ${requestScope.student.sname}<br/>
 <input type="text" name="sex" value="${requestScope.student.sex}"><br/>
 ${requestScope.student.sex}<br/>  <!-- 如果值为null 自动转换成空字符串 -->
 ${requestScope.student.age}<br/>
 
 <!-- 获取map的值 -->
 ${requestScope.map.phone}<br/>
 ${requestScope.map.companyphone}<br/>
 
 <!-- 获取集合的值
 	  目前没办法做到循环操作,只能根据下标来获取对应的值
  -->
 ${requestScope}<br/>
 ${requestScope.stulist}<br/>
 ${requestScope.stulist[2]}<br/>
 ${requestScope.stulist[2].sno}<br/>
 ${requestScope.stulist[2].sname}<br/>
 ${requestScope.stulist[2].age}<br/>
 ${requestScope.stulist[2].sex}<br/>
 
 <!-- 取得数组 -->
 ${arr}<br/>
 ${arr[0]}<br/>
 ${arr[1]}<br/>
 ${arr[2]}<br/>
<%
	request.setAttribute("msgrequest", "request的值");
	pageContext.setAttribute("msgpagecontext", "page的值");
	session.setAttribute("msgsession", "session的值");
	application.setAttribute("msgapplication", "application的值");
	
	//request.setAttribute("user", "request的值user");
	//pageContext.setAttribute("user", "page的值user");
	//session.setAttribute("user", "session的值user");
	application.setAttribute("user", "application的值user");
%>

获取的是request的值:${requestScope.msgrequest}<br/>
获取的是pageContext的值:${pageScope.msgpagecontext}<br/>
获取的是session的值:${sessionScope.msgsession}<br/>
获取的是application的值:${applicationScope.msgapplication}<br/>

------------------<br/>
获取的是request的值:${msgrequest}<br/>
获取的是pageContext的值:${msgpagecontext}<br/>
获取的是session的值:${msgsession}<br/>
获取的是application的值:${msgapplication}<br/>


<hr/>
<!--什么时候可以省略xxxScope内置对象   如果你往这几个对象设值得属性名称不同的名称时 可以省略xxxxScope内置对象
    什么时候不能省略xxxScope内置对象  如果你往这几个对象设值得属性名称相同的名称时  不可以省略xxxxScope内置对象 -->
获取的是request的值:${requestScope.user}<br/>
获取的是pageContext的值:${pageScope.user}<br/>
获取的是session的值:${sessionScope.user}<br/>
获取的是application的值:${applicationScope.user}<br/>


------------------
获取的是request的值:${user}
获取的是pageContext的值:${user}
获取的是session的值:${user}
获取的是application的值:${user}

<!-- 如果你往这四个对象中设置了相同的属性名称后,取值顺序: pageScope > requestScope> sessionScope > applicationScope -->
 <table width="300" border="1">
 	<tr>
 		<td>el表达式</td>
 		<td>el表达式结果</td>
 	</tr>
 	<tr>
 		<!-- el表达式的运算都是在el表达式里面   -->
 		<td>\${age1+age2}</td>
 		<td>${age1+age2}</td>
 	</tr>
 	<tr>
 		<!-- el表达式的运算都是在el表达式里面   -->
 		<td>\${age1-age2}</td>
 		<td>${age1-age2}</td>
 	</tr>
 	<tr>
 		<!-- el表达式的运算都是在el表达式里面   -->
 		<td>\${age1*age2}</td>
 		<td>${age1*age2}</td>
 	</tr>
 	<tr>
 		<!-- el表达式的运算都是在el表达式里面   -->
 		<td>\${age1/age2}</td>
 		<td>${age1/age2}</td>
 	</tr>
 	<tr>
 		<!-- el表达式的运算都是在el表达式里面   -->
 		<td>\${age1%age2}</td>
 		<td>${age1%age2}</td>
 	</tr>
 	
 	<tr>
 		<!-- el表达式的运算都是在el表达式里面   -->
 		<td>\${age1 == age2}</td>
 		<td>${age1 == age2}</td>
 	</tr>
 	
 	<tr>
 		<!-- el表达式的运算都是在el表达式里面   -->
 		<td>\${age1 eq age2}</td>
 		<td>${age1 eq age2}</td>
 	</tr>
 	
 	<tr>
 		<!-- el表达式的运算都是在el表达式里面   -->
 		<td>\${age1 != age2}</td>
 		<td>${age1 != age2}</td>
 	</tr>
 	
 	<tr>
 		<!-- el表达式的运算都是在el表达式里面   -->
 		<td>\${age1 ne age2}</td>
 		<td>${age1 ne age2}</td>
 	</tr>
 	
 	<!-- 逻辑运算符和条件运算符 最终的结果都是boolean类型  -->
 	
 	<tr>
 		<!-- el表达式的运算都是在el表达式里面   -->
 		<td>\${age1 >= age2 && age1!=age2}</td>
 		<td>${age1 >= age2 && age1!=age2}</td>
 	</tr>
 	
 	<tr>
 		<!-- el表达式的运算都是在el表达式里面   -->
 		<td>\${age1 >= age2 and age1==age2}</td>
 		<td>${age1 >= age2 and age1==age2}</td>
 	</tr>
 	
 	<tr>
 		<!-- el表达式的运算都是在el表达式里面   -->
 		<td>\${age1 >= age2 || age1==age2}</td>
 		<td>${age1 >= age2 ||  age1==age2}</td>
 	</tr>
 	
 	<tr>
 		<!-- el表达式的运算都是在el表达式里面   -->
 		<td>\${age1 >= age2 or age1==age2}</td>
 		<td>${age1 >= age2 or  age1==age2}</td>
 	</tr>
 	
 	<tr>
 		<!-- el表达式的运算都是在el表达式里面   -->
 		<td>\${!(age1 == age2)}</td>
 		<td>${!(age1 == age2)}</td>
 	</tr>
 	
 	<tr>
 		<!-- el表达式的运算都是在el表达式里面   -->
 		<td>\${not (age1 == age2)}</td>
 		<td>${not (age1 == age2)}</td>
 	</tr>
 	
 	<tr>
 		<!-- 三目运算符  a>b? true结果: false结果-->
 		<td>\${age1>age2?age1:age2}</td>
 		<td>${age1>age2?age1:age2}</td>
 	</tr>
 	
 	<!-- empty运算符  前缀运算符 -->
 	<tr>
 		<td>\${not empty msg}</td>
 		<td>${not empty msg}</td>
 	</tr>
 	
 	<tr>
 		<td>\${empty msg}</td>
 		<td>${empty msg}</td>
 	</tr>
 取得登录的请求参数:
<%=request.getParameter("username") %><br/>
<%=request.getParameter("password") %><br/>

<%=request.getParameterValues("enjoy") %><br/>

<!-- 可以使用el表达式来获取请求参数   内置对象 param paramValues -->
${param}<br/>
${param.username}<br/>
${param.password}<br/>
${paramValues}<br/>
${paramValues.enjoy}<br/>
${paramValues.enjoy[0]}<br/>
${paramValues.enjoy[1]}<br/>
${paramValues.enjoy[2]}<br/>
${paramValues.enjoy[3]}<br/>

<hr/>
<!-- header /headerValues  取得请求头中的内容 -->
<%=request.getHeader("Accept")%><br/>
<%=request.getHeader("Accept-Encoding")%><br/>

<!--用el表达式获取请求头中的内容   map对象.key  -->
${header}<br/>
${header.accept}<br/>
<!-- 如果实体对象或者map中的key或者属性名称如果带有特殊字符的话 如:-   map对象['key']/ map对象["key"]-->
${header['accept-language']}<br/>

${headerValues}
<hr/>
${headerValues['accept-language'][0]}

<hr/>
<!-- 获取cookie信息 -->
<%
	Cookie cookies[] = request.getCookies();
	if(cookies !=null && cookies.length>0){
		for(Cookie c:cookies){
%>
			<%=c.getName() %>
			=
			<%=c.getValue() %><br/>
<%
		}
	}
%>

<hr/>
<!-- 通过内置对象获取cookie信息 -->
${cookie}<br/>
${cookie.username.name}<br/>
${cookie.username.value}<br/>

${cookie.password.name}<br/>
${cookie.password.value}<br/>

<hr/>
<!-- el表达式中的pageContext对象 (el表达式中的内置对象)  跟jsp中学习的pageContext功能一样
	 pageContext 可以获取其他8个内置对象
-->
<!-- pageScope 取得pageContext.setAttribute方法设置的值 -->
项目的名称 :${pageContext.request.contextPath}<br/>
<hr/>
<%=request.getContextPath() %><br/>


<!-- 获取项目全局参数  通过initParam 内置对象获取全局参数 -->
<hr/>
<%=application.getInitParameter("sh") %><br/>
<%=application.getInitParameter("wh") %><br/>

${initParam}<br/>
${initParam.wh}<br/>
${initParam.sh}<br/>

JSTL

<c:if test="${age==18}" var="result" scope="request">
		我成年了
	</c:if>
	<hr/>
	<%-- <% 
		request.setAttribute("result", false);
		request.getAttribute("result")
	%> --%>
	${result}
	<hr/>
	over

<%--
	if(age==18){
	}else{
	}
 --%>
<h1>c:choose c:when c:otherwise 多分支语句</h1>
<c:choose>
	<c:when test="${age==18}">
		<p style='color:red'>我成年了</p>
	</c:when>
	<c:otherwise>
		<p style='color:green'>我不是18岁</p>
	</c:otherwise>
</c:choose>

<%--
	if(week==1){
	}else if(week==2){
	}else if(week==3){
	}
 --%>
 <c:choose>
	<c:when test="${week==1}">
		<p style='color:red'>星期一</p>
	</c:when>
	<c:when test="${week==2}">
		<p style='color:red'>星期二</p>
	</c:when>
	<c:when test="${week==3}">
		<p style='color:red'>星期三</p>
	</c:when>
	<c:when test="${week==4}">
		<p style='color:red'>星期四</p>
	</c:when>
	<c:when test="${week==5}">
		<p style='color:red'>星期五</p>
	</c:when>
	<c:when test="${week==6}">
		<p style='color:red'>星期六</p>
	</c:when>
	<c:when test="${week==7}">
		<p style='color:red'>星期天</p>
	</c:when>
	<c:otherwise>
		<p style='color:green'>输入错误</p>
	</c:otherwise>
</c:choose>

<!-- c:set  存值    xxxx.setAttribute("属性名","属性值")
	var : 你要存储的数据的属性名称
	value:你要存储的值
	scope:page,request,session,application
-->
<c:set var="name" value="我的名字叫战三" scope="page"></c:set>

<% 
	session.setAttribute("name", "我的名字叫战三")
	pageContext.getAttribute("name")
%>
<hr/>
${pageScope.name}<br/>
${name}<br/>
${proname}<br/>

<%
	List<Student> stulist = (List<Student>)request.getAttribute("stulist");
	%> 
	<table width="500" border="1" cellspacing="0">
		<tr>
			<td>序号</td>
			<td>学号</td>
			<td>姓名</td>
			<td>年龄</td>
			<td>性别</td>
		</tr>
	
	<!-- 
		if(stulist!=null && stulist.size()>0){
			for(Student s:stulist){
				pageContext.setAttribute("stu",s);
				pageContext.setAttribute("vs",当前迭代的信息的状态对象);
				
			}
		}
		c:forEach  可以循环集合、数组、map
		items:你要循环的集合  你的集合的值后台传过来的,你获取后台的值  用el表达式来获取
		var:集合循环下标的元素名称
		
		varStatus:当前迭代的信息的状态对象
		           该对象中有哪些属性:
		    count  表示迭代的个数   下标+1
		    index  获取下标  0开始
		    first  判断是否是第一个元素
		    last   判断是否是最后一个元素
	 -->
	
	
	<c:forEach items="${stulist}" var="stu" varStatus="vs" begin="0" end="4" step="1">
		<tr>
			<%-- <td>${vs.index+1}</td> --%>
			<td>${vs.count}</td>
			<td>${stu.sno}</td>
			<td>${stu.sname}</td>
			<td>${stu.age}</td>
			<td>${stu.sex}</td>
			<td>${vs.first}</td>
			<td>${vs.last}</td>
			<td>${vs.begin}</td>
			<td>${vs.end}</td>
			<td>${vs.current}</td>
			<td>${vs.step}</td>
			<td>${stu}</td>
		</tr>
	</c:forEach>
	
	
	<%-- <%
		if(stulist!=null && stulist.size()>0){
			for(Student stu:stulist){
	%>
				<tr>
					<td><%=stu.getSno() %></td>
					<td><%=stu.getSname() %></td>
					<td><%=stu.getAge() %></td>
					<td><%=stu.getSex()%></td>
				</tr>
	<%
			}
		}
	%> --%>
	
	
	
</table>

<!-- 循环数组 
	for(int i=0;i<arr.length;i=i+3){
	}
-->
<%-- <c:forEach items="${arr}" var="a" begin="3" end="7"> --%>
<c:forEach items="${arr}" var="a" step="3">
	<p>${a}</p>
</c:forEach>

<hr/>
<!-- 循环map -->
${map}<br/>
<%
	Map<String,Object> map = (Map<String,Object>)request.getAttribute("map");
	Set<Entry<String, Object>> entrySet = map.entrySet();
	for(Entry<String, Object> entry:entrySet){
%>
		<%=entry.getKey() %>
		=
		<%=entry.getValue()%>
		<br/>
<%
	}
%>
<hr/>
<!-- 
	m 得到是每一个key-value的键值对关系 类似于Map.entry对象
 -->
<c:forEach items="${map}" var="m">
	${m}====${m.key}=====${m.value}<br/>
</c:forEach>

<hr/>
<!-- 把data类型的值转换成日期格式的字符串 -->
${map.hiredate}<br/>
<!-- 
	fmt:formatDate 就是把date类型的值转换成字符串
	value:你要转换的date类型的值
	pattern:日期格式
 -->
<fmt:formatDate value="${map.hiredate}" pattern="yyyy-MM-dd HH:mm:ss"/>