JSP学习之web发布
时间:2010-08-08 来源:Codeplayer
1.环境:winXP + Tomcat6.0.29
2.步骤:
1)编写jsp文件en01.jsp
2)编辑%tomcathome%\conf\server.xml,在host标签中加入
<Context path="/虚拟目录名(en)" docBase="本地路径(work)" debug="0" reloadable="true" crossContext="true"></Context>
3)将en01.jsp放入"本地路径"下,启动tomcat后访问http://localhost:8080/虚拟目录名/en01.jsp即可执行. 注:访问en01.jsp时使用“虚拟路径”和“本地路径”都是可以的。 3.加入servlet处理能力
3)将en01.jsp放入"本地路径"下,启动tomcat后访问http://localhost:8080/虚拟目录名/en01.jsp即可执行. 注:访问en01.jsp时使用“虚拟路径”和“本地路径”都是可以的。 3.加入servlet处理能力
- 1)编写firstServ.java:
package useServ;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.*;
import java.io.PrintWriter;
import javax.servlet.*;
public class firstServ extends GenericServlet{
public void service(ServletRequest request, ServletResponse response)
throws ServletException,IOException{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head>");
out.println("<title>use Servlet</title>");
out.println("<body>");
out.println("<h1>This is my first Servlet<br>");
out.println("</body>");
out.println("</html>");
}
public void service(ServletRequest request, ServletResponse response)
throws ServletException,IOException{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head>");
out.println("<title>use Servlet</title>");
out.println("<body>");
out.println("<h1>This is my first Servlet<br>");
out.println("</body>");
out.println("</html>");
}
}
2)编译firstServ.java,编译之前添加%Tomcathome%\lib\servlet-api.jar和jsp-api.jar到环境变量的CLASSPATH中,否则会出现无法找到javax.servlet的错误。
3)放置firstServ.class到webapps\本地路径\WEB-INFO\classes\useServ\下
4)编辑 本地路径\WEB-INFO\web.xml,添加Servlet映射如下:
<web-app>
<display-name>firstServ</display-name>
<description>firstServ</description>
-
<servlet>
<servlet-name>firstServ</servlet-name>
<servlet-class>useServ.firstServ</servlet-class>
</servlet>
-
<servlet-mapping>
<servlet-name>firstServ</servlet-name>
<url-pattern>/firstServ</url-pattern>
</servlet-mapping>
</web-app>
5)重启Tomcat,访问localhost:8080\work或en\firstServ即可
相关阅读 更多 +