JSP基礎和內置對象編程java web實驗

1.新建一個JSP頁面,該頁面向瀏覽器輸出一句”Hello JSP”,要求採用三種方式輸出(模板文本輸出/out.println方法輸出/JSP表達式輸出),並觀察該JSP頁面所生成Servlet,解釋頁面內容在Servlet中的對應角色;

 


源碼:

<%@ 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>
    <base href="<%=basePath%>">
    <title>My JSP 'Hell.jsp' starting 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>
    Hello jsp! <br/>
  <%
out.println("Hello jsp!");  
   %>
  <br/>
  <%="Hello jsp!" %>
</body>
</html>

解釋:JSP一般的運行方式爲:當服務器啓動後,當Web瀏覽器端發送過來一個頁面請求時,Web服務器先判斷是否是JSP頁面請求。如果請求的頁面是JSP頁面,則由JSP引擎檢查該JSP頁面,如果該頁面是第一次被請求、或不是第一次被請求但已被修改,則JSP引擎將此JSP頁面代碼轉換成Servlet代碼,然後JSP引擎調用服務器端的Java編譯器javac.exe對Servlet代碼進行編譯,把它變成字節碼(.class)文件,然後再調用JAVA虛擬機執行該字節碼文件,然後將執行結果傳給Web瀏覽器端。如果該JSP頁面不是第一次被請求,且沒有被修改過,則直接由JSP引擎調用JAVA虛擬機執行已編譯過的字節碼.class文件,然後將結果傳送Web瀏覽器端。

其中Out轉譯後對應JspWriter對象,其內部關聯一個PringWriter對象。

 

2.新建JSP頁面,顯示一個如下表格,表格重複內容採用循環輸出,並觀察該JSP頁面所生成Servlet,解釋頁面內容在Servlet中的對應角色;

源碼:

<%@ 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>
    <base href="<%=basePath%>">
   <title>My JSP 'book.jsp' starting 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>
  <table border="2"width="100%"height="80%"align="center">
        <tr align="center"> <td>書號</td><td>書名</td><td>作者</td>
         <td>出版社</td><td>價格</td><td>類別</td><td>操作</td></tr>
        <%for (int i=1;i<6;i++)
        { %> 
        <tr align="center">
    <td>ISBN-001</td><td>Web開發</td><td>範月華</td>
    <td>清華出版社</td><td>25</td><td>新書</td>
    <td><a href="#">編輯</a>    
<a href="#">刪除</a>
    </td></tr> 
    <%}
     %>
  </table>  
  </body>
</html>

request轉譯後對應HttpServletRequest/ServletRequest對象

 

3.教材P154 例7.4,out.println輸出形式改成模板文本輸出和JSP表達式輸出兩種形式並觀察該JSP頁面所生成Servlet,解釋頁面內容在Servlet中的對應角色;

<%@ 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>
    <base href="<%=basePath%>">
    <title>My JSP 'Num3.jsp' starting 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>
<center><font color="red"size="7"face="微軟雅黑">
<%Date today=new Date();
int hours=today.getHours();if(hours>=0&&hours<12){ %>
朋友們,早上好!
<%}else if(hours>=12&&hours<19){ %>
朋友們,中午好!
<%}else{%>
朋友們,晚上好!
<%} %>
</font>
</center>
</body>
</html>

 

4

<%@ 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>
    <base href="<%=basePath%>">    
    <title>My JSP 'Num4.jsp' starting 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><center>
<font color="red"size="7"face="微軟雅黑">
<%
Date today=new Date();
int hours=today.getHours();
if(hours>=0&&hours<12){
 %>
<%="朋友們,早上好!"%>
<%}else if(hours>=12&&hours<19){ %>
<%="朋友們,中午好!"%>
<%}else {%>
<%="朋友們,晚上好!"%>
<%} %>
</font></center></body></html>

request轉譯後對應HttpServletRequest/ServletRequest對象

 

5.改造書籍添加頁面,實現將書籍添加信息提交至一個JSP頁面,在該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>
    <base href="<%=basePath%>">
    <title>My JSP '1.jsp' starting page</title> 
</head>
   <body>
    <table border="2"width="80%"height="80%">
     <%  request. setCharacterEncoding("utf-8");    
     String number=request.getParameter("BookNumber");
     String bookname=request.getParameter("BookName");
     String name=request.getParameter("userName");
     String chu=request.getParameter("chu_ban_she_shi");
     String data=request.getParameter("date2");
     String price=request.getParameter("money");
     String wei=request.getParameter("location");
     String lei=request.getParameter("lei_bie");
     String beizhu=request.getParameter("comment");%>
     <tr><td>書號</td><td><%=number %></td> </tr>
     <tr><td>書名</td><td><%=bookname %></td></tr>
     <tr><td>作者</td><td><%=name %></td></tr>
     <tr><td>出版社</td><td><%=chu %></td></tr>
     <tr><td>出版日期</td><td><%=data %></td></tr>
     <tr><td>價格</td><td><%=price %></td></tr>
     <tr><td>位置</td><td><%=wei %></td></tr>
     <tr><td>類別</td><td><%=lei %></td></tr>
     <tr><td>備註</td><td><%=beizhu %>
</td> </tr>
</table>
</body>
</html>

 

6.開發完成登陸頁面login.html,實現將登陸信息提交至一個JSP頁面,在該JSP頁面讀取用戶名和密碼並做簡單判斷,如果正確轉向首頁,並在首頁頂部顯示歡迎信息(今天是XXXX年XX月XX日,歡迎XX),否則轉向錯誤提示頁面(提示重新登陸);


 

<html>
<head>
<title>login.html</title>
</head>
  <body>
  <form action="2.jsp" method="get">
   <table border="0"valign="top"align="top">    
<tr><td>姓名</td><td><input type ="text"name="Name"/></td></tr>
   <tr><td>密碼</td><td><input type ="password"name="pwd"/></td></tr>
<tr><td colspan="2" align="center"><input type="submit"value="提交"></input>
</td></tr></table>
</form></body>
</html>

如果賬號輸入錯誤,則進入提示


 

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<html><head>
    <title>My JSP '2.jsp' starting page</title>    
</head>
  <body><% String name=request.getParameter("Name");  
  String pwd=request.getParameter("pwd");
  if(name.equals("alang123")&&pwd.equals("123"))
   {response.sendRedirect("3.jsp?");
     session.setAttribute("NAME",name);
}else{response.sendRedirect("error.html");} %>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
訪問出錯啦,請重新<a href="login.html">登錄</a>
</body>
</html>

 

如果賬號沒問題,則轉向首頁,並向首頁顯示歡迎信息


 

<%@ 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><base href="<%=basePath%>">
    <title>My JSP '4.jsp' starting 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>
   <%   String name = (String)session.getAttribute("NAME");    
if(name==null){response.sendRedirect("error2.html");}%>
歡迎<%=name %>!<br/> 恭喜成功!!
</body>
</html>

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章