jsp中文乱码问题
解决办法:
第一:
1:在jsp页面加入:
<%@ page contentType="text/html; charset=gb2312" %>
2:在servlet里面:
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html; charset=gb2312");//这是重要的
3:上面的如果在不行就用如下的方法在数据入库前进行调用:
public static String UnicodeToChinese(String s){
try{
if(s==null ¦ ¦s.equals("")) return "";
String newstring=null;
newstring=new String(s.getBytes("ISO8859_1"),"gb2312");
return newstring;
}
catch(UnsupportedEncodingException e)
{
return s;
}
}
public static String ChineseToUnicode(String s){
try{
if(s==null ¦ ¦s.equals("")) return "";
String newstring=null;
newstring=new String(s.getBytes("gb2312"),"ISO8859_1");
return newstring;
}
catch(UnsupportedEncodingException e)
{
return s;
}
}
相关文档:
JSP和Servlet中的绝对路径和相对路径问题困扰了我好几天,经过努力之后将其部分心得和大家共享。
前提:假设你的Http地址为http://192.168.0.1/你的web应用为webapp,那么你的web应用URL为http://192.168.0.1/webapp/
web应用的目录结构:
webapp/
web-inf/
......
//JSP禁止缓存代码
response.setHeader("Pragma","No-cache");
response.setHeader("Cache-Control","no-cache");
response.setDateHeader("Expires", 0);
//网络上的不能正确运行代码参考
if(request.getProtocol().compareTo("HTTP/1.0")==0)
{
......
在Tomcat下JSP、Servlet和JavaBean环境的配置
经常看到jsp的初学者问tomcat下如何配置jsp、servlet和bean的问题,于是总结了一下如何tomcat下配置jsp、servlet和ben,希望对那些初学者有所帮助。
第一步:下载j2sdk和tomcat:到sun官方站点(http://java.sun.com/j2se/1.4.2 /download.html)下载j2sdk,注意下载版 ......
简单示例
JSP页面中,我们经常有引用各种图片等资源,例如下面的jsp片段中
<div class="main">
<a href="http://www.sina.com.cn/abc.htm">
<strong>我的档案</strong>
&nbs ......
产生验证码图片的文件-----image.jsp
<%@ page contentType="image/jpeg" import="java.awt.*,java.awt.image.*,java.util.*,javax.imageio.*" %>
<%!
Color getRandColor(int fc,int bc){//给定范围获得随机颜色
Random random = new Random();
&nb ......