[转]Java——Servlet的配置和测试
本文转自:http://blog.csdn.net/makefish/article/details/6904807
本文以一个实例介绍如何用Java开发Servlet。
主要内容有:
配置和验证Tomcat。
测试Servlet
在这之前需要安装Java,请参考“Java——环境配置和Hello”。
第一部分:配置和验证Tomcat:
1,下载Tomcathttp://tomcat.apache.org/download-70.cgi
2, 设置classpath
追加C:\MY\tools\java\apache-tomcat-7.0.22\lib\servlet-api.jar
3,设置JAVA_HOME和JRE_HOME
C:\Program Files\Java\jre7
C:\Program Files\Java\jdk1.7.0_01
4,启动tomcat
C:\MY\tools\java\apache-tomcat-7.0.22\bin>startup.bat
5,打开测试页面
http://127.0.0.1:8080/
6,编写代码:
目录结构:
根目录:C:\MY\tools\java\apache-tomcat-7.0.22\webapps\myapp
C:\MY\tools\java\apache-tomcat-7.0.22\webapps\myapp\WEB-INF\web.xml
C:\MY\tools\java\apache-tomcat-7.0.22\webapps\myapp\index.jsp
web.xml开始******************************
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<display-name>My Web Application</display-name>
<description>
A application for test.
</description>
</web-app>
web.xml结束******************************
index.jsp开始******************************
<html>
<body>
<center>
Now time is: <%=new java.util.Date()%>
</center>
</body>
</html>
index.jsp结束******************************
7,重启tomcat
C:\MY\tools\java\apache-tomcat-7.0.22\bin>shutdown.bat
C:\MY\tools\java\apache-tomcat-7.0.22\bin>startup.bat
8,打开页面http://localhost:8080/myapp/index.jsp
第二部分:测试Servlet
1,编写Servlet,TestServlet.java
package xuxing.java.servelt;
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 TestServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
PrintWriter out=response.getWriter();
out.println("<html><body><h1>This is a servlet TestServlet.</h1></body></html>");
out.flush();
}
}
2,编译并拷贝TestServlet.java
1),javac TestServlet.java
javac TestServlet.java
这个会生成TestServlet.class。
2),建立立文件夹C:\MY\tools\java\apache-tomcat-7.0.22\webapps\myapp\WEB-INF\classes\xuxing\java\并将TestServlet.class拷贝进去。3,修改 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">
<display-name>My Web Application</display-name>
<description>A application for test.</description>
<servlet>
<servlet-name>TestServlet</servlet-name>
<servlet-class>xuxing.java.servlet.TestServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>TestServlet</servlet-name>
<url-pattern>/testServlet</url-pattern>
</servlet-mapping>
</web-app>
4,重启Tomcat
C:\MY\tools\java\apache-tomcat-7.0.22\bin>shutdown.bat
C:\MY\tools\java\apache-tomcat-7.0.22\bin>startup.bat
5,打开页面http://localhost:8080/myapp/testServlet
posted on 2012-06-13 17:03 freeliver54 阅读(11998) 评论(3) 编辑 收藏 举报