文章目录
1、解决Post请求中文乱码问题
解决方案:
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// (1)request.setCharacterEncoding方法用于将请求体中的字符集转化为UTF-8
request.setCharacterEncoding("UTF-8");
String ename = request.getParameter("ename");
String address = request.getParameter("address");
//(2)也可以使用String进行编码方式转化,但比较麻烦
//String utf8Ename = new String(ename.getBytes("iso-8859-1"), "utf-8");
//String utf8Address = new String(address.getBytes("iso-8859-1"), "utf-8");
System.out.println(ename + ":" + address);
}
2、解决Get请求与响应中文乱码
解决方案:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//对于Tomcat8.x的版本,默认get请求发送中文就是UTF-8的格式,因此无须转化
String ename = request.getParameter("ename");
String address = request.getParameter("address");
System.out.println(ename + ":" + address);
//响应乱码解决方案:
response.setContentType("text/html;charset=utf-8");
response.getWriter().println(ename + ":" + address);
}
Tomcat8以下版本设置方式:
3、web.xml常用配置
3.1 修改web应用默认首页
默认首页名称,越往上优先级越高。
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>servlet_advanced</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
3.2 Servlet 通配符映射及初始化参数
示例代码:
(1)web.xml文件设置映射地址:
<servlet>
<servlet-name>patternServlet</servlet-name>
<servlet-class>com.imooc.servlet.pattern.PatternServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>patternServlet</servlet-name>
<url-pattern>/pattern/*</url-pattern>
</servlet-mapping>
(2)编写servlet:
public class PatternServlet extends HttpServlet{
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//查询员工的基本信息
//获取当前访问的URL
String url = request.getRequestURL().toString();
System.out.println(url);
String id = url.substring(url.lastIndexOf("/") + 1);
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
out.println(id);
if(id.equals("1")) {
out.println("张三");
}else if(id.equals("2")) {
out.println("李四");
}else {
out.println("其他");
}
}
}
实验结果:
3.3 应用程序全局参数设置
在web.xml中通过context-param标签来设置应用程序全局参数
<context-param>
<param-name>name</param-name>
<param-value>xiaoming</param-value>
</context-param>
<context-param>
<param-name>age</param-name>
<param-value>21</param-value>
</context-param>
调用方式:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ServletContext context = request.getServletContext();
String name = context.getInitParameter("name");
context.setAttribute("name", name);
String age = context.getInitParameter("age");
context.setAttribute("age", age);
response.getWriter().println("init success");
}
3.4 设置404、500状态码默认页面
(1)编写错误页面信息:
(2)在web.xml中指定错误页面
<!-- 指定错误页面 -->
<error-page>
<error-code>404</error-code>
<location>/error/404.html</location>
</error-page>
<error-page>
<error-code>500</error-code>
<location>/error/500.html</location>
</error-page>
</web-app>
(3)结果展现:
4、JSP九大内置对象
代码示例:
(1)built_in_object.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
String url = request.getRequestURL().toString(); //HttpServletRequest
response.getWriter().println(url); //HttpServletResponse
%>
<%
out.println("<br>ABCCC");
session.setAttribute("user","张三");
out.println((String)session.getAttribute("user"));
%>
<%
String cp = application.getInitParameter("name");//ServletContext
out.println("<hr/>");
out.println(cp);
pageContext.getRequest(); //pageContext像一个中转站,可以快速获取其他配置项
pageContext.getResponse();
pageContext.getSession();
pageContext.getServletContext();
%>
</body>
</html>
运行结果:
(2)将错误页面改为动态页面(500.jsp)
<%@ page contentType="text/html;charset=utf-8" isErrorPage="true" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
服务器内部问题,请联系管理员,错误信息如下:
<%
String msg = exception.getMessage();
out.print("<br>" + exception.getClass().getSimpleName() + ":" + msg);
%>
</body>
</html>
结果显示: