第一个web 程序(servlet 和 jsp )&

开发工具是便于程序员的编写,真正运行的代码不是编写的代码,而是
tomcat服务器中部署好的代码。tomcat 会根据请求自动调用对应的
代码进行请求处理。

可能遇到的问题:

1、

    没有classes文件

2、

    不能继承HttpServlet

---------------------------------------------------------------------------------------------------

经验总结:

    项目名可以变

代码:

  1、继承HttpServlet

package com.bw.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class MyServlet extends HttpServlet{
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.getWriter().write("你好,张王岩");
        
        System.out.println("你好,张王岩");
    }

}

2、web 配置

<?xml version="1.0" encoding="UTF-8"?>
 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation
="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" id="WebApp_ID" version="4.0"> 
 <!-- 配置servlet -->
  
   <servlet>
   
      <servlet-name>my</servlet-name>
      <servlet-class>com.bw.servlet.MyServlet</servlet-class>
   </servlet>
 <!-- 配置servlet类路径 -->
 <!-- 配置询问方式  -->
 <servlet-mapping>
     <servlet-name>my</servlet-name>
     <url-pattern>/my</url-pattern>
  </servlet-mapping>
</web-app>

3、运行发布

猜你喜欢

转载自www.cnblogs.com/zwyzwy/p/11923388.html