雜亂的知識點

//java 日期處理
Date d=new Date();
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
String now=sdf.format(d);
//System.out.println(now);
out.println(now);
-----------------------------
//servlet 配置文件的讀取web.xml-Context Params和Init Params
eg:
//在 server 中讀取:
PrintWriter out = response.getWriter();
out.println(this.getServletConfig().getInitParameter("jubuconfig")); //讀取局部配置;
out.println(this.getServletConfig().getServletContext().getInitParameter("quanjuconfig")); // 讀取全局配置;

//在jsp中也能夠讀取Servlet的全局配置:
<%=config.getServletContext().getInitParameter("quanjuconfig") %> <!-- 在jsp中由jsp的內置對象config讀取Servlet對象配置-->

//GenericServlet的兩個init方法
javax.servlet.GenericServlet 中 public void init(ServletConfig config) throws ServletException 方法用於 tomcat 傳遞ServletConfig 到全局變量 config中,而後調用空的方法 public void init() throws ServletException。
通常重寫 init() 這個方法。出於this.getServletConfig()==null考慮。

//Servlet的生命週期
webapp中的servlet頁面html

在客戶端第一次請求服務器時,java

一、加載相關的Servlet類,使用ClassLoader:類加載器。而後web

二、由tomcat經過從HttpServlet繼承來的構造方法new出來這個類對象,或說是實例化出一個對象,而且只有這一個對象(在同一個容器中)。tomcat

三、初始化:調用HttpServlet的init(ServletConfig config)方法。服務器

//只執行一次,在第一次初始化的時候app

//config 中保存着這個Servlet 的配置信息(web.xml 中的配置信息)webapp

四、處理請求:調用HttpServlet的父類GenericServlet的void service(ServletRequest req,ServletResponse res),這個方法再調用HttpServlet的protected void servicejsp

(HttpServletRequest req,HttpServletResponse resp)throws ServletException,IOException,這個方法再調用HttpServlet的相應的doPost\doGet等方法,this

處理的是客戶端的請求。編碼

五、在適當時機退出服務 destory()。如:webApplication 退出的時候調用,或者web應用程序重啓後等。

tomcat服務器事先建立好空閒的線程(線程池),等待衆多客戶端請求,共享一個servlet對象。

//tomcat 下解決亂碼問題

傳到的頁面內開始處寫以下代碼:
 <%@page contentType="text/html;charset=gbk"%>
<%request.setCharacterEncoding("gbk");%>

實在不成,以下手工轉碼:

String str=new String("astr".getBytes("ISO8859_1"),"gbk");

將任意字符串如astr,按照ISO8859_1的編碼方式按照gbk字符編碼方式取出來。

相關文章
相關標籤/搜索