200字范文,内容丰富有趣,生活中的好帮手!
200字范文 > web实验报告——JSP动态网页编程

web实验报告——JSP动态网页编程

时间:2023-12-14 08:47:50

相关推荐

web实验报告——JSP动态网页编程

实验报告一. 基本思路及实验结果(记录各种运行情况或页面的运行效果);1、使用TOMCAT服务器配置jsp应用1)打开TOMCAT/webapps子目录,创建一web应用(如myweb),将example0.jsp与example1.jsp文件复制入内,并在该目录下设置WEB-INF目录及web.xml配置文件;

2)在浏览器地址栏输入http://localhost:8080/myweb/example0.jsp ,回车后,查看example0.jsp网页效果

运行结果:

3)在浏览器地址栏输入http://localhost:8080/myweb/example1.jsp ,回车后,查看example1.jsp网页效果;

2、编写基本JSP动态网页1)创建date.jsp,页面运行效果应根据当前系统时间输出:

“ (上午、下午、晚上)好! 今天是__ __年_ ___月____日,星期(一~日)。”; [参考代码:Date today=new Date(); thisYear= 1900+today.getYear();] 源代码: <%@ page language="java"import="java.util.*"pageEncoding="gbk"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>Date</title> </head><body> <% //Calendar这类来表示时间,现在更常用,所以我选择使用了这个 Calendar date = Calendar.getInstance();//得到Calendar对象,不能简单地用new来创建intyear = date.get(Calendar.YEAR);//年intmonth = date.get(Calendar.MONTH)+1;//月,需+1来调整intday = date.get(Calendar.DATE);//日intweekDay = date.get(Calendar.DAY_OF_WEEK)-1;//星期几 String[] sweekDay = {"日","一","二","三","四","五","六"};//讲英文表示为中文inthour = date.get(Calendar.HOUR_OF_DAY);//得到现在是几点 String weclome = "";if(hour>=0&&hour<12){weclome = "上午"; }elseif(hour>12&&hour<18){weclome = "下午"; }else{weclome = "晚上"; } %> <%-- 将时间按格式显示出来--%> <%=weclome%>好 今天是<%=year %>年<%=month%>月<%=day%>日,星期<%=sweekDay[weekDay] %> </body> </html>

运行结果:

2)采用声明实现访问计数器count.jsp,并在页面中显示:“当前访问本网站的用户数为:**人/次。”[注意:正确显示中文输出,需要对page指令的属性进行设置:<%@ page contentType="text/html;charset=gb2312" %>]; 源代码: <%@ page language="java"import="java.util.*"pageEncoding="gb2312"%> <%@ page contentType="text/html;charset=gb2312"%><% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <%!staticinti = 0; %><html> <head> <base href="<%=basePath%>"> <title>count</title></head><body> <% i++; %> <%=i%>人/次; </body> </html>

运行结果:

*3)(可选)[include指令的语法规则为:<%@ include file="count.jsp"%> include动作的语法规则为:<jsp:include page="count.jsp"/>] 分别应用include指令以及include动作,使得example0.jsp、example1.jsp和date.jsp三个页面都包含count.jsp页面的输出内容,查看页面效果,并思考:要真正地体现对整个网站的访问量进行计数,应使用include指令还是include动作。 答: 将<%@ include file="count.jsp"%>指令分别加到example0.jsp、example1.jsp和date.jsp三个页面中 和<jsp:include page="count.jsp"/>分别加到example0.jsp、example1.jsp和date.jsp三个页面中的运行结果是相同的,运行结果如下:加入之后再刷新之后的结果 至于两者的区别:我看了书之后还是稀里糊涂,网上查了一些资料,它们这样讲解: 一、执行时间上: <%@ include file=””%> 是在翻译阶段执行 <jsp:include page=”” flush=”true” /> 在请求处理阶段执行。 二:引入内容的不同: <%@ include file=””%> 引入静态文本(html,jsp),在JSP页面被转化成servlet之前和它融和到一起。 <jsp:include page=”” flush=”true” /> 引入执行页面或servlet所生成的应答文本。 上交程序源代码,代码中应有相关注释。 所以综上所诉: 要真正地体现对整个网站的访问量进行计数,应使用include指令 遇到的问题: 1. 中文乱码: 解决方案:加入<%@ page contentType="text/html;charset=gb2312"%>2. <%@ include file="count.jsp"%> 和<jsp:include page="count.jsp"/>区别 解决方案:网上查资料

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。