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是存储登陆次数的文件.

  1. package com.zhc;
  2. import java.io.BufferedReader;
  3. import java.io.File;
  4. import java.io.FileReader;
  5. import java.io.FileWriter;
  6. import java.io.IOException;
  7. import java.io.PrintWriter;
  8. import java.net.URI;
  9. /**
     *
     * @author zhc
     *    存储登陆次数的文件.用到了流的知识.
     */
  10. public class CountFileHandler {
  11.     public static void writeFile(String filename, long count) {
  12.         try {
  13.             PrintWriter out = new PrintWriter(new FileWriter(filename));
  14.             out.println(count);
  15.             out.close();
  16.         } catch (IOException e) {
  17.             e.printStackTrace();
  18.         }
  19.     }
  20.     public static long readFile(String filename) {
  21.         long count = 0;
  22.         try {
  23.             File f = new File(filename);
  24.             if (!f.exists()) {
  25.                 writeFile(filename, 0);
  26.             }
  27.             BufferedReader in = new BufferedReader(new FileReader(f));
  28.             count = Long.parseLong(in.readLine());
  29.             in.close();
  30.         } catch (IOException e) {
  31.             e.printStackTrace();
  32.         }
  33.         return count;
  34.     }
  35.     
  36.     public static String transform(long count){
  37.         String countNumber=count+"";
  38.         String newNumber="";
  39.         System.out.println("aaaaaaaa");
  40.         for(int i=0;i<countNumber.length();i++){
  41.             //System.out.println(countNumber.length());
  42.             newNumber=newNumber+"<img src=/"images//"+countNumber.charAt(i)+".gif/">";
  43.         }
  44.         return newNumber;
  45.     }
  46. }

2.简单的javabean

  1. package com.zhc;
  2. public class From {
  3.     private String name;
  4.     private String password;
  5.     public String getName() {
  6.         return name;
  7.     }
  8.     public void setName(String name) {
  9.         this.name = name;
  10.     }
  11.     public String getPassword() {
  12.         return password;
  13.     }
  14.     public void setPassword(String password) {
  15.         this.password = password;
  16.     }
  17. }

3.xml配置错误文件

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app version="2.5" 
  3.     xmlns="http://java.sun.com/xml/ns/javaee" 
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  5.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
  6.     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  7.   <welcome-file-list>
  8.     <welcome-file>body.jsp</welcome-file>
  9.   </welcome-file-list>
  10.   <error-page>
  11.   <error-code>404</error-code>
  12.   <location>/404.jsp</location>
  13.   </error-page>
  14.   <error-page>
  15.   <error-code>500</error-code>
  16.   <location>/500.html</location>
  17.   </error-page>
  18. </web-app>

4.错误文件404.jsp 500.html

  1. 404.jsp
  2. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
  3. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  4. <html>
  5.   <head>
  6.     <title>Error jsp!</title>
  7.   </head>
  8.   <body>
  9.    对不起,您要访问的页面不存在。<br>
  10. 请您访问其它页面
  11.   </body>
  12. </html>
  13. 500.html
  14. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  15. <html>
  16.   <head>
  17.      <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  18.     <title>500.html</title>
  19.   </head>
  20.   <body>
  21. <body>
  22. 对不起,亲爱的用户,您访问的网页发生不可预知的问题。<br>
  23. 请您访问其它网页,或者与我们的客服人员联系。<br>
  24.   </body>
  25. </html>

5.视图层.三个jsp页面,包括了jsp的一些基本元素的运用

 

  1. 1.head.jsp
  2. <%@ page language="java" pageEncoding="UTF-8"%>
  3. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  4. <html>
  5.   <head>
  6.     <title>This is the head of the page.</title>
  7.     <meta http-equiv="pragma" content="no-cache">
  8.     <meta http-equiv="cache-control" content="no-cache">
  9.     <meta http-equiv="expires" content="0">    
  10.     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
  11.     <meta http-equiv="description" content="This is my page">
  12.     <!--
  13.     <link rel="stylesheet" type="text/css" href="styles.css">
  14.     -->
  15.   </head>
  16.   <body>
  17. 2.body.jsp
  18. <%@ page language="java" contentType="text/html; charset=UTF-8" %>
  19. <jsp:include flush="true" page="/head.jsp"></jsp:include>
  20. <form name="form" method="post" id="form" action="action.jsp">
  21. <table border="1">
  22. <tr>
  23. <td>姓名:</td>
  24. <td><input type="text" name="name"/></td>
  25. </tr>
  26. <tr>
  27. <td>密码:</td>
  28. <td><input type="password" name="password" /></td>
  29. </tr>
  30. <tr>
  31. <td><input type="submit" name="提交"/></td>
  32. <td><input type="reset" name="重写" /></td>
  33. </tr>
  34. </table>
  35. </form>
  36. <%@ include file="/tail.jsp" %>
  37.    3.tail.jsp
  38. <br>
  39. <a href="http://zhangchao.fyi.com.cn"> http://zhangchao.fyi.com.cn</a>
  40. </body>
  41. </html>

 

6.action.jsp

业务层.判断登陆session,和登陆次数

  1. <%@page language="java"  pageEncoding="UTF-8"%>
  2. <%@page import="com.zhc.CountFileHandler" %>
  3. <%@page import="com.zhc.From" %>u
  4. <%@page import="java.net.URL" %>
  5. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  6. <html>
  7.   <head>
  8.     <title>LoginForm Action</title>
  9.   </head>
  10.   <body>
  11.   <%! long count; %>
  12.   <%=request.getContextPath() %><br>
  13.    <%=request.getRealPath("/") %><br>
  14. <% 
  15. //long count=CountFileHandler.readFile(request.getRealPath("/")+"count.txt");
  16. long count=CountFileHandler.readFile(request.getRealPath("/")+"count.txt");
  17. if(session.getAttribute("visited")==null){
  18.     session.setAttribute("visited","y");
  19.     session.setMaxInactiveInterval(2);
  20.     count=count+1;
  21.     CountFileHandler.writeFile(request.getRealPath("/")+"count.txt",count);
  22. }
  23.  %> 
  24.   <%
  25.   request.setCharacterEncoding("UTF-8");
  26.   %>
  27.  <jsp:useBean id="aaa" scope="request" class="com.zhc.From" />
  28.  <jsp:setProperty name="aaa" property="name"/>
  29.  <jsp:setProperty name="aaa" property="password"/>
  30. 得到姓名:<jsp:getProperty name="aaa" property="name" />
  31. <br>得到密码:<jsp:getProperty name="aaa" property="password" />
  32. <h2>
  33. 欢迎您访问,本页面已经被访问过
  34. <%=CountFileHandler.transform(count) %>次了。 
  35. </h2>
  36.   </body>
  37. </html>

 

原创粉丝点击