java 培訓日記

javahoting發表於2007-11-19
進行了一週緊張後的學習,今天終於閒下把日記與大家分享,在這周裡張老師給我們講了好多未來我們在工作中出現的一些問題,我記得不是很好!希望大家多提建議!讓我更好的掌握JAVA :D
一、myeclipse的安裝和基本使用
1、安裝路徑最好不帶有空格;
2、將Tomcat置於myeclipse的控制之下;
3、建立Web Project,以及釋出到Tomcat伺服器;
4、myeclipse常用的快捷鍵列表如下:
Alt + / 程式碼提示
Ctrl + shift + o 匯入包
Ctrl + shift + f 程式碼格式化
Alt + shift + s 彈出使用右鍵source的選單
Alt + shift + z 程式碼包含(如try/catch)
Alt + shift + / 將程式碼註釋掉
Alt + shift + \ 取消對程式碼的註釋

二、JSP的部分基礎知識
1、指令元素:page指令,include指令,taglib指令。指令必須巢狀在<%@ %>中
2、指令碼片段,在裡面直接寫java原始碼 <% %>
3、指令碼表示式 <%= %>
4、指令碼宣告 <%! %>
5、JSP註釋 <%-- --%>

三、Tomcat中重要的幾個Servlet(在\conf\web.xml中檢視)
DefaultServlet(預設Servlet) org.apache.catalina.servlets.DefaultServlet
作用:處理伺服器中某個靜態資源,例如載入靜態的網頁,圖片。如果在conf目錄下的web.xml中註釋掉這個servlet,則我們釋出的靜態網頁不會顯示。(還要註釋掉<servlet-mapping>)
InvokerServlet(Servlet啟用器) org.apache.catalina.servlets.InvokerServlet
作用:啟用和呼叫任何其他Servlet,需要在全域性的Web.xml中配置,預設被註釋掉,如果去掉註釋,則我們不用自己在我們的web.xml中配置我們寫的servlet,伺服器也會自動幫我們載入。
JspServlet org.apache.jasper.servlet.JspServlet
作用:編譯和執行jsp頁面

四、兩個案例
(一)模擬DefaultServlet輸出靜態檔案的內容
1、Tomcat系統的DefaultServlet的作用就是讀取靜態資源然後輸出到客戶端瀏覽器,為了能驗證程式的效果,我們需要把系統conf\web.xml中關於它的配置註釋掉;
2、新建一個Servlet,假定我們只讓這個servlet攔截對Html檔案的訪問,就把對映路徑設定為*.html;
3、在servlet裡面得到瀏覽器請求的檔名,得到輸入流,再把輸入流讀到陣列當中,再把陣列內容寫入到由response物件得到的輸出流ServletOutputStream中即可。
主要程式碼如下:
public class DefaultServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
FileInputStream fis = new FileInputStream(request.getRealPath("/") + request.getServletPath().substring(1));
ServletOutputStream sos = response.getOutputStream();
byte[] buf = new byte[1024];
int len = -1;
while((len = fis.read(buf))!= -1){
sos.write(buf);
}
sos.flush();
sos.close();
fis.close();
}
}
小知識點:如何獲取瀏覽器的請求名?
假如在瀏覽器的位址列裡面輸入:http://localhost:880/Test/MyJSP.jsp
方法一:
String path1 = request.getRequestURI() 得到/Test/MyJsp.jsp
String path2 = request.getContextPath()得到/Test
path1.substring(path2.length())
方法二:
String path = request.getServletPath() 得到/MyJsp.jsp
path.substring(1)

(二)計數器
(1)用JSP實現記數
<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<html>
<head>
<title>My JSP 'view.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">
</head>
<%!int count = 0; %>
<body>
您是第<%=++count %>個訪問者。<br>
</body>
</html>

如果這樣定義count:<% int count = 0 %>就不能看到效果,通過檢視伺服器自動生成的servlet(在work/lib內),發現這種定義是在service方法中的,而向上面那種定義是在方法外面的,就成了成員變數,在下次呼叫時,count值仍然不變。這樣就可以實現了。但是當伺服器關閉了,那麼count就歸0了。

(2)用servlet編寫通用計數器
對單個檔案寫個計數器比較簡單,複雜的是寫一個通用的計數器,可以統計很多頁面。實施原理如下:
1、使用一個CountServlet來完成計數功能;主要程式碼如下:
public class CountServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
int count = -1;
try {
count = getCount(request);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
sendImage(response, count);
}
private void sendImage(HttpServletResponse response, int count){
try {
response.setContentType("image/jpeg");
ServletOutputStream sos = response.getOutputStream();
BufferedImage image = new BufferedImage(80, 20, BufferedImage.TYPE_INT_RGB);
Graphics g = image.getGraphics();
g.setColor(Color.BLACK);
g.fillRect(0, 0, 80, 20);
g.setColor(Color.WHITE);
g.setFont(new Font(null, Font.PLAIN|Font.BOLD, 18));
g.drawString(String.valueOf(count), 0, 18);
g.dispose();

ImageIO.write(image, "JPEG", sos);
sos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void sendText(HttpServletResponse response, int count){
try {
response.setContentType("text/html;charset=GB18030");
PrintWriter out = response.getWriter();
out.println("document.write('本頁面訪問次數為" + count + "')");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private int getCount(HttpServletRequest request) throws Exception{
int count = 0;
String path = request.getRealPath("/count.txt");
FileInputStream fis = new FileInputStream(path);

Properties prop = new Properties();
prop.load(fis);

String fileName = request.getHeader("referer");
System.out.println(fileName);
String tmp = (String)prop.get(fileName);
if(tmp!=null){
count = Integer.parseInt(tmp);
}
count++;
FileOutputStream fos = new FileOutputStream(path);
prop.put(fileName, new Integer(count).toString());
prop.store(fos, "success");
fis.close();
fos.close();
return count;
}
}
2、在多個頁面中寫入相同的一段程式碼,通過這段程式碼來訪問計數Servlet並得到各自的計數值,程式碼如下:
<script type="text/javascript" src="/CountServlet"></script>
這段程式碼中src就指出了哪個訪問Servlet。這段程式碼無非是想訪問src指出的路徑,然後得到一段javascript程式碼,這段程式碼把得到的計數值顯示出來,類似如下的程式碼:

document.write("訪問次數為n");

我們可以在servlet中輸出上面這段程式碼即可。

3、需要在CountServlet中得到請求來源頁面,並把這個頁面的資訊作為屬性檔案的key
得到請求頁面的方法是 request.getHeader("referer"),referer是一個特殊的請求頭,當通過超連結從一個頁面訪問另一個頁面時,瀏覽器會自動在請求資訊裡面加上這個頭,告知伺服器它來自哪裡。
<script type="text/javascript">
document.write("訪問次數為n");
</script>

五. 對日期格式化的例子
<%
DateFormat df = new SimpleDateFormat("yyyy年MM月dd日");
df.format(new Date());

DateFormat df2 = DateFormat.getDateInstance(DateFormat.LONG, request.getLocale());
df2.format(new Date());
%>
通過該例子而引出的知識點:
大知識點:抽象類的例項化方法,一是例項化其子類,二是通過getInstance方法得到
小知識點:Locale的作用(包含國家和地區的資訊),通過更改瀏覽器語言選項來演示講解。

相關文章