javaWeb Jsp入门和常用对象的使用
2015-12-30 22:12
417 查看
本次学习学会了用jsp生成页面,这种方式比servelet更快捷,不用一行一行的输出html,加快了开发速度,在学习中借鉴了asp的一些思路
主要学会了如下方面:
使用分布视图(<%@inlude file=" "%>,<jsp:page file=" "> 的区别,如何解决帆布视图乱码问题)
Duplicate local variable 错误的解决办法
html和java代码的混合编写(<%%>,<%= %> ,<%! %> 的区别)
在jsp中使用application 和seesion对象跨页面传递数据(各自的生命周期是什么)
制作的demo运行结果如下:
相关说明如下
各jsp页面代码如下
index.jsp
分布视图 header.jsp
分布视图 footer.html
分布视图 forward.jsp
主要学会了如下方面:
使用分布视图(<%@inlude file=" "%>,<jsp:page file=" "> 的区别,如何解决帆布视图乱码问题)
Duplicate local variable 错误的解决办法
html和java代码的混合编写(<%%>,<%= %> ,<%! %> 的区别)
在jsp中使用application 和seesion对象跨页面传递数据(各自的生命周期是什么)
制作的demo运行结果如下:
相关说明如下
Duplicate local variable错误的原因是在分布视图中定义了同名变量,解决办法就是重命名 重名变量
引入的分布视图中文出现乱码解决办法就是 使用jsp动作的方式引入
各jsp页面代码如下
index.jsp
<%@ page language="java" import="java.util.*" %> <%@ page pageEncoding="UTF-8" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <body> <h1>嵌入jsp类型的 分布视图</h1> <%@include file="/header.jsp" %> <h1>嵌入html类型的 分布视图 [@include file方式](中文乱码)</h1> <%@include file="/footer.html" %> <h1>嵌入html类型的 分布视图[jsp:include page方式](该种方式引入html不会乱码)</h1> <jsp:include page="/footer.html"/> <h1>在被嵌入的jsp分布视图中使用farword 动作</h1> <%-- <%@include file="/forward.jsp" %> --%> <br/> <% if(application.getAttribute("visitCount")==null) { application.setAttribute("visitCount", 1); } else { Integer visitCount=(Integer)application.getAttribute("visitCount"); application.setAttribute("visitCount", ++visitCount); } if(session.getAttribute("visitCount")==null) { session.setAttribute("visitCount", 1); } else { Integer visitCount=(Integer)session.getAttribute("visitCount"); session.setAttribute("visitCount", ++visitCount); } %> <h1>服务器启动后的访问次数为: <%=(Integer)application.getAttribute("visitCount") %> 其中 您本次贡献了<%=(Integer)session.getAttribute("visitCount")%></h1> This is index.jsp <br> </body> </html>
分布视图 header.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> -------------------------This is header.jsp begin <br> <%! int begin=1; int end=4; int sum =0; %> <h5>我是header.jsp中的h5标签,当前正在进行<u><%=begin %></u> - <u><%=end %></u> 范围内偶数的累加运算</h5> <% String expression=""; int count=0; for(int i=begin;i<=end;i++) { count++; if(i%2==0) { expression+=i; if(i==end) expression+="="; else expression+="+"; out.print("<h4>正在进行第"+count+"次运算,本次运算的数是偶数</h4>"); sum+=i; } else { out.print("<h4>正在进行第"+count+"次运算,本次运算的数是奇数</h4>"); } } %> <h3>运算结果 : <%=expression%> <%=sum%> </h3> -------------------------This is header.jsp end <br>
分布视图 footer.html
-------------------------This is footer.html begin <br> <h5>我是footer.html中的h5标签</h5> -------------------------This is footer.html end <br>
分布视图 forward.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> -------------------------This is forward.jsp begin <br> <h5>我是forward.jsp中的h5标签,当前正在进行forward动作</h5> <jsp:forward page="/panda.html" /> -------------------------This is forward.jsp end <br>
相关文章推荐
- java学习:数据库的使用
- Android studio 如何导入Eclipse项目
- Java web开发编码设置
- java throw throws 区别(转载)
- Struts2的配置
- freemarker中参数及java片段的使用方法
- Java读写Excel文件示例
- java socket传送和接受byte[]
- Java类集学习(六)应用范例(一对多的关系)
- eclipse jad 反编译 插件安装
- javaWeb之Servlet的工作原理与生命周期
- byte[]转为string,再用substring取值,在eclipse和exe里取得值不同
- [一]初识SpringMVC
- java的内存管理机制
- Java创建线程的两个方法
- 1. 【创建与销毁对象】考虑用静态工厂方法代替构造器
- ERROR Executor: Exception in task 0.0 in stage 0.0 (TID 0) java.lang.ClassNotFoundException: main.s
- 【cas】keytool error: java.io.FileNotFoundException: xxx (Is a directory)
- java性能优化技巧
- eclipse中查看类、方法、控件等源码