当前位置 : 主页 > 网页制作 > html >

<input type="text" >发送请求参数,中文乱码问题

来源:互联网 收集:自由互联 发布时间:2021-06-12
1、不废话,直接代码如下: teacher.jsp %@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"htmlhe

1、不废话,直接代码如下:

teacher.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>教师管理页面</title>
</head>
<body>

<form action="SetDirectory" method="post">
课程资源目录:<input type="text" name="resourceDirectory"><br>
<input type="submit" value="提交">
</form>
<br>
---------------
<form action="SetDirectory" method="post">
作业提交目录:<input type="text" name="homeworkDirectory"><br>
<input type="submit" value="提交">
</form>
</body>
</html>

SetDerictoryServlet.java

@WebServlet("/SetDirectory")
public class SetDirectoryServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public SetDirectoryServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.setContentType("text/html;charset=UTF-8");
		
		String resourceDirectory = request.getParameter("resourceDirectory");
		String homeworkDirectory = request.getParameter("homeworkDirectory");
		System.out.println(resourceDirectory);
		System.out.println(homeworkDirectory);
	}

  

分享图片

分享图片

 

2、解决办法:21行加入

		if (resourceDirectory!=null) {
			resourceDirectory = new String(resourceDirectory.getBytes("ISO8859-1"), "UTF-8");
		}
		if (homeworkDirectory!=null) {
			homeworkDirectory = new String(homeworkDirectory.getBytes("ISO8859-1"), "UTF-8");			
		}
网友评论