protected void service(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
//因为tomcat自带编码是ISO-8859-1格式
//解决乱码方法之一
<span style="white-space:pre"> </span>String name=req.getParameter("username");
<span style="white-space:pre"> </span>String pwd=req.getParameter("pwd");
<span style="white-space:pre"> </span>byte[] b=name.getBytes("ISO-8859-1");//用tomcat的格式(iso-8859-1)方式去读。
<span style="white-space:pre"> </span>String str=new String(b,"utf-8");//采用utf-8去接string
<span style="white-space:pre"> </span>resp.setContentType("text/html;charset=utf-8");//设置页面的字符编码<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>PrintWriter pw =resp.getWriter(); <span style="white-space:pre"> </span>String str1="<html><body><font size='5px' color='red'>username:"+name+"pwd:"+pwd+"</font></body></html>"; <span style="white-space:pre"> </span>pw.print(str1); PrintWriter pw =resp.getWriter(); String str1="<html><body><font size='5px' color='red'>username:"+name+"pwd:"+pwd+"</font></body></html>"; pw.print(str1);
protected void service(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
//因为tomcat自带编码是ISO-8859-1格式
//解决乱码二《法一比较繁琐》
req.setCharacterEncoding("utf-8");//必须写在第一位,因为采用这种方式去读取数据,否
则数据会出错。
//设置这样方式去读。这样中文就能够读取出来了,但是需要注意。表单的发送方式必须是<span style="color:#ff0000;"> method='post'</span>
resp.setContentType("text/html;charset=utf-8");//设置传过去的页面显示的编码
String name=req.getParameter("username");
String pwd=req.getParameter("pwd");
PrintWriter pw =resp.getWriter();
String str1="<html><body><font size='5px' color='red'>username:"+name+"pwd:"+pwd+"</font></body></html>";
pw.print(str1);
<servlet> <servlet-name>Encodeing</servlet-name> <servlet-class>cn.hncu.com.encode.Encodeing</servlet-class> <init-param> <param-name>charset</param-name> <param-value>utf-8</param-value>//这里面的内容可供用户自己填写(必须是编码格式) </init-param> </servlet>
public void init(ServletConfig config) throws ServletException {
charset=config.getInitParameter("charset");//获得初始化参数。当然charset需要设置为全局变量。后面的service函数需要设置req.setCharacterEncoding(charset);
}
req.setCharacterEncoding(charset);
resp.setContentType("text/html;charset=utf-8");
String name=req.getParameter("username");
String pwd=req.getParameter("pwd");
PrintWriter pw =resp.getWriter();
String str1="<html><body><font size='5px' color='red'>username:"+name+"pwd:"+pwd+"</font></body></html>";
pw.print(str1);
<body> <form action="login" method="post">//login在web.xml中配置,就能实现到后台去读取数据 用户名:<input type="text" name="username"/><br/> 密码:<input type="password" name="pwd"/><br/> <input type="submit" value="登录"/> </form> </body>
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有