学习笔记

课程大纲

1、jsp表单数据更改
2、表单method get和post区别;
3、字符编码

课程笔记

一、jsp表单数据更改

//先通过id查询数据库,显示在表单中
<%
	String id = request.getParameter("id");
	String name ="";
	Class.forName("com.mysql.jdbc.Driver");
	Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/datt", "root", "1");
	PreparedStatement pst = conn.prepareStatement("SELECT name FROM stu WHERE id="+id);
	ResultSet rs =pst.executeQuery();
	if(rs.next()){
		name = rs.getString("name");
	}
	rs.close();
	pst.close();
	conn.close();
%>
//<%= %>直接输出变量、表达式内容
<form action="update.jsp?id=<%=id %>" method = "post" onsubmit="return edit()">
	<input type = "text" name = "username1" value ="<%=name%>"/>
	<input type = "submit" value = "修改">
</form>
//确认弹框事件
<script>
	var edit = function(){
		if(confirm("确认修改吗?")){
			return true;
		} else {
			return false;
		}
	}
</script>
<%

	request.setCharacterEncoding("utf-8");
	//获取editPre.jsp里面inputid和username的值
	String username1 = request.getParameter("username1");
	String id = request.getParameter("id");
	
	Class.forName("com.mysql.jdbc.Driver");
	Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/datt", "root", "1");
	//简化代码,用?后面setString和SetInt方法赋值;Integer.valueOf()--将String转化为int
	PreparedStatement pst = conn.prepareStatement("UPDATE stu SET name =? WHERE id=?");
	pst.setString(1, username1);
	pst.setInt(2,Integer.valueOf(id));
	int rs = pst.executeUpdate();
	//int rs = pst.executeUpdate();
	pst.close();
	conn.close(); 
	// jdbc编程
	out.print("<h1>修改"+rs+"条数据</h1>");
%>

二、表单里面method属性 get和post区别

<form action="update.jsp?id=<%=id %>" method = "post" >
	<input type = "text" name = "username1" value ="<%=name%>"/>
	<input type = "submit" value = "修改">
</form>

当里面的method为get时,传入的value = "<%=name%>会显示在URL上,安全性弱,且get不能重点内容传入action="update.jsp?id=<%=id %>"的id值
当里面的method为post时,id和name都会传入下一页面,且值不会显示在URL地址上,安全性高;

三、更改字符编码
①eclipses–>windows–>preferences–>搜索Encoding–>utf-8
②Servers–>server.xml–>端口号加URIEncoding=“UTF-8”
③表单method为post时加request.setCharacterEncoding(“utf-8”);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值