问题描述:
1,页面中有request.setCharacterEncoding("gbk"),但是查看数据库时发现存进数据库里的中文都变成了问号;
2,这个是主要不懂的地方:传递过来的pid的确不是0(红色字体的语句所得到的结果不是0),但是却执行了当pid==0时的if语句(执行了蓝色字体语句)
本人是初学者,请各位指点指点,谢谢!
(开发工具:lomboz eclipse + mysql5.0 + tomcat5.5 )
代码如下:
<%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<%@ page import="com.parckle.shopping.*" %>
<%
request.setCharacterEncoding("GBK");
int pid=0;
String action=request.getParameter("action");
String strPid=request.getParameter("pid");
if(strPid!=null){
pid=Integer.parseInt(strPid);
out.print(pid);
}
if(action!=null&&action.equals("add")){
String name=request.getParameter("name");
String descr=request.getParameter("descr");
if(pid==0){
Category.add(name,descr);
out.print("添加成功!pid="+pid);
}
else{
Category.addChildCategory(pid,name,descr);
out.print("添加成功!"+pid);
}
}
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>Insert title here</title>
</head>
<body>
<center>添加新类别</center>
<form action="CategoryAdd.jsp" method="post">
<input type="hidden" name="action" value="add"/>
<table>
<tr>
<td>类别名称:</td>
<td><input type="text" name="name"></td>
</tr>
<tr>
<td>类别描述:</td>
<td><textarea name="descr" cols=40 rows=6></textarea></td>
</tr>
<tr><td><input type="submit" name="submit" value="提交"></td></tr>
</table>
</form>
</body>
</html>
1,页面中有request.setCharacterEncoding("gbk"),但是查看数据库时发现存进数据库里的中文都变成了问号;
2,这个是主要不懂的地方:传递过来的pid的确不是0(红色字体的语句所得到的结果不是0),但是却执行了当pid==0时的if语句(执行了蓝色字体语句)
本人是初学者,请各位指点指点,谢谢!
(开发工具:lomboz eclipse + mysql5.0 + tomcat5.5 )
代码如下:
<%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<%@ page import="com.parckle.shopping.*" %>
<%
request.setCharacterEncoding("GBK");
int pid=0;
String action=request.getParameter("action");
String strPid=request.getParameter("pid");
if(strPid!=null){
pid=Integer.parseInt(strPid);
out.print(pid);
}
if(action!=null&&action.equals("add")){
String name=request.getParameter("name");
String descr=request.getParameter("descr");
if(pid==0){
Category.add(name,descr);
out.print("添加成功!pid="+pid);
}
else{
Category.addChildCategory(pid,name,descr);
out.print("添加成功!"+pid);
}
}
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>Insert title here</title>
</head>
<body>
<center>添加新类别</center>
<form action="CategoryAdd.jsp" method="post">
<input type="hidden" name="action" value="add"/>
<table>
<tr>
<td>类别名称:</td>
<td><input type="text" name="name"></td>
</tr>
<tr>
<td>类别描述:</td>
<td><textarea name="descr" cols=40 rows=6></textarea></td>
</tr>
<tr><td><input type="submit" name="submit" value="提交"></td></tr>
</table>
</form>
</body>
</html>
if(pid==0){
Category.add(name,descr);
out.print("添加成功!pid="+pid);
}
else{
Category.addChildCategory(pid,name,descr);
out.print("添加成功!"+pid);
}
等于0和不为0 的结果很相似哟
的存储没有关系。
第二个问题,就奇怪了,前边的输出不是0
后边却变成零了????
pageEncoding="GB18030"%>
request.setCharacterEncoding("GBK"); 红色部分都设成GBK2. 表单里没看到pid...
况且表单里没有提交“pid”的值,
String strPid=request.getParameter("pid");
if(strPid!=null){
pid=Integer.parseInt(strPid);
out.print(pid);
} 是不能执行的,所以pid的值一直是0.
pid是另外的文件传递过来的,具体如下:
<%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<%@ page import="com.bjsxt.shopping.*,java.util.*" %>
<%
List<Category> categories=Category.getCategories();
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>Insert title here</title>
</head>
<body>
<table border="1" align="center">
<tr>
<td align="center">ID</td>
<td align="center">NAME</td>
<td align="center">pid</td>
<td align="center">grade</td>
<td>处理</td>
</tr>
<%
for(Iterator<Category> it=categories.iterator();it.hasNext();){
Category c=it.next();
String str="";
for(int i=1;i<c.getGrade();i++){
str+="--";
}
%>
<tr>
<td align="center"><%=c.getId() %></td>
<td align="center"><%=str+c.getName() %></td>
<td align="center"><%=c.getPid() %></td>
<td align="center"><%=c.getGrade() %></td>
<td align="center"><a href="CategoryAdd.jsp?pid=<%=c.getId()%>">添加子类别</a>
</tr>
<%
}
%>
</table>
</body>
<html>
所以这个时候CategoryAdd.jsp什么都没干,就是显示个表单,对吧楼主?
然后呢?你把表单再提交给CategoryAdd.jsp,这时候action和其他表单域都有了,开始处理流程了。注意这时候pid在哪儿呢?没有跟表单一起提交啊,CategoryAdd.jsp没那么聪明,不知道这次跟上次是一个人提交的。
也就是他第一次渲染表单的时候,pid用完就已经抛弃了。如果只是想改bug的话,应该是表单再建个pid的隐藏域,或者form 的 action里边写
而你pid在jsp中初始为0