在ultraEdit里面写了一个JavaBean,部分代码如下:
String weekday[] = {"星期日","星期一","星期二","星期三","星期四","星期五","星期六"};
String tem="";
String str="";
StringBuffer sb = new StringBuffer();
for(int k=0;k<7;k++)
{
try
{
tem = new String(weekday[k].getBytes("ISO-8859-1"),"gb2312");
}catch(Exception e){}
sb.append("<td>"+tem+"</td>");
}
str = new String(sb);
return str;
JSP页面调用该Bean后出现一串问号,请问大家怎么处理?
String weekday[] = {"星期日","星期一","星期二","星期三","星期四","星期五","星期六"};
String tem="";
String str="";
StringBuffer sb = new StringBuffer();
for(int k=0;k<7;k++)
{
try
{
tem = new String(weekday[k].getBytes("ISO-8859-1"),"gb2312");
}catch(Exception e){}
sb.append("<td>"+tem+"</td>");
}
str = new String(sb);
return str;
JSP页面调用该Bean后出现一串问号,请问大家怎么处理?
jsp上设置charset=gbk
public static boolean isGBK(String string) throws Exception{
byte[] bytes =string.replace('?', 'a').getBytes("ISO-8859-1");
for (int i = 0; i < bytes.length; i++) {
if(bytes[i]==63){
return true;
}
}
return false;
}
public String toGBK(String string) throws Exception{
if(!isGBK(string)){
return new String(string.getBytes("ISO-8859-1"),"GBK");
}
return string;
}
}
这段代码是以前在网上收集的,挺不错。我代作者贴出来嘞。。
String tem="";
String str="";
StringBuffer sb = new StringBuffer();
for(int k=0;k <7;k++)
{
/**
try
{
tem = new String(weekday[k].getBytes("ISO-8859-1"),"gb2312");
}catch(Exception e){}
*/
sb.append(" <td>"+tem+" </td>");
}
str = new String(sb);
return str;
public String toGBK(String str)
{
try
{
if(str==null)
str = "";
else
str=new String(str.getBytes("ISO-8859-1"),"GBK");
}catch (Exception e) {}
return str;
}
<meta http-equiv="content-type" content="text/html; charset=GB2312">
</head>
加上看下
2、加一个过滤器
public class MyFilter extends HttpServlet implements Filter {
private static final long serialVersionUID = 1L;
private FilterConfig filterConfig;
public void init(FilterConfig filterConfig) throws ServletException {
this.filterConfig = filterConfig;
} public void doFilter(ServletRequest request, ServletResponse response,
FilterChain filterChain) {
try {
request.setCharacterEncoding("UTF-8"); //设置中文
filterChain.doFilter(request, response);
} catch (ServletException sx) {
filterConfig.getServletContext().log(sx.getMessage());
} catch (IOException iox) {
filterConfig.getServletContext().log(iox.getMessage());
}
}
public void destroy() {
}
}
不过还有一点,你的整个程序要使用相同的编码
加了,str=new String(str.getBytes("ISO-8859-1"),"GBK");
也加了,还是没解决问题,估计是ultraEdit的编码问题,我再去把保存编码的格式改改试试,谢谢大伙^_^
import java.util.*;public class CalendarBean
{
String cale;
int year,month;
public CalendarBean()
{
cale=null;
year=-1;
month=-1;
}
public void setYear(int year)
{
this.year=year;
}
public int getYear()
{
return year ;
}
public void setMonth(int month)
{
this.month=month;
}
public int getMonth()
{
return month;
}
public String getCale()
{
StringBuffer sb = new StringBuffer();
Calendar rili = Calendar.getInstance();
rili.set(year,month-1,1);
int dayOfweek = rili.get(Calendar.DAY_OF_WEEK)-1;
int day = 0;
if(month==1 ||month==3 ||month==5 ||month==7 ||month==8 ||month==10 ||month==12)
{
day = 31;
}
if(month==4 ||month==6 ||month==9 ||month==1)
{
day=30;
}
if(month == 2)
{
if( ((year%4==0)&& (year%100!=0)) || (year%400==0) )
{
day = 29;
}
else
{
day = 28;
}
}
String a[] = new String[42];
for(int i=0;i<dayOfweek;i++)
{
a[i] = "\\" ;
}
for(int i=dayOfweek,n=1 ; i<dayOfweek+day;i++)
{
a[i] = String.valueOf(n);
n++ ;
}
for(int i=dayOfweek+day;i<42;i++)
{
a[i] = "\\" ;
}
sb.append("<table border=1>");
sb.append("<tr>");
String weekday[] = {"星期日","星期一","星期二","星期三","星期四","星期五","星期六"};
String tem="";
for(int k=0;k<7;k++)
{
//中文处理需捕获异常
try
{
tem = new String(weekday[k].getBytes("ISO-8859-1"),"GBK");
}catch(Exception e){}
sb.append("<td>"+tem+"</td>");
}
sb.append("</tr>");
for(int k=0;k<42;k=k+7)
{
sb.append("<tr>");
for(int j=k;j<Math.min(7+k,42);j++)
{
sb.append("<td>"+a[j]+"</td>");
}
sb.append("</tr>");
}
sb.append("</table>");
cale = new String(sb);
if(year!=-1)
{
return cale;
}
else
{
return "<b>select year and month ! ";
}
}// end getCale()
}//end class下面是JSP页面源码
<%@ page contentType="text/html;charset=gb2312" pageEncoding="gb2312"%>
<%@ page import="MyBeans.CalendarBean"%>
<html>
<body bgcolor="#CCCCFF">
<jsp:useBean id="c" class="MyBeans.CalendarBean" scope="page"/>
<form name="form" action="" method="post">
选择年份:
<select name="year">
<option value="1980">1980月
<option value="1981">1981月
<option value="1982">1982月
<option value="1982">1983月
<option value="1982">1984月
<option value="1982">1985月
<option value="1982">1986月
</select>
选择月份
<select name="month">
<option value="1">1月
<option value="2">2月
<option value="3">3月
<option value="4">4月
<option value="5">5月
<option value="6">6月
<option value="7">7月
</select>
<input type="submit" value="提交">
</form>
<jsp:setProperty name="c" property="*"/>
<jsp:getProperty name="c" property="cale"/>
</body>
</html>
for(int k=0;k<7;k++)
{
//中文处理需捕获异常
try
{
tem = new String(weekday[k].getBytes("ISO-8859-1"),"GBK");
}catch(Exception e){}
sb.append("<td>"+tem+"</td>");
}
改成:
for(int k=0;k<7;k++)
{
sb.append("<td>"+weekday[k]+"</td>");
}
就可以了