欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Severlet提取表单信息中文乱码

程序员文章站 2022-04-04 11:13:15
...

jsp代码:

<form action="SeverletArticle?op=updateArticle" method="post">
						<table border="" cellspacing="" cellpadding=""
							class="table table-striped table-hover ">
							<tr>
								<th>文章编号</th>
								<th>文章标题</th>
								<th>文章内容</th>
								<th>发表日期</th>
								<th>作者编号</th>
							</tr>
							<tr class="gradeD">
								<td><input type="text" id="id" name="id" readonly="readonly" class="form-control" /></td>
								<td><input type="text" id="title" name="title" class="form-control"/></td>
								<td><input type="text" id="articleContent"
									name="articleContent" class="form-control"/></td>
								<td class="center"><input type="text" id="date" name="date" class="form-control"/></td>
								<td class="center"><input type="text" id="authorId"
									name="authorId" class="form-control"/></td>
							</tr>
						</table>
                        <input type="submit" class="btn btn-secondary"  value="修改"></input>
					</form>

Severlet提取表单信息中文乱码

解决办法:在service层加这行代码,如果没有service层就在第一次出现request变量的方法里面加

request.setCharacterEncoding("utf-8");

示例:

	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("utf-8");
		if (request.getParameter("op")==null) {
			doGet(request, response);
		}else if ("deleteArticle".equals(request.getParameter("op"))) {
			doDelete(request, response);
		}else if ("updateArticle".equals(request.getParameter("op"))) {
			doUpdate(request, response);
		}
		
	}

Severlet提取表单信息中文乱码