JSP学习心得,干

1、form的action=""为空,所以可以在本页面完成数据存储。如果action直接填写action="XXX.jsp", 
那么在XXX.jsp页面是接收不到值的,因为还没来得及存储数据就已经跳过去了。


如果想用form的action="XXX.jsp"不为空完成数据存储,两种方法(情况)。 
一、还是要在本页面存储数据。这时就需要再来一个临时jsp页面用来做跳板了,action="临时页面.jsp"的内容填写临时页面。
用临时页面里面来存储数据,存储完再跳到XXX.jsp页面。  
二、直接跳到去XXX.jsp页面,action="XXX.jsp"。然后在XXX.jsp存储数据,当然也可以获取数据。
这种情况就可以<jsp:useBean scope="request"/>这样用了。因为存储和获取数据是同一个页面。


2、XXX1.jsp(含表单,提交)和XXX2.jsp里面都用<jsp:useBean scope="session"/>。
如果都是scope="request",在本页面存储的数据只能在本页面使用,在本页面跳到另外一个页面之后,
存在JavaBean里面的数据就失效了, 这样在XXX2.jsp界面就不能获得XXX1.jsp提交的数据了。 


3、 关于转码,两种方式。一、可以存储数据之前转码。二、也可以在获取数据时转码。
request.setCharacterEncoding("utf-8");

try {
//方法一
byte b[]=str.getBytes("utf-8");
str=new String(b);
//方法二,直接这样也行
str=new String(str.getBytes("utf-8"));
//方法三
str=new String(str.getBytes("ISO-8859-1"),"utf-8");

catch (UnsupportedEncodingException e) {
str="Have Exception:";
e.printStackTrace();
}


4、数据库查询避免Unknown column ‘xxx’ in ‘where clause’
但凡写过sql语句的人估计都曾经碰到过类似于Unknown column ‘xxx’ in ‘where clause’的问题。 单从字面理解,我们很容易得出
列名不存在的结论,但是,很多时候起始并不是由于列名出错造成的。而是由于拼凑sql语句时对字符类型数据没有用引号引起来造成的。
例如:
一个sql语句:
String who="root";
String getUname="select Uname,Uauthority from user where Uname = +"who" ";
或者String getUname="select Uname,Uauthority from user where Uname =" +who;
或者String getUname="select Uname,Uauthority from user where Uname = who ";
则错误如下:
Unknown column‘who' in ‘where clause’
sql中如果who是整型的倒不会出现什么错误,而如果sql中字符串类型【必须】要包含在引号内。
所以修改sql为:String getUname="select Uname,Uauthority from user where Uname = '"+who+"' ";
则错误消失。

5、
java application 异常Before start of result set
ResultSet rs=st.executeQuery();
System.out.println(rs.getString("Name"));
问题出在这里,不可以这么用
这样才正确
String name = "";
if(rs.next()){//或者while(rs.next()) 
   name = rs.getString("Name");
   if(name == null){
        name = "";
   }
   System.out.println(name);

即使你十分确定能搜出记录,也不可以在没有rs.next()之前直接对rs进行取值。这涉及到rs对象的存储方法。
里面说白了就是指针。没next,指针根本没指向对应记录































猜你喜欢

转载自blog.csdn.net/luochao5862426/article/details/78896309
今日推荐