html编程tips

来源:互联网 发布:亚马逊和淘宝哪个大 编辑:程序博客网 时间:2022/08/16 18:04

【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 codereq.setCharacterEncoding("UTF-8");  //avoid err codeString 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
原创粉丝点击