代码:
<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-name>HelloServletservlet-name>
<servlet-class>com.bjsxt.servlet.HelloServletservlet-class>
servlet>
<servlet-mapping>
<servlet-name>HelloServletservlet-name>
<url-pattern>/hellourl-pattern>
servlet-mapping>
web-app>
package com.bjsxt.servlet;
import javax.servlet.*;
import java.io.IOException;
public class HelloServlet implements Servlet {
@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("service方法被调用了...");
}
@Override
public String getServletInfo() {
return null;
}
@Override
public void destroy() {
}
}
在servlet3.0之后使用注解:替代配置文件
package com.bjsxt.servlet;
import javax.servlet.*;
import java.io.IOException;
@WebServlet("/hello")
public class HelloServlet implements Servlet {
@Override
public void init(ServletConfig servletConfig) throws ServletException {
}
@Override
public ServletConfig getServletConfig() {
return null;
}
/**
* 用于提供服务, 接收请求, 处理响应
*
* @param servletRequest
* @param servletResponse
* @throws ServletException
* @throws IOException
*/
@Override
public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
System.out.println("service方法被调用了...");
}
@Override
public String getServletInfo() {
return null;
}
@Override
public void destroy() {
}
}
包括Spring,
Spring中的事务也可以用XML进行配置。但是直接加注解也可以做到。
注解可以替代很复杂的配置文件。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)