您的位置:首页 > 编程语言 > Java开发

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运行结果如下:



相关说明如下









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>


  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: