jsp初级练习1
来源:互联网 发布:深圳市云计算产业协会 编辑:程序博客网 时间:2024/04/30 10:32
jsp学习总结的小程序:
完成页面计数.session,javabean的基本功能,错误页面的配置
两个类:CountFileHandler.java 计数功能
Form.java javabean功能
若干jsp页面:
head.jsp+body.jsp+tail.jsp 组成主页面
404.jsp,500.jsp组成错误页面.
count.txt是存储登陆次数的文件.
- package com.zhc;
- import java.io.BufferedReader;
- import java.io.File;
- import java.io.FileReader;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.io.PrintWriter;
- import java.net.URI;
- /**
*
* @author zhc
* 存储登陆次数的文件.用到了流的知识.
*/ - public class CountFileHandler {
- public static void writeFile(String filename, long count) {
- try {
- PrintWriter out = new PrintWriter(new FileWriter(filename));
- out.println(count);
- out.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- public static long readFile(String filename) {
- long count = 0;
- try {
- File f = new File(filename);
- if (!f.exists()) {
- writeFile(filename, 0);
- }
- BufferedReader in = new BufferedReader(new FileReader(f));
- count = Long.parseLong(in.readLine());
- in.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- return count;
- }
- public static String transform(long count){
- String countNumber=count+"";
- String newNumber="";
- System.out.println("aaaaaaaa");
- for(int i=0;i<countNumber.length();i++){
- //System.out.println(countNumber.length());
- newNumber=newNumber+"<img src=/"images//"+countNumber.charAt(i)+".gif/">";
- }
- return newNumber;
- }
- }
2.简单的javabean
- package com.zhc;
- public class From {
- private String name;
- private String password;
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public String getPassword() {
- return password;
- }
- public void setPassword(String password) {
- this.password = password;
- }
- }
3.xml配置错误文件
- <?xml version="1.0" encoding="UTF-8"?>
- <web-app version="2.5"
- xmlns="http://java.sun.com/xml/ns/javaee"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
- http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
- <welcome-file-list>
- <welcome-file>body.jsp</welcome-file>
- </welcome-file-list>
- <error-page>
- <error-code>404</error-code>
- <location>/404.jsp</location>
- </error-page>
- <error-page>
- <error-code>500</error-code>
- <location>/500.html</location>
- </error-page>
- </web-app>
4.错误文件404.jsp 500.html
- 404.jsp
- <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
- <html>
- <head>
- <title>Error jsp!</title>
- </head>
- <body>
- 对不起,您要访问的页面不存在。<br>
- 请您访问其它页面
- </body>
- </html>
- 500.html
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
- <html>
- <head>
- <meta http-equiv="content-type" content="text/html; charset=UTF-8">
- <title>500.html</title>
- </head>
- <body>
- <body>
- 对不起,亲爱的用户,您访问的网页发生不可预知的问题。<br>
- 请您访问其它网页,或者与我们的客服人员联系。<br>
- </body>
- </html>
5.视图层.三个jsp页面,包括了jsp的一些基本元素的运用
- 1.head.jsp
- <%@ page language="java" pageEncoding="UTF-8"%>
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
- <html>
- <head>
- <title>This is the head of the page.</title>
- <meta http-equiv="pragma" content="no-cache">
- <meta http-equiv="cache-control" content="no-cache">
- <meta http-equiv="expires" content="0">
- <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
- <meta http-equiv="description" content="This is my page">
- <!--
- <link rel="stylesheet" type="text/css" href="styles.css">
- -->
- </head>
- <body>
- 2.body.jsp
- <%@ page language="java" contentType="text/html; charset=UTF-8" %>
- <jsp:include flush="true" page="/head.jsp"></jsp:include>
- <form name="form" method="post" id="form" action="action.jsp">
- <table border="1">
- <tr>
- <td>姓名:</td>
- <td><input type="text" name="name"/></td>
- </tr>
- <tr>
- <td>密码:</td>
- <td><input type="password" name="password" /></td>
- </tr>
- <tr>
- <td><input type="submit" name="提交"/></td>
- <td><input type="reset" name="重写" /></td>
- </tr>
- </table>
- </form>
- <%@ include file="/tail.jsp" %>
- 3.tail.jsp
- <br>
- <a href="http://zhangchao.fyi.com.cn"> http://zhangchao.fyi.com.cn</a>
- </body>
- </html>
6.action.jsp
业务层.判断登陆session,和登陆次数
- <%@page language="java" pageEncoding="UTF-8"%>
- <%@page import="com.zhc.CountFileHandler" %>
- <%@page import="com.zhc.From" %>u
- <%@page import="java.net.URL" %>
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
- <html>
- <head>
- <title>LoginForm Action</title>
- </head>
- <body>
- <%! long count; %>
- <%=request.getContextPath() %><br>
- <%=request.getRealPath("/") %><br>
- <%
- //long count=CountFileHandler.readFile(request.getRealPath("/")+"count.txt");
- long count=CountFileHandler.readFile(request.getRealPath("/")+"count.txt");
- if(session.getAttribute("visited")==null){
- session.setAttribute("visited","y");
- session.setMaxInactiveInterval(2);
- count=count+1;
- CountFileHandler.writeFile(request.getRealPath("/")+"count.txt",count);
- }
- %>
- <%
- request.setCharacterEncoding("UTF-8");
- %>
- <jsp:useBean id="aaa" scope="request" class="com.zhc.From" />
- <jsp:setProperty name="aaa" property="name"/>
- <jsp:setProperty name="aaa" property="password"/>
- 得到姓名:<jsp:getProperty name="aaa" property="name" />
- <br>得到密码:<jsp:getProperty name="aaa" property="password" />
- <h2>
- 欢迎您访问,本页面已经被访问过
- <%=CountFileHandler.transform(count) %>次了。
- </h2>
- </body>
- </html>
- jsp初级练习1
- 迪杰斯特拉初级练习
- MySQL初级练习笔记
- java反射初级练习
- C语言初级练习
- 算法初级练习
- python+mongodb初级练习
- Jsp初级讲解一
- JSP初级入门
- jsp初级学习
- JSP考试题(难度初级)
- 初级学习JSP总结
- jsp初级常用标签
- jsp练习
- java 初级程序员的练习
- java 初级程序员的练习
- java 初级程序员的练习
- java 初级程序员的练习
- 使用J-FTP上传下载(记录)
- 经典人生(1)
- C语言宏定义技巧
- vi使用手册(转载)
- SNMP
- jsp初级练习1
- 经典人生(2)
- 经典人生(3)
- 外文原版图书
- use css modify the sub object of a div
- 谈谈硬编码
- 孩子发烧别急着降温
- 换地方了
- 计算机取证软件--EnCase V6