Java——Servlet的配置和測試
阿新 • • 發佈:2019-02-19
本文以一個例項介紹如何用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