jsp项目开发总结

发布于 2018-07-05  217 次阅读


计数器:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
   <head>
      <title>统计在线人数</title>
   </head>
<body>
    <%!
       Integer number;//用来统计在线人数的变量
       synchronized void numberVisiter()
       {  //串行化计数函数  
         ServletContext application = getServletContext();
        Integer num = (Integer)application.getAttribute("count");
         if(num == null)
         {//如果是第一个访问者
            num = new Integer(1);
            application.setAttribute("count",num);
         }
         else
         {
             num = new Integer(num.intValue() + 1);
             application.setAttribute("count",num);
         }
      } 
   %>
    <%  
       if(session.isNew())
       {    //如果是一个新会话 
          numberVisiter();
          Integer number = (Integer)application.getAttribute("count");
       }
   %>
        简单的页面访问计数器<br/>
        欢迎访问此页面,您是<%=number%>个访问用户
    </body>
</html>

注册新班级:

<%@ page language="java" import="java.util.*" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>注册新班级</title>
</head>
<body>
<form action="createdo.jsp" method="post">
注册新班级<br>
班级名:<input type="text" name="userName" value=""></br></br>
入学年月:<input type="text" name="ontime" value=""></br></br>
毕业年月:<input type="text" name="endtime" value=""></br></br>
所在学校:<input type="text" name="place" value=""></br></br>
创建时间:<input type="text" name="createtime" value=""></br></br>
<input type="submit" value="创建新班级" >
</form>
</body>
</html>
<%@ page language="java" import="java.util.*" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>注册新班级</title>
</head>
<body>
<form action="createdo.jsp" method="post">
注册新班级<br>
班级名:<input type="text" name="userName" value=""></br></br>
入学年月:<input type="text" name="ontime" value=""></br></br>
毕业年月:<input type="text" name="endtime" value=""></br></br>
所在学校:<input type="text" name="place" value=""></br></br>
创建时间:<input type="text" name="createtime" value=""></br></br>
<input type="submit" value="创建新班级" >
</form>
</body>
</html>

注册qq号:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script type="text/javascript">
function validate(){
    var pa1=document.getElementById("password");
    var pa2=document.getElementById("password2");
    var pass1=pa1.value;
    var pass2=pa2.value;
    if(pass1!=pass2){
        alert("密码不一致");
        return false;
    }
    if(reg.password.value.length==0){
        alert("密码不能为空");
        return false;
    }
}
</script>

</head>

<body>
<form action="a.jsp" method="post" name="reg">
</br></br>
用户名:<input type="text" name="userName" value="" required="required" placeholder="请输入用户名"></br></br>
密   码:<input type="password"  id="password"  maxlength="6"></br></br>
确认密码:<input type="password" id="password2"></br></br>
邮箱:<input type="text"  name="email"><br/></br>
QQ号:<input type="text" name="name" value=""></br></br>
出生年月日:<input type="text" name="time"></br></br>
年龄:<input type="text" name="age"></br></br>
<input type="submit"  onclick="return validate()" value="提交"></br></br>
</form>
</body>
</html>
</html>
<%@ page language="java" import="java.util.*" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script type="text/javascript" src="js/jquery-1.12.0.js"></script>
<script type="text/javascript" src="js/jquery.validate.js"></script>
<script type="text/javascript" src="js/messages_zh.js"></script>


<script type="text/javascript">
$(function() {//整个页面加载完了之后执行这个方法
    $("#reg").validate({
        rules:{
            username:{
                required:true,
                rangelength:[2,6]
            },
            password:{
                required:true,
                minlength:6
            },
            repassword:{
                required:true,
                minlength:6,
                equalTo:"#password"
            }
        },
    message:{
        username:{
            required:"用户名必须填写",
            rangelength:"用户名长度必须在2-6之间"
        },
        password:{
            required:"密码必须填写",
            minlength:"密码长度至少有6位"
        },
        repassword:{
            required:"确认密码必须填写",
            minlength:"确认密码长度至少为6位",
            equalTo:"必须和密码一致"
        }
    }
    });
});
</script>
</head>
<body>
    <form id="reg" action="a.jsp" method="post">
      用户名:<input id="username" name="username" rangelength="2,6" required ><br/><br/>
      密码:<input type="password" id="password" name="password" minlength="6" required><br/><br/>
      确认密码:<input type="password"  name="repassword" equalTo="#password" required id="repassword" ><br/><br/>
      邮箱:<input id="email" name="email" email="true" required><br/><br/>
    QQ号:<input type="number" id="QQ" name="QQ" digits="true"><br/><br/>
      出生年月日:<input type="date" id="birth" name="birth" dateISO="true" required><br/><br/>
      年龄:<input id="age" name="age" min="0" max="100" number><br/><br/>
      <input type="submit" value="提交" >
    </form>
  </body>
</html>

留言和展示留言:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="leftshow.jsp" method="post">
班级在线留言</br></br>
留言标题:<input type="text" name="title" value=""></br></br>
留言内容:<input type="text" name="message" value=""></br></br>
留言时间:<input type="text" name="time" value=""></br></br>
<input type="submit" value="提交">
<input type="reset" value="取消">
</form>
</body>
</html>
<%@ page language="java" import="java.util.*" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>留言展示</title>
</head>
<body>
    <%
      request.setCharacterEncoding("utf-8");
      String title = request.getParameter("title");
      String message = request.getParameter("message");
      String time = request.getParameter("time");

   List<String []>list= (List<String []>) application.getAttribute("message");
   if(list==null)
   {
       List<String []>tmp=new ArrayList<String []>();
       list=tmp;
   }
   String[] a=new String[3];
   a[0]=title;
   a[1]=message;
   a[2]=time;
   list.add(a);
   application.setAttribute("message",list);
   List<String []>list2= (List<String []>) application.getAttribute("message");
   for(int i=0;i<list2.size();i++)
   {
       String[] b=list2.get(i);
       out.println("留言标题:"+b[0]);
       out.println("--------------------");
   }
    %>
   </body>
</html>

注册个人信息及其展示:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>

        <title>用户注册</title>
    </head>
    <body>
        <form action="userInfo.jsp" method="post">
注册新用户
</br></br>
用户名:<input type="text" name="userName" value=""></br></br>
密   码:<input type="password" name="password"></br></br>
确认密码:<input type="password" name="password2"></br></br>
电子邮件:<input type="text"  name="email"><br/></br>
性    别:<input type="radio"  name="gender" value="male" checked="checked">男
<input type="radio" name="gender" value="female">女</br></br>
爱好:<input type="checkbox" name="hobbys" value="sports" >运动
<input type="checkbox" name="hobbys" value="singing" >唱歌
<input type="checkbox" name="hobbys" value="film"  >看电影
<input type="checkbox" name="hobbys" value="reading"  >看书</br></br>
年龄:<select name="age">
<option value="0">0-30</option>
<option value="1">31-60</option>
<option value="2">61-90</option>
</select></br></br>

喜欢的花:<select name="flowers">
<option value="0">玫瑰花</option>
<option value="1">百合花</option>
<option value="2">仙人掌</option>
</select></br></br>

个人简介:<textarea rows="s" cols="20" name="resume"></textarea></br></br>
隐藏上传学号:<input type="hidden" name="id" value="123456">
<input type="submit" value="注册">
<input type="reset" value="取消"> 
</form>

    </body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<html>
  <head>
     <title>注册成功</title>
  </head>

  <body>
    <%
      request.setCharacterEncoding("utf-8");
      String userName = request.getParameter("userName");
      String password = request.getParameter("password");
      String email = request.getParameter("email");
      String gender = request.getParameter("gender");
      String[] hobbys = request.getParameterValues("hobbys");
      String age = request.getParameter("age");
      String[] flowers = request.getParameterValues("flowers");
      String resume = request.getParameter("resume");
      String id = request.getParameter("id");
     %>
     成功注册<br/>
     用户名:<%=userName %><br/>
     密  码:<%=password %><br/>
     电子邮件:<%=email %><br/>
     性  别:<%=gender %><br/>
<%--    <%! int i=0;%> --%>
     爱  好:<%
              for(int i=0; i<hobbys.length; i++){
               out.print(hobbys[i]+" ");
              }
              %><br/>
     年龄:<%=age %><br/>
     喜欢的花:<%
              for(int i=0; i<flowers.length; i++){
               out.print(flowers[i]+" ");
              }
              %><br/>
      个人简介:<%=resume %><br/>
      <%--接收隐藏域上传的参数值 --%>
      学号:<%=id %>
   </body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
  <head>
     <title>我是欢迎页</title>
   </head>
  <body>
    <% 
       String userName = (String)session.getAttribute("userName"); 
       System.out.println(userName);
       String loginTime = (String)session.getAttribute("loginTime");
       if(userName==null || userName.equals("")){
       out.println("你没有登录,不能访问,3秒钟后自动返回登录网页!<br/>");
         response.setHeader("Refresh","3;URL=login.jsp");
       }else{
         out.print(userName+",欢迎您!");
         out.print("登录时间"+loginTime+"<br/>"); 
       }  
     %>
    我是欢迎页!
  </body>
</html>

Comments


愿我如星君如月,夜夜流光相皎洁。