Java development environment series: a problem you can solve 99 percent of the mine diary

 

installation

https://archive.apache.org/dist/tomcat/

Recommended free installation version of Tomcat (no directory on Chinese and spaces), provided that the JDK is already installed and configured the environment variables.
Linux environment unzip:

unzip -o apache-tomcat-6.0.44-client.zip 或: tar -xzvf apache-tomcat-7.0.69.tar.gz

test

Double-click startup.bat, browser enter the url: localhost: 8080, if the pop Tomcat home page (black box could not close) the installation was successful.
Catalina.out view the log file, if there are mistakes, find conf \ Catalina \ localhost folder, there are two xml files host-manager.xml, manager.xml, delete them start Tomcat, no problem.

How to view the current version?

Into the implementation ./version.sh bin directory tomcat installation folder under linux

Into the implementation ./version.bat bin directory tomcat installation folder under windows

How to configure the administrator?

Profile: /conf/tomcat-users.xml
<? Xml Version = "1.0" encoding = "UTF-8"?>
<Tomcat-the Users>
 <Role rolename = "Manager" />
 <Role rolename = "ADMIN" / >
 <Role rolename = "Manager-GUI" />
 <Role rolename = "ADMIN-GUI" />
 <User username = "ADMIN" password = "ADMIN" Roles = "ADMIN-GUI, ADMIN, Manager-GUI, Manager" />
</ Tomcat-Users>
Note: If set Eclipse "Server Locations" is "Use Tomcat installation (takes control of Tomcat installation)", can not directly modify tomcat tomcat-users.xml installation directory / conf under, need to modify the file in Eclipse, and will take effect at the start tomcat, will be automatically updated tomcat tomcat-users.xml installation directory is in effect

How to resolve port conflicts?

Profile: /conf/server.xml
Note: 8080 is the default port, you can modify any of its ports, it is recommended to use more than 1024 port, port to avoid conflict with the public.
Review: restart the application.
<Connector connectionTimeout = "20000" port = "8080" protocol = "HTTP / 1.1" redirectPort = "8443" URIEncoding = "UTF-8" />
port conflict
in cmd window, netstat -ano, find the port number corresponding to the PID , → → process to find the corresponding process in task Manager, end the process can be.

How to modify the maximum number of threads?

server.xml
<Connector port="8880" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" maxThreads="1500" maxProcessors="450"
               URIEncoding="UTF-8"/>

How to solve the problem of a Tomcat will shut down other Tomcat while switched off?

In fact: startup.bat start using a Tomcat, but found other Tomcat together also launched, it is the same principle.
The reason:
shutdown.bat file has one: IF not "% CATALINA_HOME%" == "" GOTO gotHome, perform a shutdown command, the program will look for according to CATALINA_HOME, this means that if the environment variable configuration of this path is not empty, on the implementation of this, otherwise it "set CATALINA_HOME =% cURRENT_DIR%" , that is, assign it to the current path.
Solution is to if not "% CATALINA_HOME%" == "" goto gotHome
preceded rem, give it to comment out, so every time the execution of the command will only operate on the current path.
Namely:
REM IF not "% CATALINA_HOME%" == "" GOTO gotHome
Similarly, if you start tomcat port configuration is invalid (address already in use), in startup.bat in this place plus rem.
Figure:

Running multiple services

If you need to make a run multiple Tomcat service, just copy the server.xml file <Service> element, and modify the parameters, we can achieve a Tomcat running multiple services, of course, must provide services in different ports.

WEB container position setting (for Eclipse)

如果为灰色,需要把tomcat下的所有項目移除。并右击,clean...之后方可設置,启动后将又变为黑色。(Eclipse默认把部署的项目放在workspace工作空间中,MyEclipse默认放在Tomcat安装路径下)
选择第二个,这样在Eclipse启动了tomcat服务器,我们也能访问到tomcat本地管理首页。否則只能在DOS下使用Tomcat的启动命令才能访问Tomcat本地管理首页。

Tomcat目录结构

bin: 启动和关闭tomcat的服务器.

conf: 这个路径比较重要.放的是tomcat服务器的配置文件.

lib: tomcat支持的jar包

存放Tomcat服务器的核心类库(JAR文件);

如需扩展Tomcat,可将第三方类库复制到这个目录下。

logs: tomcat的日志记录.

temp: tomcat的临时文件的路径.

webapps:存放开发的项目的.一般情况下 一个项目会是一个文件夹.

该目录用于自动部署web应用;

将Web应用复制在该目录下,Tomcat会将该应用自动部署在容器中。

work: JSP被编译后文件存放的路径.(若发布新版本后页面仍显示老版本内容,可以将work目录下的文件清空,重新启动Tomcat),保存Web应用运行过程中编译生成的.class文件;该文件可以删除,但是每次启动Tomcat服务器时系统将再次建立该目录。

WEB工程/项目的目录结构

    web工程

            |--存放html 、css、js、img

            |--WEB-INF

            |-- web.xml  (当前项目配置文件)

            |-- classes   存放java类编译后的文件.

            |-- lib 需要使用的第三方的jar包.

内存溢出问题如何解决?

在Eclipse中,内存溢出(报不能创建JAVA虚拟机错时,也可能是这里配错了。)
1、双击Tomcat,点击Open launch configuration,Arguments,
2、在VM arguments文本域最后边加:
-Xms256m -Xmx1024m -XX:MaxPermSize=256m

MyEclipse中
1、依次选择Preferences,Servers,Tomcat,Tomcat 6.x
2、在Optional program arguments后面加上:
-Xms256m -Xmx1024m -XX:MaxPermSize=256m

Linux系统下
1、在Tomcat安装目录下的/bin目录下:修改catalina.sh,加入:
JAVA_OPTS='-server -XX:PermSize=256M -XX:MaxPermSize=512m -Xmx15360m'
2、使用【chmod 777 catalina.sh】给catalina.sh赋权。

JSP页面乱码问题如何排查和解决?

JSP页面中告诉浏览器使用什么编码:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
HTML中设置:
<meta http-equiv="content-type" content="text/html;charset=utf-8">
Servlet中设置:
response.setContentType("text/html;charset=utf-8");
response.getWriter().print(str);

服务器接收浏览器传递的中文参数:

如果是POST请求:
request.setCharacterEncoding("utf-8");
String userName = request.getParameter();
注:getParameter()获取POST请求参数默认使用ISO-8859-1编码,setCharacterEncoding()方法仅对POST请求有效。
如果是GET请求:
String name = request.getParameter("username");
name = new String(name.getBytes("iso-8859-1"),"utf-8");
在Tomcat容器的server.xml中配置URIEncoding:
<Connector connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443" URIEncoding="UTF-8" />

浏览器地址栏编码
http://localhost:8080/servlet/regist?name="张三"
如果在浏览器地址栏中直接给出带参数的URL,不是通过超链接方法访问服务器,那么这个参数是GBK编码的。没有办法处理这问题,因为在页面使用UTF-8编码,Servlet需要用UTF-8来解码,而此时URL中编码是GBK。但直接在地址栏中根本就不应该直接输入带有参数的URL,只有超链接中的URL是受页面的编码控制的。

IE6对UTF-8不友好(使用JS处理)
IE6对UTF-8不是很友好!它会把两个字节当成一个中文来处理,当字节数为奇数时,那么就最后一个字节就会丢失。例如我们的GET参数是“大家好”,对于UTF-8编码而言,它就是9个字节,因为中文在UTF-8编码中3个字节,三个汉字对应9个字节。IE6认为2个字节是一个汉字,那么最后一个字节不会发送到服务器端,IE6很不安全。
处理这个问题的办法是把GET请求中的参数使用JavaScript做URL编码,URL编码后的内容就不再是中文了,这样IE6也就不会丢失字节了。
<a href='javascript:_go()'>Encoding</a>
<script type="text/javascript">
function _go() {
location.href = encodeURI("EncodingServlet?name=大家好");
}
</script>

http://localhost/encoding/EncodingServlet?name=%E5%A4%A7%E5%AE%B6%E5%A5%BD
在使用URL编码后,大家好已经变成了%E5%A4%A7%E5%AE%B6%E5%A5%BD。这样就不会再丢失字节了。

Guess you like

Origin www.cnblogs.com/xyhero/p/11257578.html