進行了一周緊張后的學習,今天終于閑下把日記與大家分享,在這周里張老師給我們講了好多未來我們在工作中出現的一些問題,我記得不是很好!希望大家多提建議!讓我更好的掌握JAVA
一、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的作用(包含國家和地區的信息),通過更改瀏覽器語言選項來演示講解。

一、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的作用(包含國家和地區的信息),通過更改瀏覽器語言選項來演示講解。
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
