在Tomcat6.0中关于JSP/Servlet表单乱码的一个解决方法
步骤一:编辑Tomcat的配置文件conf/server.xml在用于接受客户端语法的Connector<connector></connector>标签中添加URIEncoding="UTF-8"属性,该属性用来解决GET中的编码问题。
xml 代码
<Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" URIEncoding="UTF-8" />
步骤二:在每个需要提交表单参数的JSP/Servlet之前加入下列代码来设置字符集,用于搞定POST请求:
java 代码
request.setCharacterEncoding("UTF-8");
这样基本就搞定了字符乱码问题了,实现上述问题的要求是所有的网页编码必须是UTF-8编码既。
在JSP中:
jsp 代码
<%@page contentType="text/html" pageEncoding="UTF-8"%>
在Servlet中:
java 代码
response.setContentType("text/html;charset=UTF-8");
在所有的网页中:
html代码
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
以上测试在Tomcat 6.0.14、IE6.0、FireFox2.0.13及Opera9.25中测试通过,开发环境使用netBeans 6.0
附测试代码:
html代码
Document : zc
Created on : 2007-12-22, 17:20:24
Author : 啊春
-->
>
<html>
<head>
<title>title>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
head>
<body>
<form action="/wat1/t1" method="GET">
姓名:<input type="text" name="name" value="" size="20" />
密码:<input type="password" name="passwd" value="" size="20" />
<input type="submit" value="注册" />
form>
<br />
<form action="/wat1/t1" method="POST">
姓名:<input type="text" name="name" value="" size="20" />
密码:<input type="password" name="passwd" value="" size="20" />
<input type="submit" value="注册" />
form>
body>
html>
java 代码
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
/* TODO output your page here
out.println("");
out.println("");
相关文档:
一 、 用struts2标签的<s:select来显示
<s:select cssStyle="width: 84%" list="#application.officeList" headerKey="" headerValue="--请选择--" listKey="#this.value" listValue="#this.key+'.'+#this.value" name="entity.officeName" value="entity.officeName"/>
其中
1、list是接收java类或者在启动时 ......
在MyEclipse中开发WEB应用时经常要创建JSP文件,但是每次MyEclipse都会用默认的MyEclipse Visual JSP Designer,打开速度非常慢,而且基本上也用不到这个可视化的JSP编辑器,多半都是使用MyEclipse JSP Editor,所以,有必要把默认的JSP文件编辑器改成MyEclipse JSP Editor,方便使用,也可 ......
一、清除页面缓存
在jsp页里
<%response.setHeader("Pragma","No-cache");
response.setHeader("Cache-Control","no-cache");
response.setDateHeader("Expires", 0);
response.flushBuffer();%>
在html页里
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<META HTTP-EQUI ......
第1个上传组件commons-fileupload
=============commons-fileupload ================
common-fileupload组件是apache的一个开源项目之一,可以从http://jakarta.apache.org/commons/fileupload/下载。该组件简单易用,可实现一次上传一个或多个文件,并可限制文件大小。
-下载后解压zip包,将commons-fileupload-1.1. ......
长度限制JavaScript代码
CODE:
<script> function test() { if(document.a.b.value.length>50) { alert("不能超过50个字符!"); document.a.b.focus(); return
false; } } </script> <form. name=a nsubmit="return test()"> <textarea name="b" cols="40" wrap="VIRTUAL" rows="6"></te ......