servlet 路径 编码 问题

一 路径 编码 问题

package com.itstaredu.servlet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 使用绝对路径
 * /项目名/......
 * 绝对路径以/开头
 * 转发时候 dispatcher 不需要 项目名 因为在同一个项目下
 * 服务器解析 会带有项目名 不要加
 * 浏览器解析没有项目名 我们自己加
 * base标签 <base href="http://localhost:8080/javaweb07/"/> 后面加相对路径
 * 绝对路径以/开头
 * 相对路径 没有/开头
 * 编码问题 ISO-8859-1
 * 服务器 重新解码 再编码
 * request 在server.xml 65行 connector标签 添加 URIEncoding="UTF-8" 或无
 * <p>
 * request.setCharacterEncoding("UTF-8");
 * response.setContentType("text/html;charset=UTF-8");
 * response.setHeader("content-Type","text/html;charset=UTF-8");
 */
public class PathServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");

        response.getWriter().write("三国志");
        String name = request.getParameter("username0");
        System.out.println(name);
    }
}

  

原文地址:https://www.cnblogs.com/liubosong/p/11989352.html