<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %>
<!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=gb2312">
<title>聊天室</title>
</head><body>
一个简单的聊天室
<br><hr><font color=red>
<%
response.setContentType("text/html;charset=GBK");
request.setCharacterEncoding("GBK");
String content=(String)getServletContext().getAttribute(new String("chatTopic_1"));
out.println(content);
getServletContext().setAttribute("chatTopic_1",content+(String)request.getParameter("content")+"<br>");
%>
</font>
<hr>
<form action="ServletContext_chast.jsp">
<input type=text name=content>
<input type=submit value="说话呀">
</form>
</body>
</html>
输出部分内容如下:
一个简单的聊天室 --------------------------------------------------------------------------------
nullnull
??????°?°?
pi
???è??
???è??
????
????????
null
×???
??
heoo
fsla
fdsfa
fdsfa
°é×???
??
??
<!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=gb2312">
<title>聊天室</title>
</head><body>
一个简单的聊天室
<br><hr><font color=red>
<%
response.setContentType("text/html;charset=GBK");
request.setCharacterEncoding("GBK");
String content=(String)getServletContext().getAttribute(new String("chatTopic_1"));
out.println(content);
getServletContext().setAttribute("chatTopic_1",content+(String)request.getParameter("content")+"<br>");
%>
</font>
<hr>
<form action="ServletContext_chast.jsp">
<input type=text name=content>
<input type=submit value="说话呀">
</form>
</body>
</html>
输出部分内容如下:
一个简单的聊天室 --------------------------------------------------------------------------------
nullnull
??????°?°?
pi
???è??
???è??
????
????????
null
×???
??
heoo
fsla
fdsfa
fdsfa
°é×???
??
??
解决方案 »
- jsp中图片保存为文件
- 在oracle中jsp对clob字段数据进行插入问题,在线等急用
- 我的一张表格,显示数据库的记录,我想点击哪个标题栏,就按照哪个标题排序,行吗?
- 最进在看孙鑫的Java_Web开发详解,有一些问题请教
- Ext.PagingToolbar不在页面最底端
- 文件上传报错java.io.IOException: Corrupt form data: premature ending
- 关于 控件 CLONE
- 帮忙看看这个小程序(99表)为什么不会换行
- 请问各位兼职JSP一般都做什么呀?会不会很难呀?偶以前没有做过,现在要做,心里先有个底。
- 我为WebSphere写的代码怎么会出错
- Tomcat中使用ICTCLAS的问题
- aaa
在你的web.xml里加入如下几行: <filter>
<filter-name>Set Character Encoding</filter-name>
<filter-class>filters.SetCharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>GBK</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>Set Character Encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
打开tomcat的server.xml文件,找到区块,加入如下一行:
URIEncoding=”GBK”
完整的应如下:
<Connector
port="80" maxThreads="150" minSpareThreads="25" maxSpareThreads="75"
enableLookups="false" redirectPort="8443" acceptCount="100"
debug="0" connectionTimeout="20000"
disableUploadTimeout="true"
URIEncoding="GBK"
/>
重启tomcat,一切OK。
%TOMCAT安装目录%/ webapps\servlets-examples\WEB-INF\classes\filters\SetCharacterEncodingFilter.class文件
我用的是tomcat5.5,我找遍了整个文件夹,根本就找不到你说的那个些路径呀,
有没有tomcat5.5的解决方法呢
out.println(content);把上面这两行代码改成下面的试一下:String content=(String)getServletContext().getAttribute(new String("chatTopic_1"));
byte temp[] =N_Body.getBytes("ISO-8859-1");
content=new String(temp);
out.println(content);
String content=request.getParameter("Username").toString();
byte temp[] =content.getBytes("ISO-8859-1");
content=new String(temp);
out.print(content);
String content=request.getParameter("Username").toString();
应该改成
String content=request.getParameter("content").toString();
是吧?其次,改之前跟改之后都不行,错误如下HTTP Status 500 - --------------------------------------------------------------------------------type Exception reportmessage description The server encountered an internal error () that prevented it from fulfilling this request.exception org.apache.jasper.JasperException: Exception in JSP: /testApplication.jsp:1512: <%
13: response.setContentType("text/html;charset=GBK");
14: request.setCharacterEncoding("GBK");
15: String content=request.getParameter("content").toString();
16: byte temp[] =content.getBytes("ISO-8859-1");
17: content=new String(temp);
18: out.print(content);
Stacktrace:
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:504)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:393)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
root cause java.lang.NullPointerException
org.apache.jsp.testApplication_jsp._jspService(org.apache.jsp.testApplication_jsp:57)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:97)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:332)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
note The full stack trace of the root cause is available in the Apache Tomcat/5.5.15 logs.
--------------------------------------------------------------------------------Apache Tomcat/5.5.15
1。
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %>
2。
response.setContentType("text/html;charset=GBK");
request.setCharacterEncoding("GBK");你可以统一一下编码方式,同时你可以加一个编码过滤器。
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;public class EncodingFilter extends HttpServlet implements Filter {
private static String ENCODING = "GBK";
private FilterConfig filterConfig;
//Handle the passed-in FilterConfig
public void init(FilterConfig filterConfig) throws ServletException {
this.filterConfig = filterConfig;
} //Process the request/response pair
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain filterChain) {
try {
request.setCharacterEncoding(ENCODING);
filterChain.doFilter(request, response);
response.setCharacterEncoding(ENCODING);
} catch (ServletException sx) {
filterConfig.getServletContext().log(sx.getMessage());
} catch (IOException iox) {
filterConfig.getServletContext().log(iox.getMessage());
}
} //Clean up resources
public void destroy() {
}
}web.xml的配置
<filter>
<filter-name>encodingfilter</filter-name>
<filter-class>cn.com.news.filter.EncodingFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>encodingfilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>