在不使用Spring框架前,单使用Servlet进行数据交互。
请求流程:客户端发起请求,请求到达容器,将请求解析成http请求,根据请求路径参数匹配web.xml中的servlet,初始化此servlet,并将包装好的httpServletRequest和空的HttlServletResponse一并传入此servlet的service()方法中,执行业务逻辑,将需返回的数据写入response的缓冲区,方法结束后,容器将缓冲区中的数据取出包装在httpResponse响应中返回给客户端。
在web工程的基础配置web.xml中配置好url访问路径,过滤器和监听器及各自的初始化参数
<display-name>web configurer</display-name>
<context-param>
<param-name>context</param-name>
<param-value>from context param</param-value>
</context-param>
<!-- 配置监听器 -->
<listener>
<listener-class>
com.muchen.listener.ServletListener
</listener-class>
</listener>
<!-- 配置过滤器 -->
<filter>
<filter-name>mu</filter-name>
<filter-class>com.muchen.filter.ServletFilter</filter-class>
<init-param>
<param-name>fileName</param-name>
<param-value>send.dat</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>mu</filter-name>
<url-pattern>/mu/*</url-pattern>
</filter-mapping>
<!-- 配置servlet -->
<servlet>
<servlet-name>muchen</servlet-name>
<servlet-class>com.muchen.web.MainServlet</servlet-class>
<init-param>
<param-name>conf</param-name>
<param-value>true</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>muchen</servlet-name>
<url-pattern>/mu/*</url-pattern>
</servlet-mapping>
监听器
public class ServletListener implements ServletContextListener {
@Override
public void contextDestroyed(ServletContextEvent arg0) {
}
@Override
public void contextInitialized(ServletContextEvent arg0) {
System.out.println(arg0.getServletContext().getInitParameter("context"));
// 获取指定文件的URL:this.getClass().getClassLoader().getResource("SensitiveWords.txt")
// 获取指定文件的URL:this.getClass().getClassLoader().getResource("SensitiveWords.txt").getPath()
System.out.println(this.getClass().getClassLoader().getResource("SensitiveWords.txt").getPath());
System.out.println(this.getClass().getClassLoader().getResource("/SensitiveWords.txt").getPath());
InputStreamReader is;
try {
// 获取指定文件的字节输入流:this.getClass().getClassLoader().getResourceAsStream(("SensitiveWords.txt"))
is = new InputStreamReader(this.getClass().getClassLoader().getResourceAsStream(("SensitiveWords.txt")),"utf-8");
BufferedReader bf = new BufferedReader(is);
System.out.println(bf.readLine());
is.close();
bf.close();
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
过滤器
public class ServletFilter implements Filter {
@Override
public void destroy() {
}
@Override
public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2) throws IOException, ServletException {
System.out.println(arg0.getServerName());
arg1.setContentType("text/html;charset=utf-8");
arg2.doFilter(arg0, arg1);
}
@Override
public void init(FilterConfig arg0) throws ServletException {
// 获取配置参数
System.out.println(arg0.getInitParameter("fileName"));
}
}
servlet
@SuppressWarnings("serial")
public class MainServlet extends HttpServlet{
public void doGet(HttpServletRequest request, HttpServletResponse response){
System.out.println(this.getServletConfig().getInitParameter("conf"));
System.out.println(this.getServletContext().getInitParameter("context"));
Connection conn = null;
PreparedStatement stat = null;
ResultSet result = null;
Utils u = new Utils();
String msg = request.getParameter("msg");
try {
String sql = "SELECT * FROM XZQH where qhjb=?";
conn = u.getConnection();
stat = conn.prepareStatement(sql);
stat.setString(1, msg);
result = stat.executeQuery();
StringBuffer buffer = new StringBuffer("");
while(result.next()){
buffer.append(result.getString("QHMC")+"\n");
}
// 写入response缓冲区
response.getWriter().write(buffer.toString());
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
u.release(stat, result, conn);
}
}
}
数据库工具类
使用的是数据库底层原始的连接方式
public class Utils {
private static String driver;
private static String url;
private static String username;
private static String password;
static{
Properties prop = new Properties();
try {
//获取数据库连接信息加入property
prop.load(Utils.class.getClassLoader().getResourceAsStream("db.properties"));
driver = prop.getProperty("driver.name");
url = prop.getProperty("db.url");
username = prop.getProperty("db.username");
password = prop.getProperty("db.password");
} catch (IOException e) {
e.printStackTrace();
}
}
public Connection getConnection() throws SQLException, ClassNotFoundException{
Class.forName(driver);
return DriverManager.getConnection(url, username, password);
}
public void release(PreparedStatement stat, ResultSet result, Connection conn) {
try {
if (result != null) {
result.close();
}
if (stat != null) {
stat.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}