课程大纲
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”);