servlet的概念请看官网:
*按照个人的理解servlet就是接口,依附在服务器上运行的一类接口。例如Tomcat服务器
servlet快速入门:
1、创建java类的接口
package com.yuan.servlet;
import javax.servlet.*;
import java.io.IOException;
//第一步
public class servletDemo1 implements Servlet{
///添加重载的办法直接按ALT+enter快捷可获得
@Override
public void init(ServletConfig servletConfig) throws ServletException {
}
@Override
public ServletConfig getServletConfig() {
return null;
}
//主方法
@Override
public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
System.out.println("hello servlet");
}
@Override
public String getServletInfo() {
return null;
}
@Override
public void destroy() {
}
}
2、配置web.xml文件
*配置servlet 名字随便起
*配置包的路径:此包名字是我项目下的名字例如:com.yuan.servle.类名
<servlet-name>demo<\servlet-name>
<servlet-class>com.yuan.servle.servletDemo1<\servlet-class>
*配置访问的路径
<servlet-mapping>
<servlet-name>demo1</servlet-name>
<url-pattern>/demo1</url-pattern>
</servlet-mapping>
配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<!-- 配置servlet -->
<servlet>
<servlet-name>demo1</servlet-name>
<servlet-class>com.yuan.servlet.selectDemo1</servlet-class>
</servlet>
<!-- 配置路径 -->
<servlet-mapping>
<servlet-name>demo1</servlet-name>
<url-pattern>/demo1</url-pattern>
</servlet-mapping>
</web-app>
网友评论