Servlet 与 tomcat 避坑指南
2023-04-18 14:08:51 时间
Servlet 与 tomcat 避坑指南
- 无法打开登录管理页面。
修改 conf/tomcat-users.xml,在文件中加入以下两行。
<role rolename="manager-gui,admin-gui"/>
<user username="ivandu" password="drh123" roles="manager-gui,admin-gui"/>
- 远程主机无法登录管理页面。
修改应用下面的 META-INF/context.xml 把<Valve className="org.apache.catalina.valves.RemoteAddrValve" allow="127.d+.d+.d+|::1|0:0:0:0:0:0:0:1" />
中 allow 后面的内容,例如改为 10 网段可以访问:
<Context antiResourceLocking="false" privileged="true" >
<CookieProcessor className="org.apache.tomcat.util.http.Rfc6265CookieProcessor"
sameSiteCookies="strict" />
<Valve className="org.apache.catalina.valves.RemoteAddrValve"
allow="10.d+.d+.d+|::1|0:0:0:0:0:0:0:1" />
<Manager sessionAttributeValueClassNameFilter="java.lang.(?:Boolean|Integer|Long|Number|String)|org.apache.catalina.filters.CsrfPreventionFilter$LruCache(?:$1)?|java.util.(?:Linked)?HashMap" />
</Context>
新增某指定 IP 的主机能访问,例如新增 IP 为 10.1.1.2 的主机访问 manager 和 host-manager 两个应用,在 webapps/manager/META-INF/context.xml 和 webapps/host-manager/META-INF/context.xml 文件中做如下修改:
<Context antiResourceLocking="false" privileged="true" >
<CookieProcessor className="org.apache.tomcat.util.http.Rfc6265CookieProcessor"
sameSiteCookies="strict" />
<Valve className="org.apache.catalina.valves.RemoteAddrValve"
allow="10.1.1.2|127.d+.d+.d+|::1|0:0:0:0:0:0:0:1" />
<Manager sessionAttributeValueClassNameFilter="java.lang.(?:Boolean|Integer|Long|Number|String)|org.apache.catalina.filters.CsrfPreventionFilter$LruCache(?:$1)?|java.util.(?:Linked)?HashMap"/>
</Context>
- tomcat 10 编译、部署时提示"程序包javax.servlet不存在"。
tomcat 10 中 servlet 相关的包名已经由 javax 变更为 jakarta,该版本的tomcat支持 java ee9 的部分实现,servlet 升级到 5.0。目前 maven 仓库还没有 servlet 5.0 相关的仓库。 版本支持情况。
- Servlet 项目中的 web.xml 无需死记硬背,可以从 tomcat 的 conf 目录下拷贝。例如 tomcat9.0.46:
<?xml version="1.0" encoding="UTF-8"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<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">
</xml-app>
- request.getRemoteAddr()获取的IP一般默认为IPv6,要转换为IPv4需要配置Tomcat的vm选项:
-Djava.net.preferIPv4Stack=true
- 服务器输出 UTF-8 日志显示乱码的解决方法,在 VM 选项增加:
-Dfile.encoding=UTF-8
相关文章
- Jease 2.6发布 Java开源内容框架
- JVM调优总结:反思
- JVM调优总结:调优方法
- JVM调优总结:新一代的垃圾回收算法
- JVM调优总结:典型配置举例
- JVM调优总结:分代垃圾回收详述
- JVM调优总结:垃圾回收面临的问题
- JVM调优总结:基本垃圾回收算法
- JVM调优总结:一些概念
- 用Java GUI编写的画板程序
- Java的动态绑定机制
- jOOQ 2.0.2发布 Java的ORM框架
- Java中带复选框的树的实现和应用
- Java网络编程菜鸟进阶:TCP和套接字入门
- 甲骨文与谷歌专利权之争定于今年三月开审
- Java调用C/C++编写的第三方dll动态链接库
- 集成开发环境 NetBeans IDE 7.1正式版发布
- kangle 2.7.5紧急发布 防hash碰撞攻击
- 东方通技术引领模式为国产软件“争权”
- UML中关联,组合与聚合等关系的辨析