黄色网页视频 I 影音先锋日日狠狠久久 I 秋霞午夜毛片 I 秋霞一二三区 I 国产成人片无码视频 I 国产 精品 自在自线 I av免费观看网站 I 日本精品久久久久中文字幕5 I 91看视频 I 看全色黄大色黄女片18 I 精品不卡一区 I 亚洲最新精品 I 欧美 激情 在线 I 人妻少妇精品久久 I 国产99视频精品免费专区 I 欧美影院 I 欧美精品在欧美一区二区少妇 I av大片网站 I 国产精品黄色片 I 888久久 I 狠狠干最新 I 看看黄色一级片 I 黄色精品久久 I 三级av在线 I 69色综合 I 国产日韩欧美91 I 亚洲精品偷拍 I 激情小说亚洲图片 I 久久国产视频精品 I 国产综合精品一区二区三区 I 色婷婷国产 I 最新成人av在线 I 国产私拍精品 I 日韩成人影音 I 日日夜夜天天综合

java 培訓(xùn)日記

系統(tǒng) 2236 0
進(jìn)行了一周緊張后的學(xué)習(xí),今天終于閑下把日記與大家分享,在這周里張老師給我們講了好多未來我們?cè)诠ぷ髦谐霈F(xiàn)的一些問題,我記得不是很好!希望大家多提建議!讓我更好的掌握J(rèn)AVA
一、myeclipse的安裝和基本使用
1、安裝路徑最好不帶有空格;
2、將Tomcat置于myeclipse的控制之下;
3、建立Web Project,以及發(fā)布到Tomcat服務(wù)器;
4、myeclipse常用的快捷鍵列表如下:
?? Alt + / ??? 代碼提示
?? Ctrl + shift + o ??? 導(dǎo)入包
?? Ctrl + shift + f ??? 代碼格式化
?? Alt? + shift + s 彈出使用右鍵source的菜單
?? Alt? + shift + z 代碼包含(如try/catch)
?? Alt? + shift + /???? 將代碼注釋掉
?? Alt? + shift + \???? 取消對(duì)代碼的注釋

二、JSP的部分基礎(chǔ)知識(shí)
1、指令元素:page指令,include指令,taglib指令。指令必須嵌套在<%@?? %>中
2、腳本片段,在里面直接寫java源代碼 <%???????? %>
3、腳本表達(dá)式 <%=??????? %>
4、腳本聲明 <%!??????? %>
5、JSP注釋 <%--???? --%>

三、Tomcat中重要的幾個(gè)Servlet(在\conf\web.xml中查看)
?? DefaultServlet(缺省Servlet) org.apache.catalina.servlets.DefaultServlet
?? 作用:處理服務(wù)器中某個(gè)靜態(tài)資源,例如加載靜態(tài)的網(wǎng)頁(yè),圖片。如果在conf目錄下的web.xml中注釋掉這個(gè)servlet,則我們發(fā)布的靜態(tài)網(wǎng)頁(yè)不會(huì)顯示。(還要注釋掉<servlet-mapping>)
?? InvokerServlet(Servlet激活器) org.apache.catalina.servlets.InvokerServlet
?? 作用:激活和調(diào)用任何其他Servlet,需要在全局的Web.xml中配置,默認(rèn)被注釋掉,如果去掉注釋,則我們不用自己在我們的web.xml中配置我們寫的servlet,服務(wù)器也會(huì)自動(dòng)幫我們加載。
?? JspServlet ??????? org.apache.jasper.servlet.JspServlet
?? 作用:編譯和執(zhí)行jsp頁(yè)面

四、兩個(gè)案例
(一)模擬DefaultServlet輸出靜態(tài)文件的內(nèi)容
1、Tomcat系統(tǒng)的DefaultServlet的作用就是讀取靜態(tài)資源然后輸出到客戶端瀏覽器,為了能驗(yàn)證程序的效果,我們需要把系統(tǒng)conf\web.xml中關(guān)于它的配置注釋掉;
2、新建一個(gè)Servlet,假定我們只讓這個(gè)servlet攔截對(duì)Html文件的訪問,就把映射路徑設(shè)置為*.html;
3、在servlet里面得到瀏覽器請(qǐng)求的文件名,得到輸入流,再把輸入流讀到數(shù)組當(dāng)中,再把數(shù)組內(nèi)容寫入到由response對(duì)象得到的輸出流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();
}
}
小知識(shí)點(diǎn):如何獲取瀏覽器的請(qǐng)求名?
假如在瀏覽器的地址欄里面輸入: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)

(二)計(jì)數(shù)器
(1)用JSP實(shí)現(xiàn)記數(shù)
<%@ 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 %>個(gè)訪問者。<br>
? </body>
</html>

如果這樣定義count:<% int count = 0 %>就不能看到效果,通過查看服務(wù)器自動(dòng)生成的servlet(在work/lib內(nèi)),發(fā)現(xiàn)這種定義是在service方法中的,而向上面那種定義是在方法外面的,就成了成員變量,在下次調(diào)用時(shí),count值仍然不變。這樣就可以實(shí)現(xiàn)了。但是當(dāng)服務(wù)器關(guān)閉了,那么count就歸0了。

(2)用servlet編寫通用計(jì)數(shù)器
對(duì)單個(gè)文件寫個(gè)計(jì)數(shù)器比較簡(jiǎn)單,復(fù)雜的是寫一個(gè)通用的計(jì)數(shù)器,可以統(tǒng)計(jì)很多頁(yè)面。實(shí)施原理如下:
1、使用一個(gè)CountServlet來完成計(jì)數(shù)功能;主要代碼如下:
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('本頁(yè)面訪問次數(shù)為" + 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、在多個(gè)頁(yè)面中寫入相同的一段代碼,通過這段代碼來訪問計(jì)數(shù)Servlet并得到各自的計(jì)數(shù)值,代碼如下:
<script type="text/javascript" src="/CountServlet"></script>
這段代碼中src就指出了哪個(gè)訪問Servlet。這段代碼無非是想訪問src指出的路徑,然后得到一段javascript代碼,這段代碼把得到的計(jì)數(shù)值顯示出來,類似如下的代碼:

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

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

3、需要在CountServlet中得到請(qǐng)求來源頁(yè)面,并把這個(gè)頁(yè)面的信息作為屬性文件的key
得到請(qǐng)求頁(yè)面的方法是 request.getHeader("referer"),referer是一個(gè)特殊的請(qǐng)求頭,當(dāng)通過超鏈接從一個(gè)頁(yè)面訪問另一個(gè)頁(yè)面時(shí),瀏覽器會(huì)自動(dòng)在請(qǐng)求信息里面加上這個(gè)頭,告知服務(wù)器它來自哪里。
<script type="text/javascript">
document.write("訪問次數(shù)為n");
</script>

五. 對(duì)日期格式化的例子
? <%
? DateFormat df = new SimpleDateFormat("yyyy年MM月dd日");
? df.format(new Date());
?
? DateFormat df2 = DateFormat.getDateInstance(DateFormat.LONG, request.getLocale());
df2.format(new Date());
? %>
通過該例子而引出的知識(shí)點(diǎn):
大知識(shí)點(diǎn):抽象類的實(shí)例化方法,一是實(shí)例化其子類,二是通過getInstance方法得到
小知識(shí)點(diǎn):Locale的作用(包含國(guó)家和地區(qū)的信息),通過更改瀏覽器語(yǔ)言選項(xiàng)來演示講解。

java 培訓(xùn)日記


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號(hào)聯(lián)系: 360901061

您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對(duì)您有幫助就好】

您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長(zhǎng)會(huì)非常 感謝您的哦?。?!

發(fā)表我的評(píng)論
最新評(píng)論 總共0條評(píng)論