Tomcat配置
安装完后的主程序所在的目录,安装完毕,打开tomcat(如何启动参考)
服务器默认是在8080端口监听。在浏览器的地址栏输入:http://localhost:8080看见界面表明服务器安装正确。
配置:将端口号改为80,避免输入端口号,修改conf/server.xml,相应内容改为
<Connector port="80" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
核心配置:虚拟目录
虚拟目录下一定包含WEB—INF/web.xml
先在电脑建立一个目录,哪个盘都行(后面会映射)eg:D:\hixin\webandroid,将*/webapps/ROOT/下面的WEB—INF文件夹复制到新建的目录下
建立映射关系:修改conf/server.xml文件:
好啦,配置好了两个虚拟目录,path是访问时输入的地址,浏览器中输入http://localhost/apk就会自动找到硬盘D:\hixin\webandroid。
后要重启tomcat才有效
ps:conf/context.xml
<?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. --> <!-- The contents of this file will be loaded for each web application --> <Context reloadable="true"> <!-- Default set of monitored resources --> <WatchedResource>WEB-INF/web.xml</WatchedResource> <!-- Uncomment this to disable session persistence across Tomcat restarts --> <!-- <Manager pathname="" /> --> <!-- Uncomment this to enable Comet connection tacking (provides events on session expiration as well as webapp lifecycle) --> <!-- <Valve className="org.apache.catalina.valves.CometConnectionManagerValve" /> --> </Context>
设置了reloadable="true",这时修改相应映射下的xml就不需要重启tomcat啦,上面的xml只监听WEB-INF/下的xml变化
如果报404:表示路径错误,则还需要修改conf/web.xml文件,listings下的false改成true,重启tomcat。
时隔数日:再次安装依然出现问题(哈哈,一闪而过,其实是没有问题的)。以上配置并没有问题,版本Tomcat7.0
启动一闪而过,网上一直拿start.bat说事儿,其实7.0版本已经没有啦。
安装完毕后,默认是下面这样的,从Monitor启动,出现上面右图所示的界面,其实这个时候tomcat已经启动啦,如果再去bin目录下面点击Tomcat7.exe
就会出现一闪而过的现象。如果点击右图的stop按钮,待tomcat关闭服务后,再点击Tomcat7.exe,则熟悉的窗口才会出现,同时注意到再点击右图start
已经没有反应。哈哈,感觉新版多了一种启动方式
webApp:
第一个Servlet程序:
import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class HelloWorldServlet extends HttpServlet { private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { PrintWriter out = resp.getWriter(); out.println("<html><head><title></title></head><body>hello world !!</body></html>"); out.flush(); out.close(); } }
要注意:使用eclipse,默认是自动编译的,因为写Servlet通常没有main方法,eclipse也就没有run as选项,很多时候在工程目录下也看到生成了对应的class文件,但其实是空的。如果直接放在webapps下面,会报错
java.lang.ClassCastException: *** cannot be cast to javax.servlet.Servlet
遇到这种情况可以用Java Decomplier软件查看生成的class文件是不是有内容
web.xml文件:
<?xml version="1.0" encoding="ISO-8859-1"?> <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0" metadata-complete="true"> <servlet> <servlet-name>myfirstservlet</servlet-name> <servlet-class>HelloWorldServlet</servlet-class> </servlet> <!-- Define the Manager Servlet Mapping --> <servlet-mapping> <servlet-name>myfirstservlet</servlet-name> <url-pattern>/HelloWorldServlet</url-pattern> <!-- Mapping address with Servlet --> </servlet-mapping> </web-app>
<url-pattern>/HelloWorldServlet</url-pattern> <!-- Mapping address with Servlet -->
这个参数很关键,决定浏览器输入地址的时候调用哪个Servlet
当然可以建立多个servlet-mapping,实现多个网址对应一个网页的效果,当然servlet-name一定要一样。
本例:
浏览器输入 http://127.0.0.1:8080/healthembed/HelloWorldServlet
就会出现一个hello world !! 的网页,查看源文件:
绝对路径和相对路径的问题:
http://127.0.0.1:8080/healthembed/index.html
绝对路径(以/打头或者http开头)
<form action=http://127.0.0.1:8080/healthembed/HelloWorldServlet method=post> <input type=text name=test> <input type=submit value="提交"> </form>.html
相对路径
<form action=HelloWorldServlet method=post> <input type=text name=test> <input type=submit value="提交"> </form>
从index。html所在路径再往下找一个HelloWorldServlet