3.編寫一個JSP程序實現手錶的功能,顯示當前時間(時:分:秒),並不停地自動刷新時間。html
方法一【scriptlet】java
<%@ page language="java" import="java.util.*,java.text.SimpleDateFormat" pageEncoding="UTF-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>work01</title> <meta http-equiv="refresh" content="1"> </head> <body> <% Date d=new Date(); SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String time=sdf.format(d); %> <h1><%=time %></h1> </body> </html>
方法二【js】瀏覽器
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%> <html> <head> <title>work1</title> <script> function run() { var d =new Date(); document.getElementById("clock").innerHTML=d.toLocaleString(); setTimeout("run();",1000); } </script> </head> <body onload="run()"> <span id ="clock"></span> </body> </html>
4,編寫一個JAVA類和一個JSP頁面,把下列信息封裝到3個Student對象裏,再把每個對象放到一個ArrayList對象裏,再利用ArrayList對象在JSP頁面的表格中顯示所示的信息session
package songyan; public class Student { private String id; private String name; private String sex; private String classes; private double grade; public Student() { } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public String getClasses() { return classes; } public void setClasses(String classes) { this.classes = classes; } public double getGrade() { return grade; } public void setGrade(double grade) { this.grade = grade; } }
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ page import="songyan.Student" %> <%@ page import="java.util.*" %> <% Student s4=new Student(); Student s2=new Student(); Student s3=new Student(); s4.setId("004"); s4.setClasses("04"); s4.setName("李白"); s4.setSex("男"); s4.setGrade(723.0); s2.setId("002"); s2.setClasses("02"); s2.setName("孟浩然"); s2.setSex("男"); s2.setGrade(689.0); s3.setId("003"); s3.setClasses("03"); s3.setName("楊玉環"); s3.setSex("女"); s3.setGrade(600.0); ArrayList<Student> al=new ArrayList<Student>(); al.add( s4); al.add( s2); al.add( s3); %> <html> <head> <title>work2</title> </head> <body> <% String[] title= new String[]{"學號","姓名","性別","班級","成績"}; %> <table width="780px" border="4px" cellpading="0"> <tr> <td><%=title[0] %></td> <td><%=title[4] %></td> <td><%=title[2] %></td> <td><%=title[3] %></td> <td><%=title[4] %></td> </tr> <tr> <td><%=s4.getId() %></td> <td><%=s4.getName() %></td> <td><%=s4.getSex() %></td> <td><%=s4.getClasses() %></td> <td><%=s4.getGrade() %></td> </tr> <tr> <td><%=s2.getId() %></td> <td><%=s2.getName() %></td> <td><%=s2.getSex() %></td> <td><%=s2.getClasses() %></td> <td><%=s2.getGrade() %></td> </tr> <tr> <td><%=s3.getId() %></td> <td><%=s3.getName() %></td> <td><%=s3.getSex() %></td> <td><%=s3.getClasses() %></td> <td><%=s3.getGrade() %></td> </tr> </table> </body> </html>
5,編寫一個JSP程序,使用JSP Script顯示網頁上的不一樣顏色的顏色條,暫顯示以下顏色的顏色條:綠色、藍綠色、黑色、紅色、黃色以及粉紅(對應的顏色爲:Green、Cyan、Black、Red、Yellow、Pink)jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <html> <head> <title>work02</title> </head> <body> <% String[] colors={"Green","Cyan","Black","Red","Yellow","Pink"}; for(int i=0;i<colors.length;i++) { %> <hr color="<%=colors[i] %>" ></hr> <% }%> </body> </html>
1,編寫兩個文檔,一個是JSP文檔命名爲myjsp.jsp,另外一個是普通的HTML文檔,命名爲myphoto.html。post
要求:在myphoto.html插入本身的照片,在myjsp.jsp中嵌入<jsp:include>操做指令,當在IE中運行myjsp.jsp時可以將myphoto.html中的照片顯示出來。網站
<!DOCTYPE html> <html> <head> <title>myphoto.html</title> </head> <body> <image src="image/3.jpg" width="400px"></image> </body> </html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
import="java.util.*,java.text.*,songyan.bean.*"%>
<html>
<head>
<title>title</title>
<%@include file="photo.html" %>
</head>
<body>
</body>
</html>
2,編寫一個JSP頁面,實現根據一我的的18位身份證顯示出生日的功能來,要求把表達式聲明和Scriptlet所有用到,並把結果顯示在表格中,以下表ui
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <html> <head> <title>index.jsp</title> </head> <body> <%! String[] ids={"373325199611234322","373325199301214322"}; %> <form action="result.jsp" method="get"> <table border="1"> <tr> <td >身份證</td> <td>生日</td> </tr> <tr> <td name="id1"><%=ids[0] %></td> <td name="date1"><%=ids[0].substring(6, 10)+"-"+ids[0].substring(11, 12)+"-"+ids[0].substring(13, 14) %></td> </tr> <tr> <td name="id2"><%=ids[1] %></td> <td name="date2"><%=ids[1].substring(6, 10)+"-"+ids[1].substring(11, 12)+"-"+ids[1].substring(13, 14) %></td> </tr> </table> </form> </body> </html>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <html> <head> <title>My JSP 'result.jsp' starting page</title> </head> <body> <% String id1=request.getParameter("id1"); String id2=request.getParameter("id2"); System.out.print("jj"); %> </body> </html>
3.編寫一個jsp頁面,利用Scriptlet編寫一段計算代碼,要求用0作除數,並使用page指令將該錯誤信息顯示在另外一個jsp頁面上,產生的錯誤信息爲「錯誤,不能用0作除數!」。this
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page errorPage="error.jsp" %>
<html>
<head>
<title>index.jsp</title>
</head>
<body>
<%=8/0 %>
</body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <html> <head> <title>My JSP 'MyJsp.jsp' starting page</title> </head> <body> 錯誤,0不能作除數! </body> </html>
4.超女音樂吧用戶需求註冊,請爲此編寫jsp頁面來實現註冊,註冊信息包括用戶名、密碼、性別、年齡、電話和E-mail。用戶名不能重複,若是用戶名已經存在要提示用戶;用戶名、性別、密碼和E-mail必須輸入;密碼須要輸入兩次,並先後一致;E-mail要求進行合法性檢驗。spa
1.請編寫JSP程序實現如圖所示的簡易計算器。要求:輸入「第一個參數」,選擇運算類型(+,-,*,/),輸入 「第二個參數」後,按「計算」按鈕,結果將顯示在「結果」文本框中。
要求:程序須要對輸入參數是否合法進行判斷,
例如參數是否爲數字,除法時,除數不爲0的判斷。
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>index.jsp</title> <script> function getresult() { var num1=document.getElementById("num1").value; var sig=document.getElementById("sig").value; var num2=document.getElementById("num2").value; n1 = parseFloat(num1); n2 = parseFloat(num2); if(sig=="/"&& num2=="0") { document.write("除數不能爲零"); } else { switch (sig) { case "+": document.getElementById("result").value = n1+n2; break; case "-": document.getElementById("result").value =n1-n2; break; case "*": document.getElementById("result").value =n1*n2; break; case "/": document.getElementById("result").value =n1/n2; break; } } } </script> </head> <body> <h1>計算器</h1> <hr color="red"> 請輸入第一個數:<input type="text" name="num1" id="num1"><br><br> <select name="sig" id="sig"> <option value="+">+</option> <option value="-">-</option> <option value="*">*</option> <option value="/">/</option> </select><br><br> 請輸入第二個數:<input type="text" name="num2" id="num2"><br><br> <input type="submit" value="submit" onclick="getresult()"> <input type="reset" value="reset"><br><br> <input type="text" name="result" id="result"/> </body> </html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>calculator.jsp</title> </head> <body> <jsp:useBean id="calcBean" class="songyan.day03.Calculator"></jsp:useBean> <jsp:setProperty property="*" name="calcBean" /> <% calcBean.calculate(); %> <hr> 計算結果是: <jsp:getProperty property="firstnum" name="calcBean" /> <jsp:getProperty property="operator" name="calcBean" /> <jsp:getProperty property="secondnum" name="calcBean" /> =<jsp:getProperty property="result" name="calcBean" /> <hr> <form action="/jsp/day03/calculator.jsp" method="post"> <table style="text-align: center;"> <tr> <td colspan="2">簡單的計算器</td> </tr> <tr> <td>第一個參數</td> <td><input type="text" name="firstnum" /></td> </tr> <tr> <td><select name="operator"> <option value="+">+</option> <option value="-">-</option> <option value="*">*</option> <option value="/">/</option> </select></td> </tr> <tr> <td>第二個數</td> <td><input type="text" name="secondnum" /></td> </tr> <tr> <td> <button name="calc" type="submit">計算</button> </td> </tr> </table> </form> </body> </html>
編寫一個JSP頁面,將用戶名和密碼存放到會話中(假設用戶名爲「孤獨求敗」,密碼爲「123456」),再從新定向到另外一個JSP頁面,將會話中存放的用戶名和密碼顯示出來。(提示:使用response對象的sendRedirect()方法進行重定向。)
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>jsp計算器</title> </head> <body> <% HttpSession ses =request.getSession(); ses.setAttribute("username", "孤獨求敗"); ses.setAttribute("password", "123456"); response.sendRedirect("response.jsp"); %> </body> </html>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <html> <head> <title>My JSP 'response.jsp' starting page</title> </head> <body> 用戶名:<%=request.getSession().getAttribute("username") %> 密碼 :<%=request.getSession().getAttribute("password") %> </body> </html>
編寫一個JSP登陸頁面,可輸入用戶名和密碼,提交請求到另外一個JSP頁面,該JSP頁面獲取請求的相關數據並顯示出來。請求的相關數據包括用戶輸入的請求數據和請求自己的一些信息(好比請求使用的協議getProtocol() 、請求的URI request.getServletPath() 、請求方法request.getMethod() 、遠程地址request.getRemoteAddr()等)。
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>index.jsp</title> </head> <body> <!-- 編寫一個JSP登陸頁面,可輸入用戶名和密碼,提交請求到另外一個JSP頁面, 該JSP頁面獲取請求的相關數據並顯示出來。請求的相關數據包括用戶輸入 的請求數據和請求自己的一些信息(好比請求使用的協議getProtocol() 、請求的URI request.getServletPath() 、請求方法request.getMethod() 、遠程地址request.getRemoteAddr()等)。 --> <form action="result.jsp"> 用戶名:<input type="text" name="userName"><br><br> 密碼:<input type="text" name="password"><br><br> <input type="submit" value="登錄"> </form> </body> </html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>result.jsp</title> </head> <body> <!-- 編寫一個JSP登陸頁面,可輸入用戶名和密碼,提交請求到另外一個JSP頁面, 該JSP頁面獲取請求的相關數據並顯示出來。請求的相關數據包括用戶輸入 的請求數據和請求自己的一些信息(好比請求使用的協議getProtocol() 、請求的URI request.getServletPath() 、請求方法request.getMethod() 、遠程地址request.getRemoteAddr()等)。 --> 用戶名:<%=request.getParameter("userName") %><br><br> 密碼:<%=request.getParameter("password") %><br><br> 請求使用的協議:<%=request.getProtocol() %><br><br> 請求的URI:<%=request.getServletPath() %><br><br> 請求方法:<%=request.getMethod() %><br><br> 遠程地址:<%=request.getRemoteAddr()%><br><br> </body> </html>
利用隱式對象爲某一網站編寫一個JSP程序,統計該網站的訪問次數。
一種狀況是:按照客戶進行統計(按照瀏覽器進行統計,一個瀏覽器若是訪問網站的話,就算一次訪問,換句話說若是這個瀏覽器刷新屢次網站的話,也算是一次訪問);
另外一種狀況:刷新一次頁面,就算是一次訪問。
要求用隱式對象去實現。
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>jsp計算器</title> </head> <body> <% if (pageContext.getAttribute("pageCount") == null) { pageContext.setAttribute("pageCount", new Integer(0)); } if (session.getAttribute("sessionCount") == null) { session.setAttribute("sessionCount", new Integer(0)); } Integer count = (Integer) pageContext.getAttribute("pageCount"); pageContext.setAttribute("pageCount", new Integer( count.intValue() + 1)); Integer count2 = (Integer) session.getAttribute("sessionCount"); session.setAttribute("sessionCount", new Integer( count2.intValue() + 1)); %> 頁面計數:<%=pageContext.getAttribute("pageCount")%> <br /> 瀏覽器計數<%=session.getAttribute("sessionCount")%> <br /> </body> </html>