<%@ page language="java" contentType="text/html; charset=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>Insert title here</title>
</head>
<body>
<form name="form1" action="register" method="post">
用户名<input type="text" name="uname">
密码<input type="password" name="pwd">
城市:<select name="city">
<optgroup label="城市">
<option value="北京">北京</option>
<option value="2">上海</option>
<option value="3">深圳</option>
<option value="4">广州</option>
<option value="5">西安</option>
</optgroup>
<input type="submit" value="提交">
</form>
</body>
<%
request.setCharacterEncoding ("utf-8");
response.setCharacterEncoding("utf-8");//放到最上面
if (request.getParameter("uname")==null) {
out.println("您没有输入姓名");
}else{
out.println(request.getCharacterEncoding()+"encoding");
response.setHeader("contentType", "text/html; charset=UTF-8");
out.println("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">");
//out.println("您输入的姓名为:"+codeToString(request.getParameter("uname")));
out.println("您输入的姓名为:"+request.getParameter("uname"));
}
%>
<%--
<% %> 和<%! %>是有区别的,在jsp中,<% %>用于写java代码,<%! %>用于声明方法和变量,你上面的那两个都是方法,需要用<%! %>
p页面会翻译成.java文件,<%! %>用于声明方法和变量,会翻译到public void _jspService方法的外面,供全局使用,
<% %>里面包含的所有内容都会翻译public void _jspService这个方法中。
如果在jsp页面中写方法,不定义为全局方法的话,就会出现方法中写入方法,当然会报错啦!
--%>
<%!
public String codeToString(String str) {//中文编码转换
String s=str;
try{
byte tempB[]=s.getBytes("utf-8");
s=new String (tempB,"utf-8");
return s;
}catch (Exception e){
return s;
}
}
%>
</html>
jsp 解决乱码问题
最新推荐文章于 2022-10-06 17:04:13 发布