1.新建项目
点击create
2.添加web项目框架
3.选中web框架
点击ok
4.添加tomcat
5.选择tomcat
点击应用点击ok
6.配置tomcat路径以及相关设置
点击应用点击ok
7.将web项目的war包部署到tomcat上
8.确保处理请求一致
点击应用点击ok
9.添加项目依赖
点击应用点击ok
10.检查依赖是否添加到war包中
这样一个基本的一个web项目配置完成
设置项目基本文件层级结构
配置文件的文件夹标记设置
文字编码过滤器设置
<?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">
<filter>
<filter-name>EncodingFilter</filter-name>
<filter-class>com.kgc.filter.EncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>htmlShow</param-name>
<param-value>text/html;charset=utf-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>EncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
package com.qy.filter;
import javax.servlet.*;
import java.io.IOException;
/**
* @author Magic
* @version 1.0
*/
public class EncodingFilter implements Filter {
// 获取web.xml文件中的配置utf-8
private String encoding;
private String htmlShow;
@Override
public void init(FilterConfig filterConfig) throws ServletException {
// 从web.xml的过滤器的init-param中,对应的值,在赋值给全局变量
encoding = filterConfig.getInitParameter("encoding");
htmlShow =filterConfig.getInitParameter("htmlShow");
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
// 设置编码;
servletRequest.setCharacterEncoding(encoding);
servletResponse.setCharacterEncoding(encoding);
servletResponse.setContentType(htmlShow);
// 放行
filterChain.doFilter(servletRequest, servletResponse);
}
@Override
public void destroy() {
}
}