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

Tomcat初学时遇到的问题

程序员文章站 2022-04-27 22:29:55
...

一、运行Tomcat时IDEA控制台乱码

淇℃伅 [main] org.apache.catalina.startup.VersionLoggerListener.log Server.鏈嶅姟鍣ㄧ増鏈�: Apache Tomcat/8.5.66
  • 解决方式:找到Tomcat安装目录的conf目录,将所有UTF-8改为GBK
    效果:
1catalina.org.apache.juli.AsyncFileHandler.level = FINE
1catalina.org.apache.juli.AsyncFileHandler.directory = ${catalina.base}/logs
1catalina.org.apache.juli.AsyncFileHandler.prefix = catalina.
1catalina.org.apache.juli.AsyncFileHandler.encoding = GBK

2localhost.org.apache.juli.AsyncFileHandler.level = FINE
2localhost.org.apache.juli.AsyncFileHandler.directory = ${catalina.base}/logs
2localhost.org.apache.juli.AsyncFileHandler.prefix = localhost.
2localhost.org.apache.juli.AsyncFileHandler.encoding = GBK

3manager.org.apache.juli.AsyncFileHandler.level = FINE
3manager.org.apache.juli.AsyncFileHandler.directory = ${catalina.base}/logs
3manager.org.apache.juli.AsyncFileHandler.prefix = manager.
3manager.org.apache.juli.AsyncFileHandler.encoding = GBK

4host-manager.org.apache.juli.AsyncFileHandler.level = FINE
4host-manager.org.apache.juli.AsyncFileHandler.directory = ${catalina.base}/logs
4host-manager.org.apache.juli.AsyncFileHandler.prefix = host-manager.
4host-manager.org.apache.juli.AsyncFileHandler.encoding = GBK

java.util.logging.ConsoleHandler.level = FINE
java.util.logging.ConsoleHandler.formatter = org.apache.juli.OneLineFormatter
java.util.logging.ConsoleHandler.encoding = GBK

二、未知版本字符串 [4.0]警告

警告 [RMI TCP Connection(5)-127.0.0.1] org.apache.tomcat.util.descriptor.web.WebXml.setVersion 未知版本字符串 [4.0]。将使用默认版本。

  • 原因:使用的Tomcat8版本,创建项目时勾选了web application版本为4.0
  • 解约方案:
  1. 手动修改web.xml文件
<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_3_1.xsd"
         version="3.1">

注意xsi:schemaLocation一行最后web-app_4_0.xsd别忘了改
2. 提升Tomcat至9版本

三、相对路径失效

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String studentName = request.getParameter("StudentName");
        String studentAge = request.getParameter("StudentAge");
        String studentScore = request.getParameter("StudentScore");
        Student student = new Student(studentName,Integer.parseInt(studentAge),Integer.parseInt(studentScore));
        BufferedWriter bw = new BufferedWriter(new FileWriter("students.txt",true));

此时的生成的students.txt文件会跑到Tomcat文件夹的bin目录下
在JavaWeb工程中不用相对路径都用绝对路径

ServletContext context = getServletContext();
        String realPath = context.getRealPath("/");
        BufferedWriter bw = new BufferedWriter(new FileWriter(realPath+"\\students.txt",true));

生成的students.txt文件会保存在项目目录下如:
Servlet-Demo01\out\artifacts\Servlet_Demo01_war_exploded\