文章目录
- 问题一:Tomcat的startup.bat启动后出现乱码
- 问题二:一闪而退之端口占用
- 问题三:非端口问题的一闪而退
- 问题四:服务器的乱码和跨域问题
- 问题五: 在tomcat\webapps\下创建文件夹为什么tomcat重启就会丢失
- 问题六:Tomcat启动报java.io.EOFException错误
- 问题七:解决跨域问题
问题一:Tomcat的startup.bat启动后出现乱码
找到Tomcat文件下的conf目录,修改logging.properties文件中
java.util.logging.ConsoleHandler.encoding对应的值为GBK
重启Tomcat,你看到的启动页面是这样的话,说明乱码问题已经解决
问题二:一闪而退之端口占用
启动Tomcat出现一闪而退的现象,其实还是可以看到Tomcat终端输出的日志,
这时你需要迅速截图捕获异常,太南了。
最正确的方法是查看日志,找到Tomcat目录下的logs目录中catalina当天的日志,
我这里找到的是C:\develop\Tomcat\apache-tomcat-8.5.47\logs\catalina.2019-10-17.log,
在日志中可以看到类似这样的输出,Address already in use: bind说明是端口占用了
在Tomcat目录找到conf目录中的server.xml配置文件,在Connector标签中修改Tomcat启动端口
<Connector port="9080" protocol="HTTP/1.1"connectionTimeout="20000"redirectPort="8443" />
问题三:非端口问题的一闪而退
启动Tomcat直接一闪而退,在logs目录的日志里面找不到错误信息,
这个时候很大可能是因为Tomcat与JDK版本不对应导致的,有可能你的Tomcat是Tomcat9版,而你的JDK是1.7版本的,
Tomcat与JDK版本对应关系可以参考这篇文章Tomcat与JDK版本对应关系,Tomcat各版本特性
问题四:服务器的乱码和跨域问题
问题五: 在tomcat\webapps\下创建文件夹为什么tomcat重启就会丢失
1.修改/tomcat/conf/web.xml文件
<servlet><servlet-name>default</servlet-name><servlet-class>org.apache.catalina.servlets.DefaultServlet</servlet-class><init-param><param-name>debug</param-name><param-value>0</param-value></init-param><init-param><param-name>listings</param-name><param-value>true</param-value></init-param><load-on-startup>1</load-on-startup>
</servlet>
2.修改/tomcat/conf/server.xml文件
<Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true"xmlValidation="false" xmlNamespaceAware="false">
<!-- SingleSignOn valve, share authentication between web applicationsDocumentation at: /docs/config/valve.html -->
<!-- <Valve className="org.apache.catalina.authenticator.SingleSignOn" /> -->
<!-- Access log processes all example. Documentation at: /docs/config/valve.html -->
<!-- <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" prefix="localhost_access_log." suffix=".txt" pattern="common" resolveHosts="false"/> -->
<!--<Context path="/ser" docBase="D:\project\myProject\editor\WebRoot"></Context>--><!--<Context path="/mvc" docBase="D:\project\myProject\你的项目名\WebRoot" </Context>-->
</Host>
3.总结
重新redeploy的时候,tomcat会同步你本地的文件 ,因为本地没有相应的文件夹导致文件丢失。
这只是在开发过程中会遇到的问题实际应用中tomcat会startup.bat/shutdown.bat。
我们这样做,其实就是给工程配置虚拟路径,这样tomcat在启动的时候直接找到工程了,
上传的图片就不会仅仅只在/tomcat/webapps/目录,而直接到了你的工程里,
不管你怎样重启项目,或者清理缓存,上传的文件依然存在!
问题六:Tomcat启动报java.io.EOFException错误
信息如下
2019-11-25 10:24:05 org.apache.catalina.startup.HostConfig deployDirectory
信息: Deploying web application directory ROOT
2019-11-25 10:24:05 org.apache.catalina.session.StandardManager doLoad
严重: IOException while loading persisted sessions: java.io.EOFException
java.io.EOFExceptionat java.io.ObjectInputStream$PeekInputStream.readFully(ObjectInputStream.java:2280)at java.io.ObjectInputStream$BlockDataInputStream.readShort(ObjectInputStream.java:2749)at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:779)at java.io.ObjectInputStream.<init>(ObjectInputStream.java:279)at org.apache.catalina.util.CustomObjectInputStream.<init>(CustomObjectInputStream.java:58)at org.apache.catalina.session.StandardManager.doLoad(StandardManager.java:246)at org.apache.catalina.session.StandardManager.load(StandardManager.java:204)at org.apache.catalina.session.StandardManager.startInternal(StandardManager.java:465)at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:140)at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5025)at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:140)at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:812)at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:787)at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:570)at org.apache.catalina.startup.HostConfig.deployDirectory(HostConfig.java:1010)at org.apache.catalina.startup.HostConfig.deployDirectories(HostConfig.java:933)at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:468)at org.apache.catalina.startup.HostConfig.start(HostConfig.java:1267)at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:308)at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:119)at org.apache.catalina.util.LifecycleBase.fireLifecycleEvent(LifecycleBase.java:89)at org.apache.catalina.util.LifecycleBase.setState(LifecycleBase.java:328)at org.apache.catalina.util.LifecycleBase.setState(LifecycleBase.java:308)at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:1043)at org.apache.catalina.core.StandardHost.startInternal(StandardHost.java:738)at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:140)at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:1035)at org.apache.catalina.core.StandardEngine.startInternal(StandardEngine.java:289)at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:140)at org.apache.catalina.core.StandardService.startInternal(StandardService.java:442)at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:140)at org.apache.catalina.core.StandardServer.startInternal(StandardServer.java:674)at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:140)at org.apache.catalina.startup.Catalina.start(Catalina.java:596)at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)at java.lang.reflect.Method.invoke(Method.java:597)at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:303)at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:431)
2019-11-25 10:24:05 org.apache.catalina.session.StandardManager startInternal
严重: Exception loading sessions from persistent storage
java.io.EOFExceptionat java.io.ObjectInputStream$PeekInputStream.readFully(ObjectInputStream.java:2280)at java.io.ObjectInputStream$BlockDataInputStream.readShort(ObjectInputStream.java:2749)at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:779)at java.io.ObjectInputStream.<init>(ObjectInputStream.java:279)at org.apache.catalina.util.CustomObjectInputStream.<init>(CustomObjectInputStream.java:58)at org.apache.catalina.session.StandardManager.doLoad(StandardManager.java:246)at org.apache.catalina.session.StandardManager.load(StandardManager.java:204)at org.apache.catalina.session.StandardManager.startInternal(StandardManager.java:465)at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:140)at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5025)at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:140)at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:812)at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:787)at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:570)at org.apache.catalina.startup.HostConfig.deployDirectory(HostConfig.java:1010)at org.apache.catalina.startup.HostConfig.deployDirectories(HostConfig.java:933)at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:468)at org.apache.catalina.startup.HostConfig.start(HostConfig.java:1267)at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:308)at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:119)at org.apache.catalina.util.LifecycleBase.fireLifecycleEvent(LifecycleBase.java:89)at org.apache.catalina.util.LifecycleBase.setState(LifecycleBase.java:328)at org.apache.catalina.util.LifecycleBase.setState(LifecycleBase.java:308)at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:1043)at org.apache.catalina.core.StandardHost.startInternal(StandardHost.java:738)at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:140)at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:1035)at org.apache.catalina.core.StandardEngine.startInternal(StandardEngine.java:289)at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:140)at org.apache.catalina.core.StandardService.startInternal(StandardService.java:442)at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:140)at org.apache.catalina.core.StandardServer.startInternal(StandardServer.java:674)at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:140)at org.apache.catalina.startup.Catalina.start(Catalina.java:596)at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)at java.lang.reflect.Method.invoke(Method.java:597)at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:303)at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:431)
2019-11-25 10:24:05 org.apache.catalina.startup.HostConfig deployDirectory
错误的原因是:EOFException表示输入过程中意外地到达文件尾或流尾的信号,导致从session中获取数据失败。
这是由于tomcat上次非正常关闭时有一些活动session被持久化(表现为一些临时文件),
在重启时,tomcat尝试去恢复这些session的持久化数据但又读取失败造成的。此异常不影响系统的使用。
可以看到,我正在使用的项目中多了一个sessions.ser文件,把此文件删除即可tomcat即可正常的使用.
如果你的项目中不知道是哪个项目引起的.就把Catalina目录下的所有文件删除,即可.
问题七:解决跨域问题
protected void doOptions(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {resp.setHeader("Access-Control-Allow-Credentials", "true");resp.setHeader("Access-Control-Allow-Origin", req.getHeader("origin"));resp.setHeader("Access-Control-Allow-Methods", "*");resp.setHeader("Access-Control-Allow-Headers", "x-requested-with,content-type");resp.setContentType("application/json");resp.setCharacterEncoding("utf-8");super.doOptions(req, resp);}