Before Spring

Spring Framework is not used before, the use of a single Servlet for data interaction.

Request Flow: client sends a request, the request reaches the container, the request is parsed into a http request, the servlet web.xml matching path according to a request parameter, this servlet initialization, and httpServletRequest packaged together and passed into this empty HttlServletResponse servlet's service () method, execute business logic, would be required to return the response data into a buffer, and after the end of the method, the data in the buffer container packaging taken back to the client in response httpResponse.

Url configured access path, filters and listeners and their respective initialization parameters based on the configuration of the web.xml web project

<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>

Monitor

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();
		}
	}
}

filter

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);
		}
	}

}

Database Tools

Database using the original underlying connection

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();
		}
	}
	
}

 

Published 10 original articles · won praise 0 · Views 205

Guess you like

Origin blog.csdn.net/fantasyleaves/article/details/104683385