servlet入门程序

servlet入门程序

1 在webapps目录下新建一个web应用

目录结构:
1 day05
2 WEB-INF
3 classes
4 FirstServlet.java
3 lib
3 web.xml

FirstServlet.java

package cn.itcast;

import java.io.*;
import javax.servlet.*;

public class FirstServlet extends GenericServlet {

    public void service(ServletRequest req, ServletResponse res) throws ServletException, java.io.IOException {

        String str = "Hello Servlet!!!";
        OutputStream out = res.getOutputStream();
        out.write(str.getBytes());
    }
}

2 在命令行输入如下命令:

第一步 cd classes目录
第二步(设置临时jar) set classpath=”classpath”;C:\apache-tomcat-7.0.82\lib\servlet-api.jar
第三部 javac -d . FirstServlet.java

3 配置servlet

web.xml



<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>FirstServletservlet-name>
    <servlet-class>cn.itcast.FirstServletservlet-class>
    servlet>

    <servlet-mapping>
      <servlet-name>FirstServletservlet-name>
      <url-pattern>/thereurl-pattern>
    servlet-mapping>

web-app>

4 启动tomcat

5 打开浏览器并输入

http://localhost:8080/day05/there

你可能感兴趣的:(servlet与jsp)