第一次web 项目的bug以及get到的好功能

这次项目已经跟学长讲过了,也暴漏了许多问题,在此将一些问题列出来,以防我下次再犯。

1.数据库连接池,链接上限问题。

	//修正前
	public static Connection getConnection() {
		ComboPooledDataSource dataSource = new ComboPooledDataSource();
		Connection connection = null;
			try {
				connection = dataSource.getConnection();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		return connection; // 返回MySQL数据库的链接对象
	}


    //修正后
	private static ComboPooledDataSource dataSource = new ComboPooledDataSource();
	public static Connection getConnection() {
		Connection connection = null;
			try {
				connection = dataSource.getConnection();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		return connection; // 返回MySQL数据库的链接对象
	}

获取数据连接池对象不能放在获取数据库连接对象的方法中,否则每次获取连接都会获取一个数据库连接池,导致数据库连接过多。

2.Cookie分隔符问题

今天想为项目添加一个浏览记录的功能,用Cookie存储浏览记录,由于我的数据是用逗号分隔,每次添加数据都添加不进去,最后在学长的帮助下,发现Cookie中不能含逗号,最后用#代替逗号解决了问题。

3.Servlet 3.0的新功能

由于我用的学习网站比较落后,学的是Servlet 2.5。每次创建一个Servlet类都去web.xml写配置,造成一些不必要的麻烦。在快交项目的时候发现了 @WebServlet注解,使用在本类上使用该注解和去web.xml写配置功能一样。

4.使用反射技术减少Servlet的数量(比用if语句更简单)

// java代码
@WebServlet("/test")
public class Demo extends HttpServlet {
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String jump = request.getParameter("jump");// 接受过来客户端需要的服务
		String path = null; // 用来接受需要跳转的路径
		Class class1 = this.getClass(); // 获得本类class
		try { // 利用反射技术减少Servlet的数量
			Method method = class1.getMethod(jump, HttpServletRequest.class,         
    HttpServletResponse.class);
			if (method != null) {
				path = (String) method.invoke(this, request, response);// 获取方法的返回值
			}
			if (path != null) {
				Forwarding.forwarding(path, request, response);// 这是一个用于实现请求转发 
    的工具类
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}


	public String demo(HttpServletRequest request, HttpServletResponse response) {
		return "demo.jsp";// 执行完该方法后需要请求转发的页面
	}
    
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
		throws ServletException, IOException {
		doGet(request, response);
	}

}

// 页面代码
<a href="test?jump=demo" >点我</a> // 点击后执行Demo类的demo方法然后跳转到demo.jsp界面

4.URL的保留字符

5.getParameter和getAttribute的区别

getParameter: 返回的是String类型,用来获取POST/GET传递的参数值,即 http://demo.jsp?id=1 中的1 或通过表单提交过来的数据。

getAttribute: 返回的是Object类型,用来获取对象容器中的数据值。即 session中的值 等其他作用域对象。

6. form标签的name属性和onsubmit属性

<form name="a" onsubmit="return b()"  action="" method="post">
</form>

a和b不能同名,否则不执行b()函数直接提交数据。

7.JS和JQ

我以前一直以为JQ中的$("#id")和JS中的document.getElementByIdx_x("id")得到的效果是一样的,直到这次写项目,通过测试得到:

1、alert($("#div"))得到的是[object Object]。

2、alert(document.getElementById("div"))得到的是[object HTMLDivElement]。

3、alert($("#div")[0])或者alert($("#div").get(0))得到的是[object HTMLDivElement]。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值