- 设置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");