[code=HTML<%@ page language="java" contentType="text/html;charset=gb2312"%> 
<%@page import="java.util.*,java.sql.*,my.*"%>
<html>
<head>
</head>
<body>
<%
request.setCharacterEncoding("gb2312");
    String str=request.getParameter("id");
    if(str==null || str.trim().equals(""))
    {
        out.print("error");
        return;
    }
        String str1=request.getParameter("rootid");
    if(str1==null || str1.trim().equals(""))
    {
        out.print("error");
        return;
    }
    int id=0;
    int rootId=0;
    try{
    id=Integer.parseInt(str);
    rootId=Integer.parseInt(str1);
    }catch(NumberFormatException e){
    out.print("error again");
    return;
    }
%>    <form action="replydeal.jsp" method="post">
    <input type="hidden" name="pid" value="<%=id %>"/>
    <input type="hidden" name="rootid" value="<%=rootId %>"/>
    标题:<input type="text" name="title"/><br>
    内容: <textarea name="cont" rows="20" cols="80"></textarea>
    <input type="submit" value="提交"/>
    </form>
</body>
</html>[/code]<%@ page language="java" contentType="text/html;charset=gb2312"%> 
<%@page import="java.util.*,java.sql.*,my.*"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'replideal.jsp' starting page</title>
    
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">    
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->  </head>
  
 <%
    String str=request.getParameter("pid");
    if(str==null || str.trim().equals(""))
    {
        out.print("error");
        return;
    }
        String str1=request.getParameter("rootid");
    if(str1==null || str1.trim().equals(""))
    {
        out.print("error");
        return;
    }
    int pid=0;
    int rootId=0;
    try{
    pid=Integer.parseInt(str);
    rootId=Integer.parseInt(str1);
    }
    catch(NumberFormatException e){
    out.print("error again");
    return;
    }
    request.setCharacterEncoding("gb2312");
    String title=request.getParameter("title");
    String cont=request.getParameter("cont");
    out.print(title);
    Connection conn=DB.getConn();
    conn.setAutoCommit(false);
    String sql="insert into article values (null,?,?,?,?,now(),?)";     
    
    PreparedStatement pstmt=DB.getPstmt(conn,sql);
    pstmt.setInt(1,pid);
    pstmt.setInt(2,rootId);
    pstmt.setString(3,title);
    pstmt.setString(4,cont);
    pstmt.setInt(5,0);
    pstmt.executeUpdate();    Statement stmt=DB.getStmt(conn);
    String q=null;
    try{
    q="update article set isleaf=1 where id="+pid;
    stmt.executeUpdate(q);
    conn.commit();
    conn.setAutoCommit(true);   }catch(SQLException e){
   out.print(q);//update article set isleaf=1 where id=25 3秒
   }
     DB.close(stmt);
     DB.close(pstmt);
    DB.close(conn);
 %>
  
  <body>
    
<script language="JavaScript1.2" type="text/javascript">
<!--
//  Place this in the 'head' section of your page.  function delayURL(url, time) {
    setTimeout("top.location.href='" + url + "'", time);
}//--></script>
3秒后自动跳转,如果没有跳转请点击下面连接
<a href="MyJsp.jsp">跳转</a>
<script type="text/javascript">
delayURL("MyJsp.jsp",3000);
</script>
  </body>
</html>这为什么会有乱码,
    request.setCharacterEncoding("gb2312");
    String title=request.getParameter("title");
    String cont=request.getParameter("cont");
    out.print(title);这边打印出来的是乱码,插入数据库里的也是乱码,其它页面都不会这样,同样都是 request.setCharacterEncoding("gb2312");,别的页面都不会有乱码问题,我还特地写了两个test页面测试下,也不会有乱码啊

解决方案 »

  1.   

    你用的是mysql吧?应该在配置文件改一下吧
      

  2.   

       对,先把 mySQL 的配置文件中的编码格式改一下;再不行用个过滤器。
      

  3.   

    其它页面用个同样的编码格式出入数据库都没问题,所以跟mysql的编码没关系吧
      

  4.   

    你的MySQL的编码是?
    如果是UTF-8,我建议你全部统一编码为UTF-8
      

  5.   

    <%@ page language="java" contentType="text/html;charset=gb2312"%> 
       -----   charset=utf-8