解决中文乱码
web应用的中文乱码
- Tomcat默认使用字符集ISO-8859-1,属于西欧字符集
- 解决乱码的核心思路是将ISO-8859-1转换为UTF-8
- Servlet中请求与响应都需要设置UTF-8字符集
解决POST请求中文乱码
POST请求具有请求体,在请求中可以设置:
request.setCharacterEncoding("utf-8");
该方法可以将请求体中的字符集转换为UTF-8
对于响应设置中文的方法:
response.setContentType("text/html;charset=utf-8");
示例程序如下:
charset_form.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="/servlet_advanced/charset" method="post">
姓名: <input type="text" name="username">
地址: <input type="text" name="address">
<input type="submit" value="提交">
</form>
</body>
</html>
CharsetServlet
@WebServlet("/charset")
public class CharsetServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 将请求体中的字符集转换为UTF-8;POST请求具有请求体,GET方法则没有
request.setCharacterEncoding("utf-8");
String username = request.getParameter("username");
String address = request.getParameter("address");
response.setContentType("text/html;charset=utf-8");
response.getWriter().println(username + ":" + address);
}
}
页面显示结果:
解决GET请求与响应中文乱码
示例程序:
charset_form.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="/servlet_advanced/charset" method="get">
姓名: <input type="text" name="username">
地址: <input type="text" name="address">
<input type="submit" value="提交">
</form>
</body>
</html>
CharsetServlet
@WebServlet("/charset")
public class CharsetServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 将请求体中的字符集转换为UTF-8;POST请求具有请求体,GET方法则没有
request.setCharacterEncoding("utf-8");
doGet(request,response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 对于Tomcat8.x以后版本来说,默认GET请求发送中文就是UTF-8的编码格式,无需转换
String username = request.getParameter("username");
String address = request.getParameter("address");
response.setContentType("text/html;charset=utf-8");
response.getWriter().println(username + ":" + address);
}
}
页面显示结果:
Web.xml配置详解
web.xml常用配置
- 修改web应用默认首页
- Servlet通配符映射及初始化参数
- 设置404,500等状态码默认页面
web.xml常用配置
url-pattern模糊匹配
示例,通过URL打印出所有员工的信息:
在地址栏中末尾输入员工的编号id,通过输入的URL不同,页面显示出不同的员工信息
web.xml配置如下:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<servlet>
<servlet-name>pattern</servlet-name>
<servlet-class>pattern.PatternServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>pattern</servlet-name>
<url-pattern>/pattern/*</url-pattern>
</servlet-mapping>
</web-app>
在web.xml配置中,url-pattern映射的地址利用*
实现了模糊匹配,只要是URL为:http://localhost:8080/servlet_advanced/pattern/XXX
都可以跳转到PatternServlet进行处理
PatternServlet
public class PatternServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 通过url打印出员工的信息
// 1:张三 2:李四 3:王五
String url = req.getRequestURL().toString();
String empId = url.substring(url.lastIndexOf("/") + 1);
resp.setContentType("text/html;charset=utf-8");
PrintWriter writer = resp.getWriter();
writer.println("<h1>"+empId+"</h1>");
if(empId.equals("1")){
writer.println("<h1>张三</h1>");
}else if(empId.equals("2")){
writer.println("<h1>李四</h1>");
}else if(empId.equals("3")){
writer.println("<h1>王五</h1>");
}else{
writer.println("<h1>查无此人</h1>");
}
}
}
页面显示如下:
除了使用web.xml的url-pattern进行模糊的地址匹配,还可以使用更简洁的注解的方式:
在PatternServlet类上添加注解:
@WebServlet("/pattern/*")
即可。
context-param的设置
可以通过web.xml文件来设置ServletContext的初始化参数
示例程序如下:
web.xml配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<context-param>
<param-name>title1</param-name>
<param-value>This is title1</param-value>
</context-param>
<context-param>
<param-name>title2</param-name>
<param-value>This is title2</param-value>
</context-param>
</web-app>
通过ServletContext的对象的getInitParameter方法来获取web.xml文件中配置的参数
ServletContextInit
@WebServlet("/servlet_context/init")
public class ServletContextInit extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ServletContext context = request.getServletContext();
String title1 = context.getInitParameter("title1");
String title2 = context.getInitParameter("title2");
context.setAttribute("title1",title1);
context.setAttribute("title2",title2);
response.getWriter().println("init success");
}
}
获取ServletContext设置的属性值
ServletContextDefault
@WebServlet("/servlet_context/default")
public class ServletContextDefault extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ServletContext servletContext = request.getServletContext();
String title1 = (String) servletContext.getAttribute("title1");
String title2 = (String)servletContext.getAttribute("title2");
response.setContentType("text/html;charset=utf-8");
response.getWriter().println("<h1>" + title1 + "</h1>");
response.getWriter().println("<h1>" + title2 + "</h1>");
}
}
页面显示结果如下:
error-page指定错误页面
示例如下:
404.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>找不到资源</h1>
</body>
</html>
web.xml
<error-page>
<error-code>404</error-code>
<location>/error/404.html</location>
</error-page>
当输入非法的URL时,页面会跳转到404.html上
如:
JSP内置对象
JSP九大内置对象
web应用程序打包与发布
- Java Web应用采用war包进行发布
- 发布路径为{TOMCAT_HOME}/webapps