Servlet學習筆記(設定servlet的字符集、生命週期、對映配置、資料庫連線引數的區域性配置)
阿新 • • 發佈:2018-11-28
- 設定servlet的字符集
servlet是sun公司提供的一門用於開發動態web資源的技術,使用java語言編寫的執行在伺服器端的程式,通過http超文字傳輸協議接受和響應來自客戶端的請求。
servlet的產生是把“html標記”和“大量的業務處理邏輯”給分開,繼續留在html頁面的“html標記”就形成了靜態網頁,而“大量的業務處理邏輯”就放到伺服器上形成了servlet;
//設定響應物件的字符集 response.setContentType("text/html;charset=utf-8"); response.setCharacterEncoding("utf-8");
- servlet的生命週期
servlet | 流程 |
---|---|
例項化 | servlet容器呼叫構造器建立servlet的例項 |
初始化 | 改容器呼叫init()方法進行初始化 |
服務 | 呼叫doGet()方法響應使用者的請求 |
銷燬 | 伺服器停止前呼叫destory()方法銷燬例項 |
不可用 | 銷燬例項並標記為垃圾,等待垃圾回收機制的回收 |
-
Tomcat 與 Servlet 是如何工作的:
-
Web Client 向Servlet容器(Tomcat)發出Http請求
-
Servlet容器接收Web Client的請求
-
Servlet容器建立一個HttpRequest物件,將Web Client請求的資訊封裝到這個物件中。
-
Servlet容器建立一個HttpResponse物件
-
Servlet容器呼叫HttpServlet物件的service方法,把HttpRequest物件與HttpResponse物件作為引數傳給 HttpServlet 物件。
-
HttpServlet呼叫HttpRequest物件的有關方法,獲取Http請求資訊。
-
HttpServlet呼叫HttpResponse物件的有關方法,生成響應資料。
-
servlet的對映配置
由於客戶端是通過url地址訪問web伺服器中的資源,所以servlet程式想被外界訪問,必須把servlet程式對映到一個url地址上,這個工作在web.xml檔案中使用和完成。
註冊servlet
<servlet>
<servlet-name>Servlet1</servlet-name>
<servlet-class>com.offcn.study.Servlet1</servlet-class>
</servlet>
對映一個已註冊的servlet的一個對外訪問路徑
<servlet-mapping>
<servlet-name>Servlet1</servlet-name>
<url-pattern>/study/Servlet1</url-pattern>
</servlet-mapping>
- servlet程式(將訪問資料庫引數寫在程式碼中)
package com.offcn.study;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class AddCustomer extends HttpServlet {
/**
* Constructor of the object.
*/
public AddCustomer() {
super();
}
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String driver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/copy";
String user= "root";
String password = "root";
Connection con = null;
Statement st = null;
String msg = "";
try {
Class.forName(driver);
con = DriverManager.getConnection(url, user, password);
String sql ="insert into customer(cid,cname,cpassword,mobile) values('C99','咕咚','3639','78945612')";
st = con.createStatement();
int iCount = st.executeUpdate(sql);
msg=iCount>0?"執行成功":"執行失敗";
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
if(st!=null){
st.close();
}
if(con!=null){
con.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
out.println("<p>"+msg+"</p>");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
out.print(" This is ");
out.print(this.getClass());
out.println(", using the POST method");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}
}
- 將資料庫連線引數區域性配置在配置檔案中(web.xml)
配置檔案中的程式碼
<servlet>
<servlet-name>AddCustomer</servlet-name>
<servlet-class>com.offcn.study.AddCustomer</servlet-class>
<init-param>
<param-name>driver</param-name>
<param-value>com.mysql.jdbc.Driver</param-value>
</init-param>
<init-param>
<param-name>url</param-name>
<param-value>jdbc:mysql://localhost:3306/copy</param-value>
</init-param>
<init-param>
<param-name>uer</param-name>
<param-value>root</param-value>
</init-param>
<init-param>
<param-name>password</param-name>
<param-value>root</param-value>
</init-param>
</servlet>
servlet中需要從web.xml中獲取連線引數,用servlet物件的ServletConfig物件。
String driver = "";
String url = "";
String user= "";
String password = "";
ServletConfig config = this.getServletConfig();//獲取ServletConfig物件
driver= config.getInitParameter("driver");//呼叫方法,使用名字獲取引數
url = config.getInitParameter("url");
user = config.getInitParameter("user");
password = config.getInitParameter("password");