J2EE实验2:乘法口诀表和访问计数器

实验2:乘法口诀表和访问计数器

实验要求:

  1. 口诀内容显示效果如下,应使用表格显示,否则格式不易控制。
  2. 在index.jsp中调用Java类的相应函数,获取最新的网站计数器值并以图形形式显示出来。计数器值存储在文本文件中。显示效果如下图所示:

直接贴代码了,需要注意的是最好把这些有功能都封装在java类里,一个是更直观,还有一个是更好调试。在jsp的调试总是出现莫名其妙的错误。

九九乘法口诀表:

index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
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>
    <titile>九九乘法表</titile>
  </head>
  
  <body>
    <h1>九九乘法表</h1>
    <hr/>
    <table>
    	<%for (int i = 1;i <= 9;i ++){
    		out.print("<tr>");
    		for (int j = 1;j <= i;j ++){
    			out.print("<td style='width:75px'>" + i + "*" + j + "=" + i*j + "</td>");
    		}
    		out.print("</tr>");
    	}
    	 %>
    </table>
  </body>
</html>

网站计数器:

WebOperate.java

package com.webcount;
import java.io.*;
public class WebOperate {
	static public int GetCount(){
		try {
			FileReader MyFileReader = new FileReader("count.txt");
			BufferedReader MyBufferedReader = new BufferedReader(MyFileReader);
			int Count = Integer.valueOf(MyBufferedReader.readLine());
			MyBufferedReader.close();
			MyFileReader.close();
			FileWriter MyFileWriter = new FileWriter("count.txt");
			BufferedWriter MyBufferedWriter = new BufferedWriter(MyFileWriter);
			MyBufferedWriter.write(String.valueOf(Count + 1));
			MyBufferedWriter.close();
			return Count;
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			FileWriter MyFileWriter;
			try {
				MyFileWriter = new FileWriter("count.txt");
				BufferedWriter MyBufferedWriter = new BufferedWriter(MyFileWriter);
				MyBufferedWriter.write("0");
				MyBufferedWriter.close();
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return 0;
	}
}
这个类就是读写文件的。


index.jsp

<%@ page language="java" import="com.webcount.*" pageEncoding="gbk"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>网站计数器</title>
  </head>
  
  <body>
    <h1>网站计数器</h1>
    <hr>
    <%
    String StrN = String.valueOf(WebOperate.GetCount());
    out.print("您好,你是本网站第");
    for (int i = 0;i < 5-StrN.length();i ++){
    	out.print("<img src='img/0.gif'/>");
    }
    for (int i = 0;i < StrN.length();i ++){
    	out.print("<img src='img/" + StrN.charAt(i) + ".gif'/>");
    }
    out.print("个访问者。");
    %>
  </body>
</html>
需要注意的是导入包,还有导入多个包需要用逗号(,)分离


还有一个需要注意的是资源的引用,你需要把资源放到WebRoot目录下。

举个例子,比如要引用图片

<img src='img/0.gif'/>
那么这个图片要这么放



阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/kydkong/article/details/49964767
文章标签: j2ee jsp
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

不良信息举报

J2EE实验2:乘法口诀表和访问计数器

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭