html编程tips

【1】当中文出现乱码时,设置格式为utf-8:在<head>中插入<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />


【2】servlet表单中文乱码

第一种情况:servlet中输出中文字符,则需要在java中将response设为utf-8:

resp.setContentType("text/html;charset=utf-8");  //avoid err code

第二种情况:servlet接收表单传值并显示,表单字段含中文,需要在java中将request设为utf-8:

req.setCharacterEncoding("UTF-8");  //avoid err code

package liyuanzhe;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class HelloServlet extends HttpServlet{
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
		throws ServletException, IOException{
		resp.setContentType("text/html;charset=utf-8");  //avoid err code
		req.setCharacterEncoding("UTF-8");  //avoid err code
		String info = req.getParameter("info");
		PrintWriter out = resp.getWriter();
		out.println("<!DOCTYPE html>");
		out.println("<html>");
		out.println("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />");
		out.println("<head><title>welcome</title></head>");
		out.println("<body>");
		String tmp = "雷神"+info;
		out.println("<h1>"+tmp+"</h1>");
		out.println("</body>");
		out.println("</html>");
		out.close();
	}
	
	public void doPost(HttpServletRequest req, HttpServletResponse resp)
		throws ServletException, IOException{
		this.doGet(req, resp);
	}
}

【3】安卓post/get地址需要加"http",否则无法成功。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值